Created
December 19, 2011 06:57
-
-
Save mediaupstream/1495793 to your computer and use it in GitHub Desktop.
Revisions
-
mediaupstream revised this gist
Dec 19, 2011 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,6 @@ var json2xml = function(json, root, cb){ } else { xml += '<'+ key +'>'+ value + '</'+key+'>'; } } // is an object if(typeof value == 'object' && key != '@'){ -
mediaupstream revised this gist
Dec 19, 2011 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,6 @@ */ var json2xml = function(json, root, cb){ var recursion = 0; var xml = '<?xml version="1.0" ?>'; var isArray = function(obj) { return obj.constructor == Array; }; -
mediaupstream revised this gist
Dec 19, 2011 . No changes.There are no files selected for viewing
-
mediaupstream created this gist
Dec 19, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,70 @@ /** * json2xml - Convert an xml2js JSON object back to XML * * @author Derek Anderson * @copyright 2011 Media Upstream * @license MIT License * * Usage: * * json2xml({"Foo": "@": { "baz": "bar", "production":"true" }}, "Test", function(xml){ * console.log(xml); // log the XML data * }); * */ var json2xml = function(json, root, cb){ var recursion = 0; var parents = []; 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! };