电速宝智配引擎
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

examples.js 2.7KB

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