forked from Wernfried/xml-twig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
51 lines (36 loc) · 1.81 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const fs = require('fs');
const path = require('path');
const twig = require('./twig.js');
const { pipeline } = require('stream/promises');
async function parse(file, handler, options) {
const parser = twig.createParser(handler, options);
const reader = fs.createReadStream(file);
console.log(`Start parsing of ${path.basename(file)} with ${options.method}`);
await pipeline(reader, parser);
console.log(`Parsing ${path.basename(file)} done`);
}
//const parser = twig.createParser({ tag: twig.Root, function: anyHandler }, { method: 'sax' });
//fs.createReadStream(`${__dirname}/samples/bookstore.xml`).pipe(parser);
//fs.createReadStream(`${__dirname}/samples/encoding-UTF-8.xml`).pipe(parser);
//fs.createReadStream(`${__dirname}/samples/encoding-UTF-16LE.xml`).pipe(parser);
//fs.createReadStream(`${__dirname}/samples/encoding-UTF-16BE.xml`).pipe(parser);
function anyHandler(elt) {
console.log(`<${elt.name}> => ${elt.text} -> ${JSON.stringify(elt.attributes)}`);
}
function nsHandler(elt) {
console.log(`${elt.name} => ${JSON.stringify(elt.namespace)} -> ${JSON.stringify(elt.attributes)}`);
}
function piHandler(elt) {
console.log(`${elt.name} -> ${JSON.stringify(elt.PI)}`);
}
const main = async () => {
for (let file of ["bookstore", "breakfast-menu"]) {
for (let method of ["sax", "expat", "saxophone"])
await parse(`${__dirname}/samples/${file}.xml`, { tag: twig.Any, function: anyHandler }, { method: method });
}
for (let method of ["sax", "expat", "saxophone"])
await parse(`${__dirname}/samples/xmlns.xml`, { tag: twig.Any, function: nsHandler }, { method: method, xmlns: true });
for (let method of ["sax", "expat", "saxophone"])
await parse(`${__dirname}/samples/processingInstruction.xml`, { tag: twig.Root, function: piHandler }, { method: method });
}
main();