cleanup documentation, examples and coverage
This commit is contained in:
41
examples/compressedStream.ts
Normal file
41
examples/compressedStream.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
// Compressed Stream Parsing
|
||||
|
||||
import { XmlParser } from "../";
|
||||
import { StreamZip } from "node-stream-zip";
|
||||
|
||||
const zip = new StreamZip({
|
||||
file: archiveName,
|
||||
storeEntries: true
|
||||
});
|
||||
|
||||
|
||||
const opts = {}; // see `Available Constructor Options` section below.
|
||||
|
||||
const parser = new XmlParser(opts);
|
||||
|
||||
parser.on("item", (item) => {
|
||||
// consume the item object here
|
||||
});
|
||||
|
||||
parser.on("end", () => {
|
||||
// parsing ended no more data events will be raised
|
||||
});
|
||||
|
||||
parser.on("error", (error) => {
|
||||
// error occurred
|
||||
// NOTE: when error event emitted no end event will be emitted
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
xmlStream.pipe(parser); // pipe your input xmlStream to parser.
|
||||
// readable
|
||||
parser.on("readable", () => {
|
||||
// if you don't want to consume "data" on "data" events you can wait for readable event and consume data by calling parser.read()
|
||||
});
|
||||
|
||||
zip.on("ready", () => {
|
||||
zip.stream('path/inside/zip.xml', (err, stm) => {
|
||||
stm.pipe(parser);
|
||||
stm.on('end', () => zip.close());
|
||||
});
|
||||
});
|
||||
27
examples/interestedNodes.ts
Normal file
27
examples/interestedNodes.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
// By listening for interested nodes.
|
||||
|
||||
import { XmlParser } from "../";
|
||||
|
||||
const opts = {}; // see `Available Constructor Options` section below.
|
||||
|
||||
const parser = new XmlParser(opts);
|
||||
|
||||
parser.on("item", (item) => {
|
||||
// consume the item object here
|
||||
});
|
||||
|
||||
parser.on("end", () => {
|
||||
// parsing ended no more data events will be raised
|
||||
});
|
||||
|
||||
parser.on("error", (error) => {
|
||||
// error occurred
|
||||
// NOTE: when error event emitted no end event will be emitted
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
xmlStream.pipe(parser); // pipe your input xmlStream to parser.
|
||||
// readable
|
||||
parser.on("readable", () => {
|
||||
// if you don't want to consume "data" on "data" events you can wait for readable event and consume data by calling parser.read()
|
||||
});
|
||||
11
examples/parse.ts
Normal file
11
examples/parse.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
// By passing a string or buffer to parse function
|
||||
|
||||
import { XmlParser } from "../";
|
||||
|
||||
const opts = { resourcePath: "/items/item" }; // resourcePath is manditory when using parse method
|
||||
|
||||
const parser = new XmlParser(opts);
|
||||
|
||||
parser.parse(stringOrBuffer, (err, data) => {
|
||||
// consume data here
|
||||
});
|
||||
27
examples/reasourcePathRead.ts
Normal file
27
examples/reasourcePathRead.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
// By passing the resourcePath and reading data by calling
|
||||
// `read` method instead listening for data events.
|
||||
|
||||
import {XmlParser} from "../";
|
||||
|
||||
const opts = { resourcePath: "/items/item" };
|
||||
|
||||
const parser = new XmlParser(opts);
|
||||
|
||||
parser.on("end", () => {
|
||||
// parsing ended no more data events will be raised
|
||||
});
|
||||
|
||||
parser.on("error", (error) => {
|
||||
// error occurred
|
||||
// NOTE: when error event emitted no end event will be emitted
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
xmlStream.pipe(parser); // pipe your input xmlStream to parser.
|
||||
// readable
|
||||
parser.on("readable", () => {
|
||||
// if you don't want to consume "data" on "data" events you can wait
|
||||
// for readable event and consume data by calling parser.read()
|
||||
});
|
||||
// after readable event occured you can call read method and get data.
|
||||
parser.read(); // will return one object at a time.
|
||||
28
examples/resourcePath.ts
Normal file
28
examples/resourcePath.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
// By passing a resource path.
|
||||
|
||||
import { XmlParser } from "../";
|
||||
|
||||
const opts = { resourcePath: "/items/item" };
|
||||
|
||||
const parser = new XmlParser(opts);
|
||||
|
||||
parser.on("data", (data) => {
|
||||
// consume the data object here
|
||||
});
|
||||
|
||||
parser.on("end", () => {
|
||||
// parsing ended no more data events will be raised
|
||||
});
|
||||
|
||||
parser.on("error", (error) => {
|
||||
// error occurred
|
||||
// NOTE: when error event emitted no end event will be emitted
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
xmlStream.pipe(parser); // pipe your input xmlStream to parser.
|
||||
// readable
|
||||
parser.on("readable", () => {
|
||||
// if you don't want to consume "data" on "data" events you
|
||||
// can wait for readable event and consume data by calling parser.read()
|
||||
});
|
||||
Reference in New Issue
Block a user