forked from Wernfried/xml-twig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.js
96 lines (73 loc) · 2.87 KB
/
sample.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const fs = require('fs');
const twig = require('../twig.js');
// Sample 1.1
function rootHandler(elt) {
console.log(`<${elt.name}> finished after ${parser.currentLine} lines`);
}
parser = twig.createParser({ tag: twig.Root, function: rootHandler }, { method: 'sax' })
fs.createReadStream(`${__dirname}/bookstore.xml`).pipe(parser)
// Sample 1.2
parser = twig.createParser({ tag: twig.Root, function: rootHandler }, { method: 'expat' })
/parser.write('<html><head><title>Hello World</title></head><body><p>Foobar</p></body></html>');
// Sample 2
parser = twig.createParser({ tag: twig.Root, event: 'rootElement' }, { method: 'sax' })
fs.createReadStream(`${__dirname}/bookstore.xml`).pipe(parser)
parser.on('rootElement', (elt) => {
console.log(`<${elt.name}> finished after ${parser.currentLine} lines`);
})
// Sample 3
function bookHandler(elt) {
console.log(`${elt.attr("category")} ${elt.name} at line ${parser.currentLine}`)
elt.purge() // -> without `purge()` the entire XML document will be loaded into memory
}
handle_book = [
{ tag: 'book', function: bookHandler },
{ tag: 'ebook', function: bookHandler }
];
handle_book = [
{ tag: /book$/, function: bookHandler }
];
handle_book = [{
tag: function (name, elt) { return name.endsWith('book') },
function: bookHandler
}];
handle_book = [{
tag: function (name, elt) { return ['book', 'ebook'].includes(name) },
function: bookHandler
}];
handle_book = [{
tag: function (name, elt) { return ['book', 'ebook'].includes(elt.name) },
function: bookHandler
}];
parser = twig.createParser(handle_book, { method: 'sax' })
fs.createReadStream(`${__dirname}/bookstore.xml`).pipe(parser)
// Sample 4
function anyHandler(elt) {
console.log(`${' '.repeat(elt.level)}${elt.name} => "${elt.text ?? ''}" at line ${parser.currentLine}`)
elt.purge() // -> without `purge()` the entire XML document will be loaded into memory
}
parser = twig.createParser({ tag: twig.Any, function: anyHandler })
// or with Regular Expression: `{ tag: tag: /i/, function: anyHandler }`
// or with Function: `{ tag: () => {return true}, function: anyHandler }`
fs.createReadStream(`${__dirname}/bookstore.xml`).pipe(parser)
// Sample 5
const handle_ebook = [
{ tag: 'ebook', function: ebookHandler },
{ tag: twig.Root, function: rootHandler }
];
const parser = twig.createParser(handle_ebook, { partial: true })
fs.createReadStream(`${__dirname}/bookstore.xml`).pipe(parser);
function ebookHandler(elt) {
//console.log(`${elt.name} at line ${parser.currentLine}`)
}
function rootHandler(elt) {
console.log( elt.writer(' ').toString() );
}
function bookHandler(elt) {
if (elt.attr('category') == 'fantasy') {
console.log(elt.writer(true).toString());
let t = elt.addElement('newTag', 'some Text > more', {id:1},2);
console.log(t.writer(true).toString());
console.log(elt.root().writer(true).toString());
}
}