source: node_modules/xml/examples/examples.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1var xml = require('../lib/xml');
2
3console.log('===== Example 1 ====');
4var example1 = { url: 'http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' };
5console.log(xml(example1));
6//<url>http://www.google.com/search?aq=f&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=opower</url>
7
8console.log('\n===== Example 2 ====');
9var example2 = [ { url: { _attr: { hostname: 'www.google.com', path: '/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' } } } ];
10console.log(xml(example2));
11//<url hostname="www.google.com" path="/search?aq=f&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=opower"/>
12
13console.log('\n===== Example 3 ====');
14var example3 = [ { toys: [ { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ];
15console.log(xml(example3));
16//<toys><toy>Transformers</toy><toy>GI Joe</toy><toy>He-man</toy></toys>
17console.log(xml(example3, { indent: true }));
18/*
19<toys>
20 <toy>Transformers</toy>
21 <toy>GI Joe</toy>
22 <toy>He-man</toy>
23</toys>
24*/
25
26console.log('\n===== Example 4 ====');
27var example4 = [ { toys: [ { _attr: { decade: '80s', locale: 'US'} }, { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ];
28console.log(xml(example4, { indent: true }));
29/*
30<toys decade="80s" locale="US">
31 <toy>Transformers</toy>
32 <toy>GI Joe</toy>
33 <toy>He-man</toy>
34</toys>
35*/
36
37console.log('\n===== Example 5 ====');
38var example5 = [ { toys: [ { _attr: { decade: '80s', locale: 'US'} }, { toy: 'Transformers' } , { toy: [ { _attr: { knowing: 'half the battle' } }, 'GI Joe'] }, { toy: [ { name: 'He-man' }, { description: { _cdata: '<strong>Master of the Universe!</strong>'} } ] } ] } ];
39console.log(xml(example5, { indent: true, declaration: true }));
40/*
41<toys><toy>Transformers</toy><toy>GI Joe</toy><toy>He-man</toy></toys>
42<toys>
43 <toy>Transformers</toy>
44 <toy>GI Joe</toy>
45 <toy>He-man</toy>
46</toys>
47<toys decade="80s" locale="US">
48 <toy>Transformers</toy>
49 <toy>GI Joe</toy>
50 <toy>He-man</toy>
51</toys>
52<toys decade="80s" locale="US">
53 <toy>Transformers</toy>
54 <toy knowing="half the battle">
55 GI Joe
56 </toy>
57 <toy>
58 <name>He-man</name>
59 <description><![CDATA[<strong>Master of the Universe!</strong>]]></description>
60 </toy>
61</toys>
62*/
63
64console.log('\n===== Example 6 ====');
65var elem = xml.Element({ _attr: { decade: '80s', locale: 'US'} });
66var xmlStream = xml({ toys: elem }, { indent: true } );
67xmlStream.on('data', function (chunk) {console.log("data:", chunk)});
68elem.push({ toy: 'Transformers' });
69elem.push({ toy: 'GI Joe' });
70elem.push({ toy: [{name:'He-man'}] });
71elem.close();
72
73/*
74data: <toys decade="80s" locale="US">
75data: <toy>Transformers</toy>
76data: <toy>GI Joe</toy>
77data: <toy>
78 <name>He-man</name>
79 </toy>
80data: </toys>
81*/
Note: See TracBrowser for help on using the repository browser.