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.1.2 to 0.1.3

94

lib/json2xml.js
/**
* Converts JSON object to XML string.
*
*
*
* Copyright(c) 2011 Etienne Lachance <et@etiennelachance.com>
* MIT Licensed
*/
/*
* 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)
* Ignore special objects - objects that inherit other objects (in practice, when working with a third-party library, most of those are circular structures)
*/

@@ -24,55 +24,55 @@

var settings = {
attributes_key:false,
header:false
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;
}
'use strict';
var result = opts.header?'<?xml version="1.0" encoding="UTF-8"?>':'';
opts.header = false;
type = json.constructor.name;
if (opts) {
Object.keys(settings).forEach(function (k) {
if (opts[k] === undefined) {
opts[k] = settings[k];
}
});
} else {
opts = settings;
}
if(type==='Array'){
var result = opts.header ? '<?xml version="1.0" encoding="UTF-8"?>' : '';
opts.header = false;
json.forEach(function(node){
result += xml(node, opts);
});
if (!!json.length && typeof json !== 'string') { //Array
json.forEach(function (node) {
result += xml(node, opts);
});
} else if (typeof json === 'object') {
Object.keys(json).forEach(function (key) {
if (key !== opts.attributes_key) {
var node = json[key],
attributes = '';
} else if(type ==='Object' && typeof json === "object") {
if (node === undefined || node === null) {
node = '';
}
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(typeof node === 'undefined' || null === node) node = '';
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;');
}
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;
}
return result;
};
{
"name": "json2xml",
"description": "Simple JavaScript Object to XML string converter.",
"version": "0.1.2",
"version": "0.1.3",
"homepage": "https://github.com/estheban/node-json2xml",

@@ -46,4 +46,4 @@ "author": {

"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.2",
"_id": "json2xml@0.1.3",
"_from": "git://github.com/estheban/node-json2xml.git"
}

@@ -1,44 +0,54 @@

node-json2xml
===========
# node-json2xml
Description
-----------
## Description
Simple JavaScript Object to XML string converter.
Installation
------------
## Installation
Simplest way to install `json2xml` is to use [npm](http://npmjs.org), just `npm
install json2xml` which will download json2xml and all dependencies.
Install via [npm](https://www.npmjs.com/package/json2xml), which will download `json2xml` and all of its dependencies.
Simple usage
-----------
var json2xml = require('json2xml');
json2xml(json, options);
```bash
npm install json2xml
```
Options & Behaviour
-----------
none:
json2xml({ a: 1 });
//<a>1</a>
## Simple usage
empty node:
json2xml({ a: '' });
//<a/>
While the name of the repo is `json2xml`, it is really `pojo2xml`, since you will need to run `JSON.parse` on the JSON data prior to converting.
add header:
json2xml({ a: 1 }, { header: true });
//<?xml version="1.0" encoding="UTF-8"?><a>1</a>
```js
var fs = require('fs');
var json2xml = require('json2xml');
add node attributes:
json2xml({ a: 1, attr: { b: 2, c: 3 } }, { attributes_key: 'attr' });
// <a b="2" c="3" >1</a>
fs.readFile('data.json', 'utf8', function read (err, data) {
if (err) console.log(err);
fs.writeFile('data.xml', json2xml(JSON.parse(data)));
});
```
arrays:
json2xml([ { a: 1 }, { b: 2 } ]);
//'<a>1</a><b>2</b>
## Options & Behaviour
json2xml({ 'items': [ { item: 1 }, { item: 2 } ] });
//'<items><item>1</item><item>2</item></items>'
```js
// none:
json2xml({ a: 1 });
//<a>1</a>
// 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