Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "xmlreader", | ||
"description": "node library to read xml the easy way", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Sam Decrock", |
@@ -24,5 +24,28 @@ node-xmlreader | ||
``` nodes.getLength() ``` and ``` nodes.at(1) ``` | ||
``` nodes.count() ``` and ``` nodes.at(1) ``` | ||
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 :-) | ||
## Functions ## | ||
#### get the attributes of a node #### | ||
node.attributes() | ||
#### get the text of a node #### | ||
node.text() | ||
#### get the number of nodes with the same name #### | ||
nodes.count() | ||
#### get node i of a series of nodes with the same name #### | ||
nodes.at(i) | ||
#### get the parent node of a node #### | ||
node.parent() | ||
## Example ## | ||
@@ -49,2 +72,4 @@ | ||
xmlreader.read(someXml, function (err, res){ | ||
if(err) return console.log(err); | ||
// use .text() to get the content of a node: | ||
@@ -58,4 +83,4 @@ console.log( res.response.text() ); | ||
// using the getLength() and the at() function, you can loop through nodes with the same name: | ||
for(var i = 0; i < res.response.who.getLength(); i++){ | ||
// 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() ); | ||
@@ -67,5 +92,10 @@ } | ||
// you can also get regular nodes like you get arrays: | ||
// 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(""); | ||
// you can also get the parent of a node using .parent(): | ||
console.log( res.response.who.at(1).parent().attributes().id ) ; | ||
}); | ||
``` |
14
test.js
@@ -19,2 +19,4 @@ var xmlreader = require('./xmlreader'); | ||
xmlreader.read(someXml, function (err, res){ | ||
if(err) return console.log(err); | ||
// use .text() to get the content of a node: | ||
@@ -28,4 +30,4 @@ console.log( res.response.text() ); | ||
// using the getLength() and the at() function, you can loop through nodes with the same name: | ||
for(var i = 0; i < res.response.who.getLength(); i++){ | ||
// 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() ); | ||
@@ -37,5 +39,11 @@ } | ||
// you can also get regular nodes like you get arrays: | ||
// 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(""); | ||
// you can also get the parent of a node using .parent(): | ||
console.log( res.response.who.at(1).parent().attributes().id ) ; | ||
}); | ||
@@ -1,6 +0,27 @@ | ||
var sax = require("sax"); | ||
/* | ||
Copyright (c) 2013 Sam Decrock <sam.decrock@gmail.com> | ||
MIT License | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
var sax = require("sax"); | ||
exports.read = function(xmlstring, callback){ | ||
@@ -13,3 +34,3 @@ var saxparser = sax.parser(true); | ||
// an error happened. | ||
return callback(err); | ||
@@ -28,8 +49,10 @@ }; | ||
addGetParentFunction(newobject, object); | ||
// add the parent() function so that we can use it later: | ||
addParentFunction(newobject, object); | ||
// add 2 functions to the object so that the object can be accessed as if it was an array: | ||
addGetLengthFunction(newobject); | ||
// add the functions count() and at() to access the nodes as if there were multiple nodes of the same name: | ||
addCountFunction(newobject); | ||
addAtFunction(newobject); | ||
// check if a node with that name already exists | ||
if(object[node.name]){ | ||
@@ -50,6 +73,6 @@ // we're dealing with objects of the same name, let's wrap them in an array | ||
// add 2 functions to work with that array: | ||
addGetLengthFunction(object[node.name]); | ||
addCountFunction(object[node.name]); | ||
addAtFunction(object[node.name]); | ||
}else{ | ||
// we're dealing with simple objects, no array: | ||
// add the functions count() and at() to access the nodes from the array: | ||
object[node.name] = newobject; | ||
@@ -62,2 +85,3 @@ } | ||
saxparser.ontext = function (text) { | ||
// add the function text() to the object to return the text value: | ||
object.text = function(){ | ||
@@ -69,3 +93,4 @@ return text; | ||
saxparser.onclosetag = function (node) { | ||
object = object.getParent(); | ||
// set the object back to its parent: | ||
object = object.parent(); | ||
} | ||
@@ -77,10 +102,11 @@ | ||
// Functions that add functions like count(), at() and parent() | ||
// We need closures for this: | ||
function addGetLengthFunction(object){ | ||
function addCountFunction(object){ | ||
if(object.array){ | ||
object.getLength = function(){ | ||
object.count = function(){ | ||
return object.array.length; | ||
} | ||
}else{ | ||
object.getLength = function(){ | ||
object.count = function(){ | ||
return 1; | ||
@@ -103,4 +129,4 @@ } | ||
function addGetParentFunction(object, parent){ | ||
object.getParent = function(){ | ||
function addParentFunction(object, parent){ | ||
object.parent = function(){ | ||
return parent; | ||
@@ -110,4 +136,4 @@ } | ||
// give the xml string to the awesome sax parser: | ||
saxparser.write(xmlstring).close(); | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8604
142
97