An xml parser for node.js written in javascript.
(C) Rob Righter (@robrighter) 2009 - 2010, Licensed under the MIT-LICENSE Contributions from David Joham
node-xml is an xml parser for node.js written in javascript.
npm install node-xmlNode-xml provides a SAX2 parser interface that can take a string, file. The parser can take characters from the document in chunks. To send chunks of the document to the parser use 'parseString(xml)'
#SAX Parser#
##new xml.SaxParser()## * Instantiate a new SaxParser * returns: a SaxParser object
##new xml.SaxParser(callback)## * Instantiate a new SaxParser * returns: a SaxParser object * Arguments *callback - a function that accepts the new sax parser as an argument
#Parse#
##parser.parseString(string)##
Parse an in memory string
##parser.parseFile(filename)##
Parse a file
##parser.pause()## pauses parsing of the document
##parser.resume()## resumes parsing of the document
#Callbacks#
##parser.onStartDocument(function() {})##
Called at the start of a document
##parse.onEndDocument(function() {})##
Called at the end of the document parse
##parser.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {})##
Called on an open element tag
##parser.onEndElementNS(function(elem, prefix, uri) {})##
Called at the close of an element
##parser.onCharacters(function(chars) {})##
Called when a set of content characters is encountered
##parser.onCdata(function(cdata) {})##
Called when a CDATA is encountered
##parser.onComment(function(msg) {})##
Called when a comment is encountered
##parser.onWarning(function(msg) {})##
Called when a warning is encountered
##parser.onError(function(msg) {})##
Called when an error is encountered
var util = require('util');
var xml = require("./lib/node-xml");
var parser = new xml.SaxParser(function(cb) {
cb.onStartDocument(function() {
});
cb.onEndDocument(function() {
});
cb.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {
util.log("=> Started: " + elem + " uri="+uri +" (Attributes: " + JSON.stringify(attrs) + " )");
});
cb.onEndElementNS(function(elem, prefix, uri) {
util.log("'+chars+"");
});
cb.onCdata(function(cdata) {
util.log(''+cdata+"");
});
cb.onComment(function(msg) {
util.log(''+msg+"");
});
cb.onWarning(function(msg) {
util.log(''+msg+"");
});
cb.onError(function(msg) {
util.log(''+JSON.stringify(msg)+"");
});
});
//example read from chunks
parser.parseString("");
parser.parseString("");
parser.parseString("and lots");
parser.parseString("and lots of text&am");
parser.parseString("p;some more.");
parser.parseString("");
parser.parseString("");
//example read from file
parser.parseFile("sample.xml");No open issues yet, or sync has not completed.