forked from SamDecrock/node-xmlreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
69 lines (50 loc) · 1.85 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var xmlreader = require('./xmlreader');
var someXml = '<response id="1" shop="aldi">'
+ 'This is some other content'
+ '<who name="james">James May</who>'
+ '<who name="sam">'
+ 'Sam Decrock'
+ '<location>Belgium</location>'
+ '</who>'
+ '<who name="jack">Jack Johnsen</who>'
+ '<games age="6">'
+ '<game>Some great game</game>'
+ '<game>Some other great game</game>'
+ '</games>'
+ '<title>'
+ '<![CDATA[Some text between CDATA tags]]>'
+ '</title>'
+ '<note>These are some notes</note>'
+ '</response>'
xmlreader.read(someXml, function (err, res){
if(err) return console.log(err);
// use .text() to get the content of a node:
console.log( res.response.text() );
// use .attributes() to get the attributes of a node:
console.log( res.response.attributes().shop );
console.log("");
// using the .count() and the .at() function, you can loop through nodes with the same name:
for(var i = 0; i < res.response.who.count(); i++){
console.log( res.response.who.at(i).text() );
}
console.log("");
// you can also use .each() to loop through the nodes of the same name:
res.response.who.each(function (i, who){
console.log( who.text() );
});
console.log("");
console.log( res.response.who.at(1).text() ) ;
console.log( res.response.who.at(1).location.text() );
// you can also use .at() to get to nodes where there's only one of them:
console.log( res.response.note.at(0).text() );
console.log("");
// or loop through them as if they were a series of nodes with the same name:
res.response.note.each(function (i, note){
console.log( note.text() );
});
console.log("");
console.log( res.response.title.text() );
console.log("");
// you can also get the parent of a node using .parent():
console.log( res.response.who.at(1).parent().attributes().id ) ;
});