Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jcreigno/node-xmlstream-parser

simple xml stream parser based on sax
https://github.com/jcreigno/node-xmlstream-parser

Last synced: 6 days ago
JSON representation

simple xml stream parser based on sax

Awesome Lists containing this project

README

        

node-xmlstream-parser
=====================

simple xml stream parser based on sax.
Takes a readable xml stream as input and emits parsed object as result.

This module is inspired by [JSONStream.parse](https://github.com/dominictarr/JSONStream#jsonstreamparsepath).

### Installation

`npm install --save xmlstream-parser`

### Usage

```js

var streamParser = require('../lib/index'),
JSONStream = require('JSONStream'),
fs = require('fs');

fs.createReadStream('response.xml')
.pipe(streamParser.parse('Item')) // emit Items elements
.pipe(JSONStream.stringify()) // stringify objects
.pipe(process.stdout);
```

### Parsing rules

* node attributes are parsed and prefixed with `$`
```xml

```
is parsed as :
```js
{
message: {
$value : 'hello'
}
}
```

* if a node element as only one child text node then it is parsed as simple value
```xml
hello
```
is parsed as :
```js
{
message: 'hello'
}
```
* if a node as more than one child with the same name then it is parsed as an Array
```xml
hello
world
```
is parsed as :
```js
{
message: ['hello', 'world']
}
```

* if a node as more than one child with with a text node, then it is parsed as `_`
```xml
hello
```
is parsed as :
```js
{
message: {
_ : 'hello',
$priority : 'important'
}
}
```