Provide an easy way to split or extract some nodes of very big XML files
npm install xml-splitter
It's native and full Javascript class, that provides an easy way to split huge XML data with one or more paths.
* Nicolas Thouvenin
* Stéphane Gully
* Alison Rowland
With npm do:
$ npm install xml-splitter
javascript
var XMLSplitter = require('xml-splitter') xs = new XMLSplitter('/root/item')
xs.on('data', function(data) {
console.log(data)
})
xs.on('end', function(counter) {
console.log(counter+' slices !')
})
xs.parseString('1 2 ')
`
Output:
{ id: { '$t': '1' } }
{ id: { '$t': '2' } }
2 slices !Multi-paths
`javascript
var XMLSplitter = require('xml-splitter') xs = new XMLSplitter(['/root/item', '/root/entry'])
xs.on('data', function(data) {
console.log(data)
})
xs.on('end', function(counter) {
console.log(counter+' slices !')
})
xs.parseString('1 2 ')
`
Output:
{ id: { '$t': '1' } }
{ id: { '$t': '2' } }
2 slices !
Streaming
`javascript
var XMLSplitter = require('xml-splitter') xs = new XMLSplitter('/root/item')
xs.on('data', function(data) {
console.log(data)
})
xs.on('end', function(counter) {
console.log(counter+' slices !')
})
xs.parseStream(process.stdin) // or process.stdin.pipe(xs.stream)
`Tests
Use nodeunit to run the tests.
$ npm install nodeunit
$ nodeunit test
API Documentation
Methods
$3
Create an new splitter, cutter is a string or an array of strings that contains path.
Options are :* regular : To indicate if the cutter is applied to not nested XML parts. By default is true (to optimize the memory consumation)
* ignoreError : To NOT emit error event when an XML Error was met . By default is false.
$3
Split XML within a string$3
Split XML within a stream
Events
$3
Emits three elements on each slice: the data node (object), the node's tag name (string), and the node's path (string). For example:``
var xs = new XMLSplitter('//(item|unit)')
xs.on('data', function (node, tag, path) {
console.log(node);
console.log(tag);
console.log(path);
})
xs.parseString('X Y ')
``
Output:``
{ value: { '$t': 'X' } }
item
/record/item
{ value: { '$t': 'Y' } }
unit
/record/unit
```The XPath standard is not supported, only basic paths (included namespaces) and fews operotors is implemented :
* / : /record, /record/item
* // : //para, /root//item
\ : /root/\/item, /root/item/\
* | : /(record|item), /root/(item|unit)
I do not think I will implement more operators.
* https://github.com/jahewson/node-big-xml
* https://github.com/DamonOehlman/xmlslicer
