Skip to content

Instantly share code, notes, and snippets.

@mediaupstream
Created December 19, 2011 06:57
Show Gist options
  • Select an option

  • Save mediaupstream/1495793 to your computer and use it in GitHub Desktop.

Select an option

Save mediaupstream/1495793 to your computer and use it in GitHub Desktop.

Revisions

  1. mediaupstream revised this gist Dec 19, 2011. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion json2xml.js
    Original 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 != '@'){
  2. mediaupstream revised this gist Dec 19, 2011. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion json2xml.js
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,6 @@
    */
    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; };

  3. mediaupstream revised this gist Dec 19, 2011. No changes.
  4. mediaupstream created this gist Dec 19, 2011.
    70 changes: 70 additions & 0 deletions json2xml.js
    Original 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!
    };