Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xml2json

Package Overview
Dependencies
Maintainers
0
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xml2json - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

lib/json2xml.js

101

lib/index.js

@@ -1,99 +0,4 @@

var expat = require('node-expat');
var fs = require('fs');
var exports = module.exports;
// This object will hold the final result.
var obj = currentObject = {};
var ancestors = [];
var options = {}; //configuration options
function startElement(name, attrs) {
if (! (name in currentObject)) {
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) {
data = data.trim();
if (!data.length) {
return;
}
currentObject['$t'] = data;
}
function endElement(name) {
// 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 ((Object.keys(currentObject).length == 1) && ('$t' in currentObject)) {
if (ancestor[name] instanceof Array) {
//console.log("list-replacing $t in " + name);
ancestor[name].push(ancestor[name].pop()['$t']);
} else {
//console.log("replacing $t in " + name);
ancestor[name] = currentObject['$t'];
}
} else {
//console.log("final " + name + ":");
//console.log(currentObject);
}
}
currentObject = ancestor;
}
module.exports.toJson = function(xml, _options) {
var parser = new expat.Parser('UTF-8');
parser.on('startElement', startElement);
parser.on('text', text);
parser.on('endElement', endElement);
obj = currentObject = {};
ancestors = [];
options = {
object: false,
reversible: false
};
for (var opt in _options) {
options[opt] = _options[opt];
}
if (!parser.parse(xml)) {
console.log('-->'+ xml + '<--');
throw new Error('There are errors in your xml file: ' + parser.getError());
}
if (options.object) {
return obj;
}
return JSON.stringify(obj);
};
exports.toJson = require('./xml2json');
exports.toXml = require('./json2xml');
{ "name" : "xml2json",
"version": "0.1.1",
"version": "0.2.0",
"author": "Andrew Turley",

@@ -8,6 +8,3 @@ "email": "aturley@buglabs.net",

"main": "index",
"maintainers":[ {
"name": "Camilo Aguilar",
"email": "camilo@buglabs.net"}
],
"contributors":[ {"name": "Camilo Aguilar", "email": "camilo@buglabs.net"}],
"dependencies": {

@@ -14,0 +11,0 @@ "node-expat": "1.3.2"

@@ -17,13 +17,10 @@ var fs = require('fs');

var data = fs.readFileSync(fixturesPath + '/' + file);
var result = parser.toJson(data);
var result = parser.toJson(data, {reversible: true});
var data2 = fs.readFileSync(fixturesPath + '/' + file);
result = parser.toJson(data2);
var jsonFile = basename + '.json'
var jsonFile = basename + '.json';
var expected = fs.readFileSync(fixturesPath + '/' + jsonFile) + '';
if (result) {
result = result.trim();
}
if (expected) {

@@ -33,3 +30,19 @@ expected = expected.trim();

assert.deepEqual(result, expected, jsonFile + ' and ' + file + ' are different');
console.log('All tests passed!');
console.log('[xml2json: ' + file + '->' + jsonFile + '] passed!');
} else if( ext == '.json') {
var basename = path.basename(file, '.json');
if (basename.match('reversible')) {
var data = fs.readFileSync(fixturesPath + '/' + file);
var result = parser.toXml(data);
var xmlFile = basename.split('-')[0] + '.xml';
var expected = fs.readFileSync(fixturesPath + '/' + xmlFile) + '';
if (expected) {
expected = expected.trim();
}
//console.log(result + '<---');
assert.deepEqual(result, expected, xmlFile + ' and ' + file + ' are different');
console.log('[json2xml: ' + file + '->' + xmlFile + '] passed!');
}
}

@@ -36,0 +49,0 @@ }

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc