final commit for this PR

This commit is contained in:
Sai1919
2016-11-11 15:39:04 +05:30
parent 398e707aef
commit 51b7639f2a
3 changed files with 74 additions and 11 deletions

3
.npmignore Normal file
View File

@@ -0,0 +1,3 @@
# Dependency directories
node_modules
test

View File

@@ -3,7 +3,7 @@
[![Build Status](https://travis-ci.org/Sai1919/xml-streamer.svg?branch=master)](https://travis-ci.org/Sai1919/xml-streamer) [![Build Status](https://travis-ci.org/Sai1919/xml-streamer.svg?branch=master)](https://travis-ci.org/Sai1919/xml-streamer)
## Motivation ## Motivation
You use [Node.js](https://nodejs.org) for speed? You process XML streams? Then you want the fastest XML to JS parser: xml-streamer You use [Node.js](https://nodejs.org) for speed? You process XML streams? Then you want the fastest XML to JS parser: `xml-streamer`, based on [node-expat](https://github.com/astro/node-expat)
## Install ## Install
@@ -13,7 +13,45 @@ npm install xml-streamer
## Basic Usage ## Basic Usage
`xml-streamer can be used in three
ways`
```javascript ```javascript
// 1. By listening for interested nodes.
(function () {
"use strict";
var Parser = require('xml-streamer')
var opts = {} // see `Available Constructor Options` section below.
var parser = new Parser(opts)
parser.on('item', function (item) {
// consume the item object here
})
parser.on('end', function () {
// parsing ended no more data events will be raised
})
parser.on('error', function (error) {
// error occurred
// NOTE: when error event emitted no end event will be emitted
console.error(error)
})
xmlStream.pipe(parser) // pipe your input xmlStream to parser.
// readable
parser.on('readable', function () {
// if you don't want to consume "data" on "data" events you can wait for readable event and consume data by calling parser.read()
})
}())
// 2. By passing a resource path.
(function () { (function () {
"use strict"; "use strict";
@@ -32,7 +70,35 @@ npm install xml-streamer
parser.on('error', function (error) { parser.on('error', function (error) {
// error occurred // error occurred
// NOTE: when error emit emitted no end event will be emitted // NOTE: when error event emitted no end event will be emitted
console.error(error)
})
xmlStream.pipe(parser) // pipe your input xmlStream to parser.
// readable
parser.on('readable', function () {
// if you don't want to consume "data" on "data" events you can wait for readable event and consume data by calling parser.read()
})
}())
// 2. By passing the resourcePath as shown in 2 method and reading data by calling `read` method instead listening for data events.
(function () {
"use strict";
var Parser = require('xml-streamer')
var opts = {resourcePath: '/items/item'}
var parser = new Parser(opts)
parser.on('end', function () {
// parsing ended no more data events will be raised
})
parser.on('error', function (error) {
// error occurred
// NOTE: when error event emitted no end event will be emitted
console.error(error) console.error(error)
}) })
@@ -57,7 +123,7 @@ npm install xml-streamer
## Available Constructor Options ## Available Constructor Options
* `resourcePath`: `Type: String` Manditory field. Used to extract the XML nodes that you are interested in. * `resourcePath`: `Type: String` Optional field. Used to extract the XML nodes that you are interested in.
// Ex: let the XML be // Ex: let the XML be
```xml ```xml
@@ -122,9 +188,8 @@ npm install xml-streamer
## upcoming features ## upcoming features
1. `allowing to listen on interested nodes instead of passing resourcePath in options` 1. `handling of compressed streams`
2. `handling of compressed streams` 2. `handling of different encodings`
3. `handling of different encodings`
## Namespace handling ## Namespace handling

View File

@@ -14,7 +14,6 @@ function XmlParser (opts) {
this.opts = _.defaults(opts, defaults) this.opts = _.defaults(opts, defaults)
this.parserState = new ParserState() this.parserState = new ParserState()
this.parser = new expat.Parser('UTF-8') this.parser = new expat.Parser('UTF-8')
// var transformOpts = { readableObjectMode: true }
stream.Transform.call(this) stream.Transform.call(this)
this._readableState.objectMode = true this._readableState.objectMode = true
} }
@@ -79,10 +78,6 @@ XmlParser.prototype.parse = function (chunk) {
parser.on('error', function (err) { parser.on('error', function (err) {
processError(err) processError(err)
}) })
/* parser.on('end', function () {
scope.emit('end')
})*/
} }
function processError (err) { function processError (err) {