An event based XML parsing library using SAX under the hood for speed and efficiency.
The exml node module provides an intuitive event based XML parsing API which sits on top of a standard SAX parser, greatly simplifying the parsing code while retaining the raw speed and low memory overhead of the underlying SAX engine. The module takes care of the complex tasks of maintaining contexts between SAX event handlers allowing you to concentrate on dealing with the actual structure of the XML document.
npm install exml
The best way to illustrate how exml makes parsing very easy is to look at actual examples. Consider the following contrived sample document:
Here is a first way to parse it into an array of contact objects using exml:
var exml = ;var contacts = ;var parser = ;parser;parser;parser;contacts;
To reduce the amount and depth of event callbacks that you have to write, exml provides stacked events. We are now going to re-write the previous example using stacked events:
Finally, since using nodes text content to initialize object properties is a pretty frequent task, exml provides a shortcut to make it shorter to write. Let's revisit the previous example and use this shortcut:
parser.write(chunk): pass a chunk of xml data to the parser.
parser.end([chunk]): notify the parser that the xml data is complete, optionnaly passing a final chunk of data.
parser.on(event, [event, ...], callback): register a handler for the specified (optioannly stacked) event.
Any tag name: the events you are probably going to use the most take the form of the XML tag names you want to catch. Tag event callbacks receive an object containing the node attributes as a parameter.
$text: this custom event is sent when text content is encountered. The text data is passed as a parameter to the event handler callback.
$cdata: this custom event is sent when CDATA content is encountered. The CDATA content is passed as a parameter to the event handler callback.
$content: this custom event is sent when a node full text and/or CDATA content has been accumulated. The content is passed as a parameter to the event handler callback.