.editorconfig 0000664 0000000 0000000 00000000362 15160706752 0013545 0 ustar 00root root 0000000 0000000 root = true
; Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
; JS
[*.js]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true .gitignore 0000664 0000000 0000000 00000000036 15160706752 0013056 0 ustar 00root root 0000000 0000000 node_modules
*.swp
*.DS_Store
.npmignore 0000664 0000000 0000000 00000000015 15160706752 0013062 0 ustar 00root root 0000000 0000000 node_modules
.travis.yml 0000664 0000000 0000000 00000000610 15160706752 0013175 0 ustar 00root root 0000000 0000000 sudo: required
language: node_js
env:
- CXX="g++-4.8"
node_js:
- "0.10"
- "0.12"
- "iojs"
- "iojs-v2"
- "iojs-v1"
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
- gcc-4.8
before_install:
# npm shipped with Node.js 0.8 doesn't support carret so let's update it
- if [ "$TRAVIS_NODE_VERSION" == "0.8" ]; then npm install -g npm; fi
AUTHORS 0000664 0000000 0000000 00000001332 15160706752 0012136 0 ustar 00root root 0000000 0000000 Arek W arek01@gmail.com
Camilo Aguilar camilo.aguilar@gmail.com
Craig Condon craig@spiceapps.com
Daniel Bretoi daniel@bretoi.com
Daniel Juhl danieljuhl@gmail.com
Dmitry Fink github@finik.net
Garvit Sharma garvits45@gmail.com
Julian Duque julianduquej@gmail.com
Karl Böhlmark karl.bohlmark@edgeware.tv
Kevin McTigue firefoxman1@gmail.com
Kirill Vergun github.com@o-nix.me
Maher Beg maherbeg@gmail.com
Nicholas Kinsey pyrotechnick@feistystudios.com
Rob Brackett rob@robbrackett.com
Subbu Allamaraju subbu@ebaysf.com
The Gitter Badger badger@gitter.im
Trotter Cashion cashion@gmail.com
Yan idy0013@gmail.com
Ziggy Jonsson ziggy.jonsson.nyc@gmail.com
andres suarez zertosh@gmail.com
andris9 andris@node.ee
fengmk2 fengmk2@gmail.com
README.md 0000664 0000000 0000000 00000006572 15160706752 0012360 0 ustar 00root root 0000000 0000000 # Simple XML2JSON Parser
[](https://gitter.im/buglabs/node-xml2json?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[](https://travis-ci.org/buglabs/node-xml2json)
It does not parse the following elements:
* CDATA sections (*)
* Processing instructions
* XML declarations
* Entity declarations
* Comments
This module uses node-expat which will require extra steps if you want to get it installed on Windows. Please
refer to its [documentation](http://node-xmpp.org/doc/expat.html#installing-on-windows?).
## Installation
```
$ npm install xml2json
```
## Usage
```javascript
var parser = require('xml2json');
var xml = "bar";
var json = parser.toJson(xml); //returns a string containing the JSON structure by default
console.log(json);
```
## API
```javascript
parser.toJson(xml, options);
```
```javascript
parser.toXml(json);
```
### Options object for `toJson`
Default values:
```javascript
var options = {
object: false,
reversible: false,
coerce: false,
sanitize: true,
trim: true,
arrayNotation: false
};
```
* **object:** Returns a Javascript object instead of a JSON string
* **reversible:** Makes the JSON reversible to XML (*)
* **coerce:** Makes type coercion. i.e.: numbers and booleans present in attributes and element values are converted from string to its correspondent data types. Coerce can be optionally defined as an object with specific methods of coercion based on attribute name or tag name, with fallback to default coercion.
* **trim:** Removes leading and trailing whitespaces as well as line terminators in element values.
* **arrayNotation:** XML child nodes are always treated as arrays
* **sanitize:** Sanitizes the following characters present in element values:
```javascript
var chars = {
'<': '<',
'>': '>',
'(': '(',
')': ')',
'#': '#',
'&': '&',
'"': '"',
"'": '''
};
```
### Options object for `toXml`
Default values:
```javascript
var options = {
sanitize: false
};
```
`sanitize: false` is the default option to behave like previous versions
(*) xml2json tranforms CDATA content to JSON, but it doesn't generate a reversible structure.
## License
(The MIT License)
Copyright 2015 BugLabs. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
bin/ 0000775 0000000 0000000 00000000000 15160706752 0011637 5 ustar 00root root 0000000 0000000 bin/xml2json 0000664 0000000 0000000 00000000652 15160706752 0013341 0 ustar 00root root 0000000 0000000 #!/usr/bin/env node
var xml2json = require('../');
var pkg = require('../package.json');
var xml = '';
var args = process.argv.slice(2)
var arg = args[0]
if (arg == '--version') {
console.log(pkg.version)
process.exit(0)
}
process.stdin.on('data', function (data) {
xml += data;
});
process.stdin.resume();
process.stdin.on('end', function () {
json = xml2json.toJson(xml)
process.stdout.write(json + '\n')
});
index.js 0000664 0000000 0000000 00000000043 15160706752 0012531 0 ustar 00root root 0000000 0000000 module.exports = require('./lib');
lib/ 0000775 0000000 0000000 00000000000 15160706752 0011635 5 ustar 00root root 0000000 0000000 lib/index.js 0000664 0000000 0000000 00000000156 15160706752 0013304 0 ustar 00root root 0000000 0000000 var exports = module.exports;
exports.toJson = require('./xml2json');
exports.toXml = require('./json2xml');
lib/json2xml.js 0000664 0000000 0000000 00000005446 15160706752 0013760 0 ustar 00root root 0000000 0000000 var sanitizer = require('./sanitize.js')
module.exports = function (json, options) {
if (json instanceof Buffer) {
json = json.toString();
}
var obj = null;
if (typeof(json) == 'string') {
try {
obj = JSON.parse(json);
} catch(e) {
throw new Error("The JSON structure is invalid");
}
} else {
obj = json;
}
var toXml = new ToXml(options);
toXml.parse(obj);
return toXml.xml;
}
ToXml.prototype.parse = function(obj) {
var self = this;
var keys = Object.keys(obj);
var len = keys.length;
// First pass, extract strings only
for (var i = 0; i < len; i++) {
var key = keys[i], value = obj[key], isArray = Array.isArray(value);
var type = typeof(value);
if (type == 'string' || type == 'number' || type == 'boolean' || isArray) {
var it = isArray ? value : [value];
it.forEach(function(subVal) {
if (typeof(subVal) != 'object') {
if (key == '$t') {
self.addTextContent(subVal);
} else {
self.addAttr(key, subVal);
}
}
});
}
}
// Second path, now handle sub-objects and arrays
for (var i = 0; i < len; i++) {
var key = keys[i];
if (Array.isArray(obj[key])) {
var elems = obj[key];
var l = elems.length;
for (var j = 0; j < l; j++) {
var elem = elems[j];
if (typeof(elem) == 'object') {
self.openTag(key);
self.parse(elem);
self.closeTag(key);
}
}
} else if (typeof(obj[key]) == 'object') {
self.openTag(key);
self.parse(obj[key]);
self.closeTag(key);
}
}
};
ToXml.prototype.openTag = function(key) {
this.completeTag();
this.xml += '<' + key;
this.tagIncomplete = true;
}
ToXml.prototype.addAttr = function(key, val) {
if (this.options.sanitize) {
val = sanitizer.sanitize(val)
}
this.xml += ' ' + key + '="' + val + '"';
}
ToXml.prototype.addTextContent = function(text) {
this.completeTag();
this.xml += text;
}
ToXml.prototype.closeTag = function(key) {
this.completeTag();
this.xml += '' + key + '>';
}
ToXml.prototype.completeTag = function() {
if (this.tagIncomplete) {
this.xml += '>';
this.tagIncomplete = false;
}
}
function ToXml(options) {
var defaultOpts = {
sanitize: false
};
if (options) {
for (var opt in options) {
defaultOpts[opt] = options[opt];
}
}
this.options = defaultOpts;
this.xml = '';
this.tagIncomplete = false;
}
lib/sanitize.js 0000664 0000000 0000000 00000001467 15160706752 0014031 0 ustar 00root root 0000000 0000000 /**
* Simple sanitization. It is not intended to sanitize
* malicious element values.
*
* character | escaped
* < <
* > >
* ( (
* ) )
* # #
* & &
* " "
* ' '
*/
var chars = {
'&': '&',
'#': '#',
'<': '<',
'>': '>',
'(': '(',
')': ')',
'"': '"',
"'": '''
};
function escapeRegExp(string) {
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
exports.sanitize = function sanitize(value) {
if (typeof value !== 'string') {
return value;
}
Object.keys(chars).forEach(function(key) {
value = value.replace(new RegExp(escapeRegExp(key), 'g'), chars[key]);
});
return value;
}
lib/xml2json.js 0000664 0000000 0000000 00000011636 15160706752 0013756 0 ustar 00root root 0000000 0000000 var expat = require('node-expat');
var sanitizer = require('./sanitize.js')
var joi = require('joi');
var hoek = require('hoek');
// This object will hold the final result.
var obj = {};
var currentObject = {};
var ancestors = [];
var currentElementName = null;
var options = {}; //configuration options
function startElement(name, attrs) {
currentElementName = name;
if(options.coerce) {
// Looping here in stead of making coerce generic as object walk is unnecessary
for(var key in attrs) {
attrs[key] = coerce(attrs[key],key);
}
}
if (! (name in currentObject)) {
if(options.arrayNotation) {
currentObject[name] = [attrs];
} else {
currentObject[name] = attrs;
}
} else if (! (currentObject[name] instanceof Array)) {
// Put the existing object in an array.
var newArray = [currentObject[name]];
// Add the new object to the array.
newArray.push(attrs);
// Point to the new array.
currentObject[name] = newArray;
} else {
// An array already exists, push the attributes on to it.
currentObject[name].push(attrs);
}
// Store the current (old) parent.
ancestors.push(currentObject);
// We are now working with this object, so it becomes the current parent.
if (currentObject[name] instanceof Array) {
// If it is an array, get the last element of the array.
currentObject = currentObject[name][currentObject[name].length - 1];
} else {
// Otherwise, use the object itself.
currentObject = currentObject[name];
}
}
function text(data) {
currentObject['$t'] = (currentObject['$t'] || '') + data;
}
function endElement(name) {
if (currentObject['$t']) {
if (options.trim) {
currentObject['$t'] = currentObject['$t'].trim()
}
if (options.sanitize) {
currentObject['$t'] = sanitizer.sanitize(currentObject['$t']);
}
currentObject['$t'] = coerce(currentObject['$t'],name);
}
if (currentElementName !== name) {
delete currentObject['$t'];
}
// This should check to make sure that the name we're ending
// matches the name we started on.
var ancestor = ancestors.pop();
if (!options.reversible) {
if (('$t' in currentObject) && (Object.keys(currentObject).length == 1)) {
if (ancestor[name] instanceof Array) {
ancestor[name].push(ancestor[name].pop()['$t']);
} else {
ancestor[name] = currentObject['$t'];
}
}
}
currentObject = ancestor;
}
function coerce(value,key) {
if (!options.coerce || value.trim() === '') {
return value;
}
if (typeof options.coerce[key] === 'function')
return options.coerce[key](value);
var num = Number(value);
if (!isNaN(num)) {
return num;
}
var _value = value.toLowerCase();
if (_value == 'true') {
return true;
}
if (_value == 'false') {
return false;
}
return value;
}
/**
* Parses xml to json using node-expat.
* @param {String|Buffer} xml The xml to be parsed to json.
* @param {Object} _options An object with options provided by the user.
* The available options are:
* - object: If true, the parser returns a Javascript object instead of
* a JSON string.
* - reversible: If true, the parser generates a reversible JSON, mainly
* characterized by the presence of the property $t.
* - sanitize_values: If true, the parser escapes any element value in the xml
* that has any of the following characters: <, >, (, ), #, #, &, ", '.
*
* @return {String|Object} A String or an Object with the JSON representation
* of the XML.
*/
module.exports = function(xml, _options) {
_options = _options || {};
var parser = new expat.Parser('UTF-8');
parser.on('startElement', startElement);
parser.on('text', text);
parser.on('endElement', endElement);
obj = currentObject = {};
ancestors = [];
currentElementName = null;
var schema = {
object: joi.boolean().default(false),
reversible: joi.boolean().default(false),
coerce: joi.alternatives([joi.boolean(), joi.object()]).default(false),
sanitize: joi.boolean().default(true),
trim: joi.boolean().default(true),
arrayNotation: joi.boolean().default(false)
};
var validation = joi.validate(_options, schema);
hoek.assert(validation.error === null, validation.error);
options = validation.value;
if (!parser.parse(xml)) {
throw new Error('There are errors in your xml file: ' + parser.getError());
}
if (options.object) {
return obj;
}
var json = JSON.stringify(obj);
//See: http://timelessrepo.com/json-isnt-a-javascript-subset
json = json.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
return json;
};
package.json 0000664 0000000 0000000 00000000767 15160706752 0013367 0 ustar 00root root 0000000 0000000 {
"name": "xml2json",
"version": "0.9.0",
"description": "Converts xml to json and vice-versa, using node-expat.",
"repository": "git://github.com/buglabs/node-xml2json.git",
"license": "MIT",
"main": "index",
"scripts": {
"test": "lab -a code -v -t 93 test/test.js"
},
"dependencies": {
"hoek": "^2.14.0",
"joi": "^6.4.3",
"node-expat": "^2.3.9"
},
"bin": {
"xml2json": "bin/xml2json"
},
"devDependencies": {
"code": "^1.4.1",
"lab": "5.x.x"
}
}
test/ 0000775 0000000 0000000 00000000000 15160706752 0012046 5 ustar 00root root 0000000 0000000 test/.gitignore 0000664 0000000 0000000 00000000013 15160706752 0014030 0 ustar 00root root 0000000 0000000 *.DS_Store
test/coerce-overhead.js 0000664 0000000 0000000 00000000767 15160706752 0015451 0 ustar 00root root 0000000 0000000 var fs = require('fs');
var parser = require('../lib');
var file = __dirname + '/fixtures/large.xml';
var data = fs.readFileSync(file);
// With coercion
var t0 = Date.now();
for(var i = 0; i < 10000; i++) {
var result = parser.toJson(data, {reversible: true, coerce: true, object: true});
}
console.log(Date.now() - t0);
// Without coercion
var t0 = Date.now();
for(var i = 0; i < 10000; i++) {
result = parser.toJson(data, {reversible: true, object: true});
}
console.log(Date.now() - t0);
test/fixtures/ 0000775 0000000 0000000 00000000000 15160706752 0013717 5 ustar 00root root 0000000 0000000 test/fixtures/array-notation.json 0000664 0000000 0000000 00000000073 15160706752 0017561 0 ustar 00root root 0000000 0000000 {"abcd":[{"efg":[{"hijk":["qrstuv",{"lmnop":["wxyz"]}]}]}]} test/fixtures/array-notation.xml 0000664 0000000 0000000 00000000151 15160706752 0017405 0 ustar 00root root 0000000 0000000
qrstuv
wxyz
test/fixtures/coerce.json 0000664 0000000 0000000 00000000642 15160706752 0016054 0 ustar 00root root 0000000 0000000 {"itemRecord":{"value":[{"longValue":"12345"},{"stringValue":{"number":"false","$t":"this is a string value"}},{"moneyValue":{"number":"true","currencyId":"USD","text":"123.45","$t":"104.95"}},{"moneyValue":{"currencyId":"USD","$t":"104.95"}},{"longValue":"0","bool":{"id":"0","$t":"true"}},{"longValue":"0"},{"dateValue":"2012-02-16T17:03:33.000-07:00"},{"stringValue":"SmDZ8RlMIjDvlEW3KUibzj2Q"},{"text":"42.42"}]}}
test/fixtures/coerce.xml 0000664 0000000 0000000 00000001261 15160706752 0015701 0 ustar 00root root 0000000 0000000
12345
this is a string value
104.95
104.95
0
true
0
2012-02-16T17:03:33.000-07:00
SmDZ8RlMIjDvlEW3KUibzj2Q
42.42