public
Last active — forked from mediaupstream/json2xml.js

json2xml NodeJS Module - Convert an xml2js JSON object back to XML

  • Download Gist
json2xml.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/**
* json2xml NodeJS Module - Convert an xml2js JSON object back to XML
*
* Redsandro - changed into requirable
*
* @author Derek Anderson
* @copyright 2011 Media Upstream
* @license MIT License
* @url https://gist.github.com/1495793
*
* Usage:
* json2xml = require('./json2xml').json2xml;
*
* json2xml({"jsonObject": "@": { "attribute": "value", "foo": "bar" }}, "rootElement", function(xml) {
* console.log(xml); // log the XML data
* });
*
* License
* -------
*
* Copyright (c) 2011 Derek Anderson / Media Upstream
* https://gist.github.com/1495793
*
* Copyright (c) 2012 Redsandro - Turned into NodeJS Module
* http://www.redsandro.com/
*
* The MIT License (MIT)
*
* 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.
*/
 
 
 
exports.json2xml = function(json, root, cb){
var recursion = 0;
var xml = '<?xml version="1.0" ?>';
var isArray = function(obj) { return obj.constructor == Array; };
var parseAttributes = function(node){
for(key in node){
var value = node[key];
xml += ' ' + key +'="'+ value +'"';
};
xml += '>';
};
var parseNode = function(node, parentNode){
recursion++;
// Handle Object structures in the JSON properly
if(!isArray(node)){
xml += '<'+ parentNode;
if(typeof node == 'object' && node['@']){
parseAttributes(node['@']);
} else {
xml += '>';
}
for(key in node){
var value = node[key];
// text values
if(typeof value == 'string'){
if(key === '#'){
xml += value;
} else {
xml += '<'+ key +'>'+ value + '</'+key+'>';
}
}
// is an object
if(typeof value == 'object' && key != '@'){
parseNode(node[key], key);
}
}
recursion--;
xml += '</'+ parentNode +'>';
}
// Handle array structures in the JSON properly
if(isArray(node)){
for(var i=0; i < node.length; i++){
parseNode(node[i], parentNode);
}
recursion--;
}
if (recursion === 0) { cb(xml); }
};
parseNode(json, root); // fire up the parser!
};

The example does not work with node.js.
The JSON object is invalid in that case, with valid JSON there is no way to add attributes to a node.

This does work with node.js and it is proper json.
As you can see, attributes are added in a key called '@'.

There is a curly bracket missing, the proper notation for the JSON object is {"jsonObject": {"@": { "attribute": "value", "foo": "bar" }}}

Looks like it doesn't work right with arrays :(

 in {"trk":{"trkseg":[{"trkpt":{"time":9}},{"trkpt":{"time":0}}]}}
 out <?xml version="1.0" ?><gpx><trk><trkseg><trkpt></trkpt></trkseg><trkseg><trkpt></trkpt></trkseg></trk></gpx>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.