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

json2xml

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json2xml - npm Package Compare versions

Comparing version 0.0.1 to 0.1.1

.npmignore

94

lib/json2xml.js
/**
* 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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}
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>'
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