xmlreader.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Copyright (c) 2013 Sam Decrock <sam.decrock@gmail.com>
  3. MIT License
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. var sax = require("sax");
  21. exports.read = function(xmlstring, callback){
  22. var saxparser = sax.parser(true);
  23. var rootobject = {};
  24. var object = rootobject;
  25. saxparser.onerror = function (err) {
  26. // an error happened.
  27. return callback(err);
  28. };
  29. saxparser.onopentag = function (node) {
  30. // opened a tag. node has "name" and "attributes"
  31. // create a new object and fill it with a function that returns the attributes:
  32. var newobject = {
  33. attributes: function(name){
  34. return node.attributes;
  35. }
  36. };
  37. // add the parent() function so that we can use it later:
  38. addParentFunction(newobject, object);
  39. // add the functions count(), at() and each() to access the nodes as if they were multiple nodes of the same name:
  40. addCountFunction(newobject);
  41. addAtFunction(newobject);
  42. addEachFunction(newobject);
  43. // check if a node with that name already exists
  44. if(object[node.name]){
  45. // we're dealing with objects of the same name, let's wrap them in an array
  46. // check if there's already an array:
  47. if(!object[node.name].array){
  48. // no array, create one and at itself + newobject:
  49. var firstobject = object[node.name];
  50. object[node.name] = {};
  51. object[node.name].array = new Array(firstobject, newobject);
  52. }else{
  53. // alreay an array, just add the newobject to it:
  54. object[node.name].array.push(newobject);
  55. }
  56. // add 3 functions to work with that array:
  57. addCountFunction(object[node.name]);
  58. addAtFunction(object[node.name]);
  59. addEachFunction(object[node.name]);
  60. }else{
  61. // add the functions count() and at() to access the nodes from the array:
  62. object[node.name] = newobject;
  63. }
  64. // set the current object to the newobject:
  65. object = newobject;
  66. };
  67. saxparser.oncdata = function(cdata){
  68. // add the function text() to the object to return the cdata value:
  69. object.text = function(){
  70. return cdata;
  71. }
  72. };
  73. saxparser.ontext = function (text) {
  74. // add the function text() to the object to return the text value:
  75. !object.text ? object.text = function(){
  76. return text;
  77. } : null;
  78. };
  79. saxparser.onclosetag = function (node) {
  80. // set the object back to its parent:
  81. object = object.parent();
  82. }
  83. saxparser.onend = function () {
  84. return callback(null, rootobject);
  85. };
  86. // Functions that add functions like count(), at() and parent()
  87. // We need closures for this:
  88. function addCountFunction(object){
  89. if(object.array){
  90. object.count = function(){
  91. return object.array.length;
  92. }
  93. }else{
  94. object.count = function(){
  95. return 1;
  96. }
  97. }
  98. }
  99. function addAtFunction(object){
  100. if(object.array){
  101. object.at = function(index){
  102. return object.array[index];
  103. }
  104. }else{
  105. object.at = function(index){
  106. return object;
  107. }
  108. }
  109. }
  110. function addEachFunction(object){
  111. if(object.array){
  112. object.each = function(callback){
  113. for(var i in object.array){
  114. callback(i, object.array[i]);
  115. }
  116. return;
  117. }
  118. }else{
  119. object.each = function(callback){
  120. return callback(0, object);
  121. }
  122. }
  123. }
  124. function addParentFunction(object, parent){
  125. object.parent = function(){
  126. return parent;
  127. }
  128. }
  129. // pass the xml string to the awesome sax parser:
  130. saxparser.write(xmlstring).close();
  131. }