# xml-splitter

> Provide an easy way to split or extract some nodes of very big XML files

Latest version **1.2.1** (published 2014-01-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install xml-splitter
pnpm add xml-splitter
yarn add xml-splitter
bun add xml-splitter
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2014-01-07 |
| First published | 2012-05-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.6.0 |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Nicolas Thouvenin |
| Maintainers | touv |
| Keywords | xml, splitter, extractor, reader |

## Links

- npm: https://www.npmjs.com/package/xml-splitter
- Repository: https://github.com/touv/node-xml-splitter
- Homepage: http://github.com/touv/node-xml-splitter
- Issues: https://github.com/touv/node-xml-splitter/issues
- npm.io page: https://npm.io/package/xml-splitter

## Dependencies (2)

- [sax](https://npm.io/package/sax.md) ~0.5.5
- [clone](https://npm.io/package/clone.md) ~0.1.11

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.2.1 (latest) — 2014-01-07
- 1.2.0 — 2013-10-16
- 1.1.7 — 2013-02-11
- 1.1.6 — 2012-09-17
- 1.1.5 — 2012-07-11
- 1.1.3 — 2012-06-27
- 1.1.2 — 2012-06-06
- 1.1.1 — 2012-06-05
- 1.0.1 — 2012-05-09
- 1.0.0 — 2012-05-02

## README

# XML Splitter for NodeJS

[![Build Status](https://secure.travis-ci.org/touv/node-xml-splitter.png?branch=master)](http://travis-ci.org/touv/node-xml-splitter)

It's native and full Javascript class, that provides an easy way to split **huge** XML data with one or more paths.

## Contributors

  * [Nicolas Thouvenin](https://github.com/touv) 
  * [Stéphane Gully](https://github.com/kerphi)
  * [Alison Rowland](https://github.com/arowla)

# Installation

With [npm](http://npmjs.org) do:

    $ npm install xml-splitter


# Examples

## Basic
```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('<root><item><id>1</id></item><item><id>2</id></item></root>')
```
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('<root><item><id>1</id></item><entry><id>2</id></entry></root>')
```
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](https://github.com/caolan/nodeunit) to run the tests.

    $ npm install nodeunit
    $ nodeunit test

# API Documentation

## Methods

### constructor XMLSplitter(cutter, options)
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.

### parseString(string, encoding)
Split XML within a string

### parseStream(stream)
Split XML within a stream


## Events

### data
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('<record><item><value>X</value></item><unit><value>Y</value></unit></record>')
````
Output:

````
{ value: { '$t': 'X' } }
item
/record/item
{ value: { '$t': 'Y' } }
unit
/record/unit
````

### close
Emit if the stream emit the close event OR if the stream is destroyed

### end
Emit on the end of the XML parsing

### error
Emit when something bad happened


## XPath's operators

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.

# Also

* https://github.com/jahewson/node-big-xml
* https://github.com/DamonOehlman/xmlslicer

# License

[MIT/X11](./LICENSE)


[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/touv/node-xml-splitter/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

---
_Source: https://npm.io/package/xml-splitter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
