XML Reader
Reads XML documents and emits JavaScript objects with a simple, easy to use structure.
Features
- Small, fast and simple
- Runs everywhere (browser, node.js, React Native, ServiceWorkers, WebWorkers...)
- Event driven and synchronous API
- Can process input piece-by-piece in a serial fashion
- Stream mode (low memory usage)
- Reads CDATA sections
Install
npm install --save xml-reader
Node structure
Objects emitted by the reader are trees where each node has the following structure:
Breaking changes in version 2
Added the tagPrefix
option with a default value of 'tag:'
. This way we avoid possible name collisions with the done
event.
To keep the old behavior, set it to an empty string.
Reading results
Check the xml-query
package! It is very useful to read values from the structures returned by xml-reader
.
Examples
Read document (event driven)
Basic example. Read and parse a XML document.
const XmlReader = ;const reader = XmlReader;const xml = `<?xml version="1.0" encoding="UTF-8"?> <message> <to>Alice</to> <from>Bob</from> <heading color="blue">Hello</heading> <body color="red">This is a demo!</body> </message>`; reader;reader; /*Console output: { name: 'message', type: 'element', children: [ { name: 'to', type: 'element', children: [{ type: 'text', value: 'Alice' }]}, { name: 'from', type: 'element', children: [{ type: 'text', value: 'Bob' }]}, { name: 'heading', type: 'element', attributes: { color: 'blue' }, children: [{ type: 'text', value: 'Hello' }]}, { name: 'body', type: 'element', attributes: { color: 'red' }, children: [{ type: 'text', value: 'This is a demo!' }]}]} Note: empty values and references to parent nodes removed for brevity!*/
Read document (synchronous)
This mode is only valid for reading complete documents (root node must be closed).
const XmlReader = ; const xml = '<doc>Hello!</doc>';const result = XmlReader;
Stream mode
In stream mode, nodes are removed from root as they are emitted. This way memory usage does not increases.
const XmlReader = ; const reader = XmlReader;const xml = `<root> <item v=1/> <item v=2/> <item v=3/> </root>`; reader;// {name: 'item', type: 'element', value: '', attributes: {v: '1'}, children: []}// {name: 'item', type: 'element', value: '', attributes: {v: '2'}, children: []}// {name: 'item', type: 'element', value: '', attributes: {v: '3'}, children: []} reader;// 0 reader;
You can also listen to all tags:
reader;
Stream mode (chunked)
In this example we are calling multiple times to the parser. This is useful if your XML document is a stream that comes from a TCP socket or WebSocket (for example XMPP streams).
Simply feed the parser with the data as it arrives. As you can see, the result is exactly the same as the previous one.
const XmlReader = ; const reader = XmlReader;const xml = `<root> <item v=1/> <item v=2/> <item v=3/> </root>`; reader;// {name: 'item', type: 'element', value: '', attributes: {v: '1'}, children: []}// {name: 'item', type: 'element', value: '', attributes: {v: '2'}, children: []}// {name: 'item', type: 'element', value: '', attributes: {v: '3'}, children: []} reader;// 0 // Note that we are calling the parse function providing just one char each timexml;
Reset
Use the reset()
method to reset the reader. This is useful if a stream gets interrupted and you want to start a new one or to use the same reader instance to parse multiple documents (just reset the reader between them).
Example:
const doc1 = '<document>...</document>';const doc2 = '<document>...</document>'; reader; // when the document ends, the reader stops emitting eventsreader; // now you can parse a new documentreader;
Options
Default options are:
stream: false parentNodes: true tagPrefix: 'tag:' doneEvent: 'done' emitTopLevelOnly: false
parentNodes (boolean)
If true
(default), each node of the AST has a parent
node which point to its parent. If false
the parent node is always null
.
stream (boolean)
Enable or disable stream mode. In stream mode nodes are removed from root after being emitted. Default false
.
Ignored in parseSync
;
doneEvent (string)
Default value is 'done'
. This is the name of the event emitted when the root node is closed and the parse is done.
Ignored in parseSync
;
tagPrefix (string)
Default value is 'tag:'
. The event driven API emits an event each time a tag is read. Use this option to set a name prefix.
Ignored in parseSync
;
emitTopLevelOnly (boolean)
Default value is false
. When true, tag events are only emitted by top level nodes (direct children from root). This is useful for XMPP streams like XMPP where each top level child is a stanza.
For example, given the following XML stream:
hello 2016-10-06 bye 2016-10-07
tags emitted with emitTopLevelOnly=false
bodydatemessagebodydatemessage
tags emitted with emitTopLevelOnly=true
messagemessage
License
MIT