xml2js-parser
Advanced tools
Comparing version 1.0.1 to 1.1.0
106
index.js
@@ -1,2 +0,2 @@ | ||
'use strict' | ||
'use strict'; | ||
const sax = require('sax'); | ||
@@ -10,4 +10,4 @@ const events = require('events'); | ||
normalizeTags: false, | ||
attrkey: "$", | ||
charkey: "_", | ||
attrkey: '$', | ||
charkey: '_', | ||
explicitArray: true, | ||
@@ -24,3 +24,2 @@ ignoreAttrs: false, | ||
includeWhiteChars: false, | ||
async: false, | ||
strict: true, | ||
@@ -46,3 +45,3 @@ attrNameProcessors: null, | ||
class ValidationError extends Error {}; | ||
class ValidationError extends Error {} | ||
@@ -55,2 +54,7 @@ module.exports = class Parser extends events.EventEmitter { | ||
static parseStringSync(str, options = {}) { | ||
const parser = new Parser(options); | ||
return parser.parseStringSync(str); | ||
} | ||
static parseString(str, a, b) { | ||
@@ -79,14 +83,11 @@ let cb, options = {}; | ||
processAsync() { | ||
processAsync(remaining) { | ||
try { | ||
if (this.remaining.length <= this.chunkSize) { | ||
const chunk = this.remaining; | ||
this.remaining = ''; | ||
this.saxParser = this.saxParser.write(chunk); | ||
this.saxParser.close(); | ||
if (remaining.length <= this.chunkSize) { | ||
this.saxParser.write(remaining).close(); | ||
} else { | ||
const chunk = this.remaining.substr(0, this.chunkSize); | ||
this.remaining = this.remaining.substr(this.chunkSize, this.remaining.length); | ||
this.saxParser = this.saxParser.write(chunk); | ||
setImmediate(() => this.processAsync()); | ||
const chunk = remaining.substr(0, this.chunkSize); | ||
remaining = remaining.substr(this.chunkSize, remaining.length); | ||
this.saxParser.write(chunk); | ||
setImmediate(() => this.processAsync(remaining)); | ||
} | ||
@@ -104,2 +105,3 @@ } catch (err) { | ||
this.stack = []; | ||
this.result = null; | ||
this.saxParser = sax.parser(this.strict, { | ||
@@ -120,31 +122,37 @@ trim: false, | ||
parseStringSync(str) { | ||
let result, error; | ||
this.on('end', (res) => result = res); | ||
this.on('error', (err) => error = err); | ||
try { | ||
str = stripBOM(str.toString()).trim(); | ||
if (!str) return null; | ||
this.saxParser.write(str).close(); | ||
} finally { | ||
this.reset(); | ||
} | ||
if (error) throw error; | ||
return result; | ||
} | ||
parseString(str, cb) { | ||
if (typeof cb === 'function') { | ||
const promise = new Promise((reolve, reject) => { | ||
this.on('end', (result) => { | ||
this.reset(); | ||
cb(null, result); | ||
reolve(result); | ||
}); | ||
this.on('error', (err) => { | ||
this.reset(); | ||
cb(err); | ||
reject(err); | ||
}); | ||
} | ||
try { | ||
str = str.toString(); | ||
if (str.trim() === '') return this.emit('end', null); | ||
str = stripBOM(str); | ||
if (this.async) { | ||
this.remaining = str; | ||
setImmediate(() => this.processAsync()); | ||
return this.saxParser; | ||
} | ||
this.saxParser.write(str).close(); | ||
} catch (err) { | ||
if (!(this.saxParser.errThrown || this.saxParser.ended)) { | ||
try { | ||
str = stripBOM(str.toString()).trim(); | ||
if (!str) return this.emit('end', null); | ||
setImmediate(() => this.processAsync(str)); | ||
} catch (err) { | ||
this.emit('error', err); | ||
this.saxParser.errThrown = true; | ||
} else if (this.saxParser.ended) { | ||
throw err; | ||
} | ||
} | ||
}); | ||
if (typeof cb != 'function') return promise; | ||
promise.then((res) => cb(null, res), (err) => cb(err)); | ||
} | ||
@@ -252,4 +260,3 @@ | ||
} | ||
this.saxParser.ended = true; | ||
this.emit('end', obj); | ||
this.result = obj; | ||
} | ||
@@ -268,3 +275,3 @@ } | ||
if (this.normalize) { | ||
charChild[this.charkey] = charChild[this.charkey].replace(/\s{2,}/g, " ").trim(); | ||
charChild[this.charkey] = charChild[this.charkey].replace(/\s{2,}/g, ' ').trim(); | ||
} | ||
@@ -283,6 +290,5 @@ s[this.childkey].push(charChild); | ||
_onEnd() { | ||
if (!this.saxParser.ended) { | ||
this.saxParser.ended = true; | ||
this.emit('end', null); | ||
} | ||
if (this.saxParser.ended) return; | ||
this.saxParser.ended = true; | ||
this.emit('end', this.result); | ||
} | ||
@@ -292,9 +298,8 @@ | ||
this.saxParser.resume(); | ||
if (!this.saxParser.errThrown) { | ||
this.saxParser.errThrown = true; | ||
this.emit('error', err); | ||
} | ||
if (this.saxParser.errThrown) return; | ||
this.saxParser.errThrown = true; | ||
this.emit('error', err); | ||
} | ||
} | ||
}; | ||
@@ -312,6 +317,3 @@ function assignOrPush(obj, key, value, explicit) { | ||
if (!processors) return value; | ||
for (let i = 0, len = processors.length; i < len; i++) { | ||
value = processors[i](value); | ||
} | ||
return value; | ||
return processors.reduce((v, func) => func(v), value); | ||
} | ||
@@ -323,2 +325,2 @@ | ||
module.exports.Parser = function(opts) { return new module.exports(opts); } | ||
module.exports.Parser = function(opts) { return new module.exports(opts); }; |
{ | ||
"name": "xml2js-parser", | ||
"description": "Simple XML to JavaScript object converter.", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"author": "Lauris Vavere <lauris@ma-1.lv>", | ||
@@ -59,5 +59,3 @@ "contributors": [ | ||
"scripts": { | ||
"test": "zap", | ||
"coverage": "nyc npm test && nyc report", | ||
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls" | ||
"test": "tape test/*.js | tap-summary" | ||
}, | ||
@@ -72,11 +70,8 @@ "repository": { | ||
"devDependencies": { | ||
"coffee-script": ">=1.10.0", | ||
"coveralls": "^2.11.2", | ||
"diff": ">=1.0.8", | ||
"nyc": ">=2.2.1", | ||
"zap": ">=0.2.9" | ||
"tap-summary": "^3.0.1", | ||
"tape": "^4.4.0" | ||
}, | ||
"engines" : { | ||
"node" : ">=6.0.0" | ||
} | ||
"engines": { | ||
"node": ">=6.0.0" | ||
} | ||
} |
xml2js-parser | ||
============= | ||
Simple XML to JavaScript object converter that uses a [sax-js](https://github.com/isaacs/sax-js/) for parsing. | ||
Support async as native promise or callback and sync mode operation. | ||
Description | ||
----------- | ||
This is a fork from a wonderful [xml2js](https://www.npmjs.com/package/xml2js) module. | ||
It aims to keep backwards compatibility with `xml2js` version 0.4.17 and | ||
target `nodejs` v6.x and up. `xml2js-parser` does not support XML building. | ||
target `nodejs` v6.x but without XML building support. | ||
@@ -15,7 +21,2 @@ Motivation behind fork was: | ||
Description | ||
----------- | ||
Simple XML to JavaScript object converter and uses [sax-js](https://github.com/isaacs/sax-js/) for parsing. | ||
Installation | ||
@@ -48,2 +49,15 @@ ----------- | ||
New promises usage | ||
------------------ | ||
```javascript | ||
const parseString = require('xml2js-parser').parseString; | ||
const xml = '<root>Hello xml2js-parser!</root>'; | ||
parseString(xml) | ||
.then(res) => console.log(res)) | ||
.catch(err) => console.error(err)); | ||
}); | ||
``` | ||
Standard usage | ||
@@ -64,2 +78,20 @@ -------------- | ||
Sync mode | ||
--------- | ||
```javascript | ||
const fs = require('fs'); | ||
const parseStringSync = require('xml2js-parser').parseStringSync; | ||
var parser = new Parser({trim: true}); | ||
fs.readFile(__dirname + '/foo.xml', (err, xml) => { | ||
try { | ||
const res = parser.parseStringSync(xml); | ||
console.log(res); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}); | ||
``` | ||
Processing attribute, tag names and values | ||
@@ -157,4 +189,2 @@ ------------------------------------------ | ||
text nodes should be included. | ||
* `async` (default `false`): Should the callbacks be async? This *might* be | ||
an incompatible change if your code depends on sync execution of callbacks. | ||
* `strict` (default `true`): Set sax-js to strict or non-strict parsing mode. | ||
@@ -161,0 +191,0 @@ Defaults to `true` which is *highly* recommended, since parsing HTML which |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22188
2
226