Comparing version 0.0.1 to 0.1.1
/** | ||
* Converts JSON object to XML string. | ||
* | ||
* @param name of the first tag | ||
* @param json object to convert | ||
* @return XML string | ||
* | ||
@@ -11,30 +8,69 @@ * Copyright(c) 2011 Etienne Lachance <et@etiennelachance.com> | ||
*/ | ||
/* | ||
* Modifications (Ivo Georgiev <ivo@linvo.org>): | ||
* Escape XML entities to avoid breaking the XML if any string in the JSON contains a special char | ||
* Ignore special objects - objects that inherit other objects (in practice, when working with a third-party library, most of those are circular structures) | ||
*/ | ||
var json2xml = Object(); | ||
/* | ||
* Modifications (Alan Clarke <hi@alz.so>): | ||
* added unit tests, ability to add xml node attributes, xml header option and simplified syntax | ||
* removed root node, this is already covered by the module's default functionality | ||
*/ | ||
exports.toXml = function(name, json) { | ||
var header = "<"+name+">"; | ||
var footer = "</"+name+">"; | ||
var result = ""; | ||
for(var key in json) { | ||
switch(Object.prototype.toString.call(json[key])) { | ||
case "[object Array]": | ||
for(var elem in json[key]) { | ||
result += this.toXml(key, json[key][elem], key); | ||
} | ||
break; | ||
case "[object Object]": | ||
result += this.toXml(key, json[key], key); | ||
break; | ||
default: | ||
result += "<"+key+">"+json[key]+"</"+key+">"; | ||
break; | ||
} | ||
} | ||
if(name != "") { | ||
result = header+result+footer; | ||
} | ||
return result; | ||
var util = require('util'); | ||
var settings = { | ||
attributes_key:false, | ||
header:false | ||
}; | ||
module.exports = function xml(json, opts) { | ||
if(opts){ | ||
Object.keys(settings).forEach(function(k){ | ||
if(typeof opts[k]==='undefined'){ | ||
opts[k] = settings[k]; | ||
} | ||
}); | ||
} else { | ||
opts = settings; | ||
} | ||
var result = opts.header?'<?xml version="1.0" encoding="UTF-8"?>':''; | ||
opts.header = false; | ||
type = json.constructor.name; | ||
if(type==='Array'){ | ||
json.forEach(function(node){ | ||
result += xml(node, opts); | ||
}); | ||
} else if(type ==='Object' && typeof json === "object") { | ||
Object.keys(json).forEach(function(key){ | ||
if(key!==opts.attributes_key){ | ||
var node = json[key], | ||
attributes = ''; | ||
if(opts.attributes_key && json[opts.attributes_key]){ | ||
Object.keys(json[opts.attributes_key]).forEach(function(k){ | ||
attributes += util.format(' %s="%s"', k, json[opts.attributes_key][k]); | ||
}); | ||
} | ||
var inner = xml(node,opts); | ||
if(inner){ | ||
result += util.format("<%s%s>%s</%s>", key, attributes, xml(node,opts), key); | ||
} else { | ||
result += util.format("<%s%s/>", key, attributes); | ||
} | ||
} | ||
}); | ||
} else { | ||
return json.toString().replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); | ||
} | ||
return result; | ||
} |
{ | ||
"name": "json2xml", | ||
"description": "JSON 2 XML Parser", | ||
"keywords" : ["json","xml"], | ||
"homepage" : "https://github.com/estheban/node-json2xml", | ||
"version": "0.0.1", | ||
"author": "Etienne Lachance <et@etiennelachance.com>", | ||
"main": "./index.js", | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "https://estheban@github.com/estheban/node-json2xml.git" | ||
} | ||
"description": "Simple JavaScript Object to XML string converter.", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/alanclarke/node-json2xml", | ||
"author": { | ||
"name": "Etienne Lachance", | ||
"email": "et@etiennelachance.com" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Alan Clarke", | ||
"email": "hi@alz.so", | ||
"url": "alz.so" | ||
}, | ||
{ | ||
"name": "Ivo Georgiev", | ||
"email": "ivo@linvo.org" | ||
} | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/estheban/node-json2xml.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/estheban/node-json2xml/issues" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/estheban/node-json2xml/blob/master/LICENSE-MIT" | ||
} | ||
], | ||
"main": "lib/json2xml", | ||
"engines": { | ||
"node": ">= 0.6.0" | ||
}, | ||
"scripts": { | ||
"test": "grunt test" | ||
}, | ||
"devDependencies": { | ||
"grunt": "~0.3.11" | ||
}, | ||
"keywords": [], | ||
"readme": "node-json2xml\n===========\n\nDescription\n-----------\n\nSimple JavaScript Object to XML string converter.\n\nInstallation\n------------\n\nSimplest way to install `json2xml` is to use [npm](http://npmjs.org), just `npm\ninstall json2xml` which will download json2xml and all dependencies.\n\nSimple usage\n-----------\n \n var json2xml = require('json2xml');\n json2xml({a:1});\n //<a>1</a>\n\t \n\nOptions\n-----------\n\n\tadd header:\n\n\tjson2xml({a:1}, { header:true });\n\t//<?xml version=\"1.0\" encoding=\"UTF-8\"?><a>1</a>\n\n\tadd node attributes:\n\n\tjson2xml({a:1, attr:{b:2,c:3 }}, { attributes_key:'attr' });\t\n\t// <a b=\"2\" c=\"3\" >1</a>", | ||
"_id": "json2xml@0.1.1", | ||
"dist": { | ||
"shasum": "03d51836b3b3e9a53980132e3a3d07c50bb7d9ec" | ||
}, | ||
"_from": "git://github.com/alanclarke/node-json2xml.git" | ||
} |
@@ -13,18 +13,33 @@ node-json2xml | ||
Simplest way to install `json2xml` is to use [npm](http://npmjs.org), just `npm | ||
install xml2js` which will download xml2js and all dependencies. | ||
install json2xml` which will download json2xml and all dependencies. | ||
Simple usage | ||
----------- | ||
var json2xml = require('json2xml'); | ||
json2xml(json, options); | ||
var sys = require('sys'), | ||
fs = require('fs'), | ||
xml2js = require('xml2js'); | ||
Options & Behaviour | ||
----------- | ||
none: | ||
json2xml({ a: 1 }); | ||
//<a>1</a> | ||
var parser = new xml2js.Parser(); | ||
parser.addListener('end', function(result) { | ||
console.log(sys.inspect(result)); | ||
console.log('Done.'); | ||
}); | ||
fs.readFile(__dirname + '/foo.xml', function(err, data) { | ||
parser.parseString(data); | ||
}); | ||
empty node: | ||
json2xml({ a: '' }); | ||
//<a/> | ||
add header: | ||
json2xml({ a: 1 }, { header: true }); | ||
//<?xml version="1.0" encoding="UTF-8"?><a>1</a> | ||
add node attributes: | ||
json2xml({ a: 1, attr: { b: 2, c: 3 } }, { attributes_key: 'attr' }); | ||
// <a b="2" c="3" >1</a> | ||
arrays: | ||
json2xml([ { a: 1 }, { b: 2 } ]); | ||
//'<a>1</a><b>2</b> | ||
json2xml({ 'items': [ { item: 1 }, { item: 2 } ] }); | ||
//'<items><item>1</item><item>2</item></items>' |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
7971
8
137
1
44
0
1
2