Permalink
Browse files

you can now use .each() to loop through nodes with the same name

  • Loading branch information...
1 parent 0892a95 commit c370ba4e305c6cda291d07c6c7bc93c895f8891b @SamDecrock committed Feb 25, 2013
Showing with 65 additions and 8 deletions.
  1. +25 −3 README.md
  2. +1 −1 package.json
  3. +18 −2 test.js
  4. +21 −2 xmlreader.js
View
@@ -21,7 +21,7 @@ I also wanted a xml parser that can handle multiple nodes of the same name. Most
I'm using functions to get to nodes of the same name. The same functions can also be used to get to nodes where there's only one of them:
-``` nodes.count() ``` and ``` nodes.at(1) ```
+``` nodes.count() ```, ``` nodes.at(0) ``` and ``` nodes.each() ```
I'm using [sax js](https://github.com/isaacs/sax-js) created by [Isaac](https://github.com/isaacs) to do all the hard work of actually parsing the xml :-)
@@ -43,6 +43,12 @@ I'm using [sax js](https://github.com/isaacs/sax-js) created by [Isaac](https://
nodes.at(i)
+#### loop through the nodes of the same name ####
+
+ nodes.each(callback)
+
+with: callback = function(index, node)
+
#### get the parent node of a node ####
node.parent()
@@ -64,7 +70,7 @@ var someXml = '<response id="1" shop="aldi">'
+ '<game>Some great game</game>'
+ '<game>Some other great game</game>'
+ '</games>'
- + '<notes>These are some notes</notes>'
+ + '<note>These are some notes</note>'
+ '</response>'
xmlreader.read(someXml, function (err, res){
@@ -83,11 +89,27 @@ xmlreader.read(someXml, function (err, res){
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.notes.at(0).text() );
+ 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("");
View
@@ -1,7 +1,7 @@
{
"name": "xmlreader",
"description": "node library to read xml the easy way",
- "version": "0.2.1",
+ "version": "0.2.2",
"author": {
"name": "Sam Decrock",
"url": "https://github.com/SamDecrock/"
View
@@ -12,7 +12,7 @@ var someXml = '<response id="1" shop="aldi">'
+ '<game>Some great game</game>'
+ '<game>Some other great game</game>'
+ '</games>'
- + '<notes>These are some notes</notes>'
+ + '<note>These are some notes</note>'
+ '</response>'
xmlreader.read(someXml, function (err, res){
@@ -31,11 +31,27 @@ xmlreader.read(someXml, function (err, res){
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.notes.at(0).text() );
+ 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("");
View
@@ -48,9 +48,10 @@ exports.read = function(xmlstring, callback){
// add the parent() function so that we can use it later:
addParentFunction(newobject, object);
- // add the functions count() and at() to access the nodes as if they were multiple nodes of the same name:
+ // add the functions count(), at() and each() to access the nodes as if they were multiple nodes of the same name:
addCountFunction(newobject);
addAtFunction(newobject);
+ addEachFunction(newobject);
// check if a node with that name already exists
if(object[node.name]){
@@ -67,9 +68,10 @@ exports.read = function(xmlstring, callback){
object[node.name].array.push(newobject);
}
- // add 2 functions to work with that array:
+ // add 3 functions to work with that array:
addCountFunction(object[node.name]);
addAtFunction(object[node.name]);
+ addEachFunction(object[node.name]);
}else{
// add the functions count() and at() to access the nodes from the array:
object[node.name] = newobject;
@@ -120,6 +122,23 @@ exports.read = function(xmlstring, callback){
}
}
+ function addEachFunction(object){
+ if(object.array){
+ object.each = function(callback){
+ for(var i in object.array){
+ callback(i, object.array[i]);
+ }
+ return;
+ }
+ }else{
+ object.each = function(callback){
+ return callback(0, object);
+ }
+ }
+ }
+
+
+
function addParentFunction(object, parent){
object.parent = function(){
return parent;

0 comments on commit c370ba4

Please sign in to comment.