node library to read xml the easy way
An xml reader for node that uses a different approach than all other xml readers/parsers out there.
You can install xmlreader using the Node Package Manager (npm):
npm install xmlreader
I wanted a xml reader that's easy to use and that doesn't require tricks to access the attributes and text values of xml nodes. Most xml parsers out there use some kind of prefix, but that's not a sound solution.
I'm using functions to differentiate the attributes and text from the nodes:
I also wanted a xml parser that can handle multiple nodes of the same name. Most parsers out there just ignore those or threat every node as an array.
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:
note: if CDATA is found, CDATA is returned as text()
with: callback = function(index, node)
var xmlreader = require'xmlreader';var someXml = '<response id="1" shop="aldi">'+ 'This is some other content'+ '<who name="james">James May</who>'+ '<who name="sam">'+ 'Sam Decrock'+ '<location>Belgium</location>'+ '</who>'+ '<who name="jack">Jack Johnsen</who>'+ '<games age="6">'+ '<game>Some great game</game>'+ '<game>Some other great game</game>'+ '</games>'+ '<note>These are some notes</note>'+ '</response>'xmlreaderreadsomeXmliferr return console.logerr;// use .text() to get the content of a node:console.log resresponsetext ;// use .attributes() to get the attributes of a node:console.log resresponseattributesshop ;console.log"";// using the .count() and the .at() function, you can loop through nodes with the same name:forvar i = 0; i < resresponsewhocount; i++console.log resresponsewhoatitext ;console.log"";// you can also use .each() to loop through the nodes of the same name:resresponsewhoeachconsole.log whotext ;;console.log"";console.log resresponsewhoat1text ;console.log resresponsewhoat1locationtext ;// you can also use .at() to get to nodes where there's only one of them:console.log resresponsenoteat0text ;console.log"";// or loop through them as if they were a series of nodes with the same name:resresponsenoteeachconsole.log notetext ;;console.log"";// you can also get the parent of a node using .parent():console.log resresponsewhoat1parentattributesid ;;