xml-twig 1.7.0 → 1.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/doc/twig.md +75 -40
- package/package.json +8 -2
- package/twig.js +1303 -1290
- package/demo.js +0 -12
- package/doc/build.sh +0 -6
- package/samples/bookstore.xml +0 -48
- package/samples/breakfast-menu.xml +0 -44
- package/samples/encoding-UTF-16BE.xml +0 -0
- package/samples/encoding-UTF-16LE.xml +0 -0
- package/samples/encoding-UTF-8.xml +0 -2
- package/samples/memory-test.js +0 -77
- package/samples/processingInstruction.xml +0 -28
- package/samples/sample.js +0 -96
- package/samples/speed-test.js +0 -92
- package/samples/xmlns.xml +0 -19
- package/test.js +0 -51
package/demo.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const twig = require('./twig.js');
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const parser = twig.createParser({ tag: 'book', event: 'bookElement' });
|
|
6
|
-
fs.createReadStream(`${__dirname}/samples/bookstore.xml`).pipe(parser);
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
parser.on('bookElement', (elt) => {
|
|
10
|
-
console.log(`<${elt.name}> finished after ${parser.currentLine} lines`);
|
|
11
|
-
})
|
|
12
|
-
|
package/doc/build.sh
DELETED
package/samples/bookstore.xml
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<bookstore id="a">
|
|
3
|
-
<book category="cooking">
|
|
4
|
-
<title lang="en">Everyday Italian</title>
|
|
5
|
-
<author>Giada De Laurentiis</author>
|
|
6
|
-
<year>2005</year>
|
|
7
|
-
<price>30.00</price>
|
|
8
|
-
</book>
|
|
9
|
-
<!-- A comment -->
|
|
10
|
-
<book category="children">
|
|
11
|
-
<title lang="en">Harry Potter</title>
|
|
12
|
-
<author>J K. Rowling</author>
|
|
13
|
-
<year>2005</year>
|
|
14
|
-
<price>29.99</price>
|
|
15
|
-
</book>
|
|
16
|
-
<ebook category="fantasy">
|
|
17
|
-
<title lang="en">Harry Potter</title>
|
|
18
|
-
<author>Joanne Kathleen Rowling</author>
|
|
19
|
-
<year>2001 </year>
|
|
20
|
-
<price>12.99</price>
|
|
21
|
-
<format>Kindle</format>
|
|
22
|
-
<device>ePub</device>
|
|
23
|
-
</ebook>
|
|
24
|
-
<book category="web">
|
|
25
|
-
<title lang="en">XQuery Kick Start</title>
|
|
26
|
-
<author>James McGovern</author>
|
|
27
|
-
<author>Per Bothner</author>
|
|
28
|
-
<author>Kurt Cagle</author>
|
|
29
|
-
<author>James Linn</author>
|
|
30
|
-
<author>Vaidyanathan Nagarajan</author>
|
|
31
|
-
<year>2003</year>
|
|
32
|
-
<price>29.99</price>
|
|
33
|
-
</book>
|
|
34
|
-
<ebook category="biography">
|
|
35
|
-
<title lang="en">The Autobiography of Benjamin Franklin</title>
|
|
36
|
-
<author>Benjamin Franklin</author>
|
|
37
|
-
<year>1996</year>
|
|
38
|
-
<price>39.99</price>
|
|
39
|
-
<format>Kindle</format>
|
|
40
|
-
<device>ePub</device>
|
|
41
|
-
</ebook>
|
|
42
|
-
<book category="web">
|
|
43
|
-
<title lang="en">Learning XML</title>
|
|
44
|
-
<author>Erik T. Ray</author>
|
|
45
|
-
<year>2003</year>
|
|
46
|
-
<price>39.95</price>
|
|
47
|
-
</book>
|
|
48
|
-
</bookstore>
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<breakfast_menu>
|
|
3
|
-
<food>
|
|
4
|
-
<name>Belgian Waffles</name>
|
|
5
|
-
<price>$5.95</price>
|
|
6
|
-
<description>
|
|
7
|
-
Two of our famous Belgian Waffles with plenty of real maple syrup
|
|
8
|
-
</description>
|
|
9
|
-
<calories>650</calories>
|
|
10
|
-
</food>
|
|
11
|
-
<food>
|
|
12
|
-
<name>Strawberry Belgian Waffles</name>
|
|
13
|
-
<price>$7.95</price>
|
|
14
|
-
<description>
|
|
15
|
-
Light Belgian waffles covered with strawberries and whipped cream
|
|
16
|
-
</description>
|
|
17
|
-
<calories>900</calories>
|
|
18
|
-
</food>
|
|
19
|
-
<food>
|
|
20
|
-
<name>Berry-Berry Belgian Waffles</name>
|
|
21
|
-
<price>$8.95</price>
|
|
22
|
-
<description><![CDATA[
|
|
23
|
-
Belgian waffles covered with assorted <bold>fresh</bold> berries and whipped cream
|
|
24
|
-
]]>
|
|
25
|
-
</description>
|
|
26
|
-
<calories>900</calories>
|
|
27
|
-
</food>
|
|
28
|
-
<food>
|
|
29
|
-
<name>French Toast</name>
|
|
30
|
-
<price>$4.50</price>
|
|
31
|
-
<description>
|
|
32
|
-
Thick slices made from our homemade sourdough bread
|
|
33
|
-
</description>
|
|
34
|
-
<calories>600</calories>
|
|
35
|
-
</food>
|
|
36
|
-
<food>
|
|
37
|
-
<name>Homestyle Breakfast</name>
|
|
38
|
-
<price>$6.95</price>
|
|
39
|
-
<description>
|
|
40
|
-
Two eggs, bacon or sausage, toast, and our ever-popular hash browns
|
|
41
|
-
</description>
|
|
42
|
-
<calories>950</calories>
|
|
43
|
-
</food>
|
|
44
|
-
</breakfast_menu>
|
|
Binary file
|
|
Binary file
|
package/samples/memory-test.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const process = require('process');
|
|
3
|
-
const twig = require('xml-twig');
|
|
4
|
-
|
|
5
|
-
let Entry = 0;
|
|
6
|
-
|
|
7
|
-
let parser = twig.createParser([{ tag: 'Entry', function: EntryHandler }], { method: 'expat' })
|
|
8
|
-
// http://aiweb.cs.washington.edu/research/projects/xmltk/xmldata/data/SwissProt/SwissProt.xml.gz
|
|
9
|
-
// For more files see http://aiweb.cs.washington.edu/research/projects/xmltk/xmldata/www/repository.html
|
|
10
|
-
let reader = fs.createReadStream(`SwissProt.xml`);
|
|
11
|
-
reader.pipe(parser);
|
|
12
|
-
|
|
13
|
-
function EntryHandler(elt) {
|
|
14
|
-
Entry++;
|
|
15
|
-
if (Entry % 10000 === 0) {
|
|
16
|
-
console.log(`Entry ${Entry}`)
|
|
17
|
-
let len = elt.root().writer().toString().length;
|
|
18
|
-
len = Math.round((len / 1024 / 1024 + Number.EPSILON) * 100) / 100;
|
|
19
|
-
console.log(`Size of XML string: ${len} MiB`);
|
|
20
|
-
for (const [key, value] of Object.entries(process.memoryUsage())) {
|
|
21
|
-
console.log(` Memory usage by ${key}, ${Math.round((value / 1024 / 1024 + Number.EPSILON) * 100) / 100} MiB`)
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
/*
|
|
29
|
-
**********************
|
|
30
|
-
* Results
|
|
31
|
-
**********************
|
|
32
|
-
|
|
33
|
-
# Set memory limit to 4GiB for demonstration reasons:
|
|
34
|
-
NODE_OPTIONS=--max-old-space-size=4096
|
|
35
|
-
|
|
36
|
-
Entry 10000
|
|
37
|
-
Size of XML string: 21.08 MiB
|
|
38
|
-
Memory usage by rss, 1897.36 MiB
|
|
39
|
-
Memory usage by heapTotal, 1855.68 MiB
|
|
40
|
-
Memory usage by heapUsed, 1807.43 MiB
|
|
41
|
-
Memory usage by external, 1.13 MiB
|
|
42
|
-
Memory usage by arrayBuffers, 0.7 MiB
|
|
43
|
-
|
|
44
|
-
Entry 20000
|
|
45
|
-
Size of XML string: 40.86 MiB
|
|
46
|
-
Memory usage by rss, 3615.75 MiB
|
|
47
|
-
Memory usage by heapTotal, 3562.62 MiB
|
|
48
|
-
Memory usage by heapUsed, 3482.97 MiB
|
|
49
|
-
Memory usage by external, 0.63 MiB
|
|
50
|
-
Memory usage by arrayBuffers, 0.2 MiB
|
|
51
|
-
|
|
52
|
-
<--- Last few GCs --->
|
|
53
|
-
|
|
54
|
-
[18648:000001F930FC8F90] 13906 ms: Scavenge 4047.7 (4123.2) -> 4047.3 (4133.7) MB, 3.4 / 0.0 ms (average mu = 0.332, current mu = 0.185) allocation failure;
|
|
55
|
-
[18648:000001F930FC8F90] 13916 ms: Scavenge 4054.1 (4133.7) -> 4053.8 (4135.2) MB, 4.0 / 0.0 ms (average mu = 0.332, current mu = 0.185) allocation failure;
|
|
56
|
-
[18648:000001F930FC8F90] 13925 ms: Scavenge 4055.1 (4135.2) -> 4054.3 (4156.2) MB, 8.3 / 0.0 ms (average mu = 0.332, current mu = 0.185) allocation failure;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
<--- JS stacktrace --->
|
|
60
|
-
|
|
61
|
-
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
|
|
62
|
-
1: 00007FF604481C7F node_api_throw_syntax_error+175855
|
|
63
|
-
2: 00007FF604406476 EVP_MD_meth_get_input_blocksize+59654
|
|
64
|
-
3: 00007FF604408160 EVP_MD_meth_get_input_blocksize+67056
|
|
65
|
-
4: 00007FF604EB0434 v8::Isolate::ReportExternalAllocationLimitReached+116
|
|
66
|
-
5: 00007FF604E9B7C2 v8::Isolate::Exit+674
|
|
67
|
-
6: 00007FF604D1D65C v8::internal::EmbedderStackStateScope::ExplicitScopeForTesting+124
|
|
68
|
-
7: 00007FF604D1A87B v8::internal::Heap::CollectGarbage+3963
|
|
69
|
-
8: 00007FF604D30AB3 v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath+2099
|
|
70
|
-
9: 00007FF604D3135D v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath+93
|
|
71
|
-
10: 00007FF604D40B20 v8::internal::Factory::NewFillerObject+816
|
|
72
|
-
11: 00007FF604A31565 v8::internal::DateCache::Weekday+1349
|
|
73
|
-
12: 00007FF604F4D961 v8::internal::SetupIsolateDelegate::SetupHeap+558193
|
|
74
|
-
13: 00007FF5B03D1532
|
|
75
|
-
|
|
76
|
-
*/
|
|
77
|
-
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
|
|
3
|
-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
|
4
|
-
<xsl:template match="/">
|
|
5
|
-
<emptyElement/>
|
|
6
|
-
<html>
|
|
7
|
-
<body>xx<!--Your comment-->xx
|
|
8
|
-
<h2>My CD Collection</h2>
|
|
9
|
-
<table border="1">
|
|
10
|
-
<tr>
|
|
11
|
-
<th style="text-align:left">Title</th>
|
|
12
|
-
<th style="text-align:left">Artist</th>
|
|
13
|
-
</tr>
|
|
14
|
-
<xsl:for-each select="catalog/cd">
|
|
15
|
-
<tr>
|
|
16
|
-
<td>
|
|
17
|
-
<xsl:value-of select="title"/>
|
|
18
|
-
</td>
|
|
19
|
-
<td>
|
|
20
|
-
<xsl:value-of select="artist"/>
|
|
21
|
-
</td>
|
|
22
|
-
</tr>
|
|
23
|
-
</xsl:for-each>
|
|
24
|
-
</table>
|
|
25
|
-
</body>
|
|
26
|
-
</html>
|
|
27
|
-
</xsl:template>
|
|
28
|
-
</xsl:stylesheet>
|
package/samples/sample.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const twig = require('../twig.js');
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
// Sample 1.1
|
|
6
|
-
function rootHandler(elt) {
|
|
7
|
-
console.log(`<${elt.name}> finished after ${parser.currentLine} lines`);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
parser = twig.createParser({ tag: twig.Root, function: rootHandler }, { method: 'sax' })
|
|
11
|
-
fs.createReadStream(`${__dirname}/bookstore.xml`).pipe(parser)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
// Sample 1.2
|
|
15
|
-
parser = twig.createParser({ tag: twig.Root, function: rootHandler }, { method: 'expat' })
|
|
16
|
-
/parser.write('<html><head><title>Hello World</title></head><body><p>Foobar</p></body></html>');
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// Sample 2
|
|
20
|
-
parser = twig.createParser({ tag: twig.Root, event: 'rootElement' }, { method: 'sax' })
|
|
21
|
-
fs.createReadStream(`${__dirname}/bookstore.xml`).pipe(parser)
|
|
22
|
-
|
|
23
|
-
parser.on('rootElement', (elt) => {
|
|
24
|
-
console.log(`<${elt.name}> finished after ${parser.currentLine} lines`);
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
// Sample 3
|
|
30
|
-
function bookHandler(elt) {
|
|
31
|
-
console.log(`${elt.attr("category")} ${elt.name} at line ${parser.currentLine}`)
|
|
32
|
-
elt.purge() // -> without `purge()` the entire XML document will be loaded into memory
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
handle_book = [
|
|
36
|
-
{ tag: 'book', function: bookHandler },
|
|
37
|
-
{ tag: 'ebook', function: bookHandler }
|
|
38
|
-
];
|
|
39
|
-
handle_book = [
|
|
40
|
-
{ tag: /book$/, function: bookHandler }
|
|
41
|
-
];
|
|
42
|
-
handle_book = [{
|
|
43
|
-
tag: function (name, elt) { return name.endsWith('book') },
|
|
44
|
-
function: bookHandler
|
|
45
|
-
}];
|
|
46
|
-
handle_book = [{
|
|
47
|
-
tag: function (name, elt) { return ['book', 'ebook'].includes(name) },
|
|
48
|
-
function: bookHandler
|
|
49
|
-
}];
|
|
50
|
-
handle_book = [{
|
|
51
|
-
tag: function (name, elt) { return ['book', 'ebook'].includes(elt.name) },
|
|
52
|
-
function: bookHandler
|
|
53
|
-
}];
|
|
54
|
-
|
|
55
|
-
parser = twig.createParser(handle_book, { method: 'sax' })
|
|
56
|
-
fs.createReadStream(`${__dirname}/bookstore.xml`).pipe(parser)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
// Sample 4
|
|
61
|
-
function anyHandler(elt) {
|
|
62
|
-
console.log(`${' '.repeat(elt.level)}${elt.name} => "${elt.text ?? ''}" at line ${parser.currentLine}`)
|
|
63
|
-
elt.purge() // -> without `purge()` the entire XML document will be loaded into memory
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
parser = twig.createParser({ tag: twig.Any, function: anyHandler })
|
|
67
|
-
// or with Regular Expression: `{ tag: tag: /i/, function: anyHandler }`
|
|
68
|
-
// or with Function: `{ tag: () => {return true}, function: anyHandler }`
|
|
69
|
-
fs.createReadStream(`${__dirname}/bookstore.xml`).pipe(parser)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
// Sample 5
|
|
73
|
-
const handle_ebook = [
|
|
74
|
-
{ tag: 'ebook', function: ebookHandler },
|
|
75
|
-
{ tag: twig.Root, function: rootHandler }
|
|
76
|
-
];
|
|
77
|
-
const parser = twig.createParser(handle_ebook, { partial: true })
|
|
78
|
-
fs.createReadStream(`${__dirname}/bookstore.xml`).pipe(parser);
|
|
79
|
-
|
|
80
|
-
function ebookHandler(elt) {
|
|
81
|
-
//console.log(`${elt.name} at line ${parser.currentLine}`)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function rootHandler(elt) {
|
|
85
|
-
console.log( elt.writer(' ').toString() );
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
function bookHandler(elt) {
|
|
90
|
-
if (elt.attr('category') == 'fantasy') {
|
|
91
|
-
console.log(elt.writer(true).toString());
|
|
92
|
-
let t = elt.addElement('newTag', 'some Text > more', {id:1},2);
|
|
93
|
-
console.log(t.writer(true).toString());
|
|
94
|
-
console.log(elt.root().writer(true).toString());
|
|
95
|
-
}
|
|
96
|
-
}
|
package/samples/speed-test.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const twig = require('../twig.js');
|
|
4
|
-
const { pipeline } = require('stream/promises');
|
|
5
|
-
const { DateTime } = require('luxon');
|
|
6
|
-
|
|
7
|
-
let NE = 0;
|
|
8
|
-
let startTime = DateTime.now();
|
|
9
|
-
let printNE = true;
|
|
10
|
-
|
|
11
|
-
function anyHandler(elt) {
|
|
12
|
-
NE++;
|
|
13
|
-
if (printNE && NE % 25 === 0) {
|
|
14
|
-
let d = DateTime.now().diff(startTime);
|
|
15
|
-
console.log(`\t${NE} NE in ${d.toFormat('mm:ss.S')}`);
|
|
16
|
-
}
|
|
17
|
-
elt.purge();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async function parse(method) {
|
|
21
|
-
const parser = twig.createParser([{ tag: 'subsession', function: anyHandler }], { method: method })
|
|
22
|
-
const reader = fs.createReadStream(`${__dirname}/20240304015234.9-MSRAN.xml`);
|
|
23
|
-
|
|
24
|
-
NE = 0;
|
|
25
|
-
startTime = DateTime.now();
|
|
26
|
-
await pipeline(reader, parser);
|
|
27
|
-
let d = DateTime.now().diff(startTime);
|
|
28
|
-
console.log(`Finished with ${method} in ${d.toFormat('mm:ss.S')}`);
|
|
29
|
-
printNE = false;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const main = async () => {
|
|
33
|
-
|
|
34
|
-
for (let method of ["sax", "expat"]) {
|
|
35
|
-
console.log(`Running with ${method}...`);
|
|
36
|
-
printNE = true;
|
|
37
|
-
for (let i = 0; i <= 5; i++)
|
|
38
|
-
await parse(method);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
main();
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
/*
|
|
46
|
-
**********************
|
|
47
|
-
* Results
|
|
48
|
-
**********************
|
|
49
|
-
|
|
50
|
-
Running with sax...
|
|
51
|
-
25 NE in 00:21.260
|
|
52
|
-
50 NE in 00:46.36
|
|
53
|
-
75 NE in 01:08.314
|
|
54
|
-
100 NE in 01:36.5
|
|
55
|
-
125 NE in 02:11.869
|
|
56
|
-
150 NE in 02:49.940
|
|
57
|
-
175 NE in 03:15.653
|
|
58
|
-
Finished with sax in 03:37.656
|
|
59
|
-
Finished with sax in 03:41.884
|
|
60
|
-
Finished with sax in 03:45.146
|
|
61
|
-
Finished with sax in 03:42.84
|
|
62
|
-
Finished with sax in 03:57.614
|
|
63
|
-
Finished with sax in 03:42.634
|
|
64
|
-
|
|
65
|
-
Running with expat...
|
|
66
|
-
25 NE in 00:15.746
|
|
67
|
-
50 NE in 00:33.605
|
|
68
|
-
75 NE in 00:48.993
|
|
69
|
-
100 NE in 01:07.777
|
|
70
|
-
125 NE in 01:32.327
|
|
71
|
-
150 NE in 01:59.544
|
|
72
|
-
175 NE in 02:17.560
|
|
73
|
-
Finished with expat in 02:33.545
|
|
74
|
-
Finished with expat in 02:32.371
|
|
75
|
-
Finished with expat in 02:39.785
|
|
76
|
-
Finished with expat in 02:33.270
|
|
77
|
-
Finished with expat in 02:33.231
|
|
78
|
-
Finished with expat in 02:38.269
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
Good old Perl XML::Twig
|
|
82
|
-
25 NE in 1:14
|
|
83
|
-
50 NE in 2:03
|
|
84
|
-
75 NE in 3:43
|
|
85
|
-
100 NE in 5:02
|
|
86
|
-
125 NE in 6:12
|
|
87
|
-
150 NE in 7:16
|
|
88
|
-
175 NE in 8:17
|
|
89
|
-
200 NE in 9:24
|
|
90
|
-
Finished with perl in 9:24
|
|
91
|
-
|
|
92
|
-
*/
|
package/samples/xmlns.xml
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
<root>
|
|
2
|
-
<h:table xmlns:h="http://www.w3.org/TR/html4/" id="1">
|
|
3
|
-
<h:tr>
|
|
4
|
-
<h:td>Apples</h:td>
|
|
5
|
-
<h:td>Bananas</h:td>
|
|
6
|
-
</h:tr>
|
|
7
|
-
</h:table>
|
|
8
|
-
<f:table xmlns:f="https://www.w3schools.com/furniture">
|
|
9
|
-
<f:name>African Coffee Table</f:name>
|
|
10
|
-
<f:width>80</f:width>
|
|
11
|
-
<f:length>120</f:length>
|
|
12
|
-
</f:table>
|
|
13
|
-
<book xmlns:a="http://www.w3.org/" category="web">
|
|
14
|
-
<title lang="en">Learning XML</title>
|
|
15
|
-
<a:author>Erik T. Ray</a:author>
|
|
16
|
-
<year>2003</year>
|
|
17
|
-
<price>39.95</price>
|
|
18
|
-
</book>
|
|
19
|
-
</root>
|
package/test.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const twig = require('./twig.js');
|
|
4
|
-
const { pipeline } = require('stream/promises');
|
|
5
|
-
|
|
6
|
-
async function parse(file, handler, options) {
|
|
7
|
-
const parser = twig.createParser(handler, options);
|
|
8
|
-
const reader = fs.createReadStream(file);
|
|
9
|
-
console.log(`Start parsing of ${path.basename(file)} with ${options.method}`);
|
|
10
|
-
await pipeline(reader, parser);
|
|
11
|
-
console.log(`Parsing ${path.basename(file)} done`);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
//const parser = twig.createParser({ tag: twig.Root, function: anyHandler }, { method: 'sax' });
|
|
15
|
-
//fs.createReadStream(`${__dirname}/samples/bookstore.xml`).pipe(parser);
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//fs.createReadStream(`${__dirname}/samples/encoding-UTF-8.xml`).pipe(parser);
|
|
19
|
-
//fs.createReadStream(`${__dirname}/samples/encoding-UTF-16LE.xml`).pipe(parser);
|
|
20
|
-
//fs.createReadStream(`${__dirname}/samples/encoding-UTF-16BE.xml`).pipe(parser);
|
|
21
|
-
|
|
22
|
-
function anyHandler(elt) {
|
|
23
|
-
console.log(`<${elt.name}> => ${elt.text} -> ${JSON.stringify(elt.attributes)}`);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function nsHandler(elt) {
|
|
27
|
-
console.log(`${elt.name} => ${JSON.stringify(elt.namespace)} -> ${JSON.stringify(elt.attributes)}`);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function piHandler(elt) {
|
|
31
|
-
console.log(`${elt.name} -> ${JSON.stringify(elt.PI)}`);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const main = async () => {
|
|
36
|
-
|
|
37
|
-
for (let file of ["bookstore", "breakfast-menu"]) {
|
|
38
|
-
for (let method of ["sax", "expat"])
|
|
39
|
-
await parse(`${__dirname}/samples/${file}.xml`, { tag: twig.Any, function: anyHandler }, { method: method });
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
for (let method of ["sax", "expat"])
|
|
43
|
-
await parse(`${__dirname}/samples/xmlns.xml`, { tag: twig.Any, function: nsHandler }, { method: method, xmlns: true });
|
|
44
|
-
|
|
45
|
-
for (let method of ["sax", "expat"])
|
|
46
|
-
await parse(`${__dirname}/samples/processingInstruction.xml`, { tag: twig.Root, function: piHandler }, { method: method });
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
main();
|
|
51
|
-
|