XmlStream is a Node.js XML stream parser and editor, based on node-expat (libexpat SAX-like parser binding).
$ npm install xml-stream
This is why we've rolled our own stream parser that tries to address these shortcomings. It processes an XML stream chunk by chunk and fires events only for nodes of interest, matching them with CSS-like selectors.
dataon outgoing data chunk,
endwhen parsing has ended,
startElement[: selector]on opening tag for selector match,
updateElement[: selector]on finished node for selector match with its contents buffered,
endElement[: selector]on closing tag for selector match,
text[: selector]on tag text for selector match.
When adding listeners for
callback can modify the provided node, before it is sent to the consumer.
Selector syntax is CSS-like and currently supports:
parent > child
Take a look at the examples for more information.
Each of the four node events has a callback with one argument. When parsing, this argument is set to the current matched node. Having a chunk of XML like this:
Item TitleDescription of this item.(text)
The structure of the item element node would be:
title: 'Item Title'description: 'Description of this item.''$':'id': '123''type': 'common''$name': 'item''$text': '(text)'
Naturally, element text and child elements wouldn't be known until discovered in the stream, so the structure may differ across events. The complete structure as displayed should be available on updateElement. The $name is not available on endElement.
It is sometimes required to select elements that have many children with one and the same name. Like this XML:
By default, parsed element node contains children as properties. In the case of several children with same names, the last one would overwrite others. To collect all of subitem elements in an array use collect:
By default, element text is returned as one concatenated string. In this XML:
one 1two 2
The value of $text for item would be:
one 1 two 2 without any
indication of the order of element a, element b, and text parts.
To preserve this order:
If you want parsing to pause (for example, until some asynchronous operation of yours is finished), you can pause and resume XML parsing:
Beware that resume() must not be called from within a handler callback.