Comparison of XML to JSON approaches

Benchmark created by MRis on


Description

Yup

Preparation HTML

<script src="http://svn.coderepos.org/share/lang/javascript/jkl/jkl-parsexml.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://terracoder.com/scripts/jqXMLUtils.js"></script>
<script src="http://goessner.net/download/prj/jsonxml/xml2json.js"></script>

<script>
//================================================================
//class: JKL.ParseXML.FromString
//constructer: new JKL.ParseXML.FromString()
JKL.ParseXML.FromString = function( xmlStr ) {
    this.xmlStr = xmlStr;
    return this;
};

// Extend base class prototype with derived class' functionality
JKL.ParseXML.FromString.prototype = jQuery.extend({}, JKL.ParseXML.prototype, JKL.ParseXML.FromString.prototype);

//================================================================
//method: documentElement()
//return: XML DOM in response body

JKL.ParseXML.FromString.prototype.documentElement = function() {
        if ( ! this.xmlStr ) return;
        
        // Parse the DOM out of the string
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(this.xmlStr, "text/xml");

    return xmlDoc.documentElement;
};

//================================================================
//method: parseResponse()

JKL.ParseXML.FromString.prototype.parse = function () {
        var root = this.documentElement();
        var data = this.parseDocument( root );
        return data;
};

// Changes XML to JSON
function xmlToJson(xml) {
  
  // Create the return object
  var obj = {};

  if (xml.nodeType == 1) { // element
    // do attributes
    if (xml.attributes.length > 0) {
    obj["@attributes"] = {};
      for (var j = 0; j < xml.attributes.length; j++) {
        var attribute = xml.attributes.item(j);
        obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
      }
    }
  } else if (xml.nodeType == 3) { // text
    obj = xml.nodeValue;
  }

  // do children
  if (xml.hasChildNodes()) {
    for(var i = 0; i < xml.childNodes.length; i++) {
      var item = xml.childNodes.item(i);
      var nodeName = item.nodeName;
      if (typeof(obj[nodeName]) == "undefined") {
        obj[nodeName] = xmlToJson(item);
      } else {
        if (typeof(obj[nodeName].length) == "undefined") {
          var old = obj[nodeName];
          obj[nodeName] = [];
          obj[nodeName].push(old);
        }
        obj[nodeName].push(xmlToJson(item));
      }
    }
  }
  return obj;
};
</script>

Setup

testxml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><svg width="800" height="895" xml:id="page.RTG3DB9JLVA4LDL7VR0SS3RSF"><title>Spinning wheel counting</title><rect x="0" y="0" width="100%" height="100%" fill="#ffff88" st_id="52"/><g class="foreground"><flash xmlns:xlink="http://www.w3.org/1999/xlink" metadatatoken="annotationmetadata/metadata(3).xml" RotationPoint="(320.000000,240.000000)" transform="rotate(0.00,392.57,203.57)" xml:id="annotation.MIAKRS6IBFGKPFBJN0KIF9MOQ" visible="1" id="Spinner - 4 - add persistence" x="170.00" y="-19.00" width="445.13" height="445.13" xlink:href="flash/flash2/Spinner - 4 - add persistence.swf"><persistence><![CDATA[<persistencedata><PersistenceProperties id="spinner"><segments>12</segments><segText0>2</segText0><fontSize0>34</fontSize0><fontSizeSelected0>2</fontSizeSelected0><font0>Arial</font0><fontSelected0>0</fontSelected0><fontColor0>undefined</fontColor0><segText1>8</segText1><fontSize1>34</fontSize1><fontSizeSelected1>2</fontSizeSelected1><font1>Arial</font1><fontSelected1>0</fontSelected1><fontColor1>undefined</fontColor1><segText2>5</segText2><fontSize2>34</fontSize2><fontSizeSelected2>2</fontSizeSelected2><font2>Arial</font2><fontSelected2>0</fontSelected2><fontColor2>undefined</fontColor2><segText3>3</segText3><fontSize3>34</fontSize3><fontSizeSelected3>2</fontSizeSelected3><font3>Arial</font3><fontSelected3>0</fontSelected3><fontColor3>undefined</fontColor3><segText4>4</segText4><fontSize4>34</fontSize4><fontSizeSelected4>2</fontSizeSelected4><font4>Arial</font4><fontSelected4>0</fontSelected4><fontColor4>undefined</fontColor4><segText5>11</segText5><fontSize5>34</fontSize5><fontSizeSelected5>2</fontSizeSelected5><font5>Arial</font5><fontSelected5>0</fontSelected5><fontColor5>undefined</fontColor5><segText6>7</segText6><fontSize6>34</fontSize6><fontSizeSelected6>2</fontSizeSelected6><font6>Arial</font6><fontSelected6>0</fontSelected6><fontColor6>undefined</fontColor6><segText7>12</segText7><fontSize7>34</fontSize7><fontSizeSelected7>2</fontSizeSelected7><font7>Arial</font7><fontSelected7>0</fontSelected7><fontColor7>undefined</fontColor7><segText8>10</segText8><fontSize8>34</fontSize8><fontSizeSelected8>2</fontSizeSelected8><font8>Arial</font8><fontSelected8>0</fontSelected8><fontColor8>undefined</fontColor8><segText9>1</segText9><fontSize9>34</fontSize9><fontSizeSelected9>2</fontSizeSelected9><font9>Arial</font9><fontSelected9>0</fontSelected9><fontColor9>undefined</fontColor9><segText10>9</segText10><fontSize10>34</fontSize10><fontSizeSelected10>2</fontSizeSelected10><font10>Arial</font10><fontSelected10>0</fontSelected10><fontColor10>undefined</fontColor10><segText11>6</segText11><fontSize11>34</fontSize11><fontSizeSelected11>2</fontSizeSelected11><font11>Arial</font11><fontSelected11>0</fontSelected11><fontColor11>undefined</fontColor11><segText12>undefined</segText12><fontSize12>14</fontSize12><fontSizeSelected12>0</fontSizeSelected12><font12>Arial</font12><fontSelected12>0</fontSelected12><fontColor12>undefined</fontColor12><segColor0>0x33cc00</segColor0><segColor1>0x6600ff</segColor1><segColor2>0xff9900</segColor2><segColor3>0xff0000</segColor3><segColor4>0xFFFF00</segColor4><segColor5>3978097</segColor5><segColor6>16756059</segColor6><segColor7>0xFFC0CB</segColor7><segColor8>0x98FB98</segColor8><segColor9>0x40E0D0</segColor9><segColor10>16776960</segColor10><segColor11>0xAFEEEE</segColor11><segColor12>0x009300</segColor12><segColor13>undefined</segColor13></PersistenceProperties><PersistenceProperties id="_level0"><persistenceinfo>:2::3::0::1</persistenceinfo></PersistenceProperties></persistencedata>]]></persistence></flash><text transform="translate(179,429) rotate(0,212,69) scale(0.997,0.997)" theme_anno_style="0" language_direction="1" RotationPoint="(320.000000,240.000000)" xml:id="annotation.H6IKAIOSJJV4P1ND5CR4NC4LP" visible="1" editwidth="425.24" editheight="138.44" forcewidth="0" textdirection="0"><tspan justification="center" bullet="0"><tspan><tspan x="47.57" y="35.00" textLength="339.81" font-family="Century Gothic" font-size="36.00" font-weight="bold" fill="#ff00ff">Spin the wheel and </tspan></tspan><tspan><tspan x="7.77" y="77.72" textLength="416.50" font-family="Century Gothic" font-size="36.00" font-weight="bold" fill="#ff00ff">move that many circles </tspan></tspan><tspan><tspan x="109.71" y="120.44" textLength="205.83" font-family="Century Gothic" font-size="36.00" font-weight="bold" fill="#ff00ff">into the box</tspan></tspan></tspan></text><path fill-rule="evenodd" slscaler="0.033333" d="M96.00 578.00 L677.13 578.00 L677.13 865.13 L96.00 865.13 z" shapename="3" fill="#ffffc6" st_id="50" stroke="#000000" stroke-width="3" fade-time="6" fade-enable="0" RotationPoint="(320.000000,240.000000)" transform="rotate(0.00,386.57,721.57)" xml:id="annotation.9PF0BDM5A4SKL0PVG9O2M37PF" visible="1"/><ellipse cx="74.57" cy="508.57" rx="32.57" ry="32.57" shapename="3" fill="#ff8080" st_id="49" stroke="#000000" stroke-width="3" fade-time="6" fade-enable="0" RotationPoint="(320.000000,240.000000)" transform="rotate(0.00,74.57,508.57)" locked="30" cloner="1" xml:id="annotation.KSAB09FOJHE4L057IHIU6FDG4" visible="1"/></g></svg>';

Test runner

Ready to run.

Testing in
TestOps/sec
Convert XML string to XML Document object
parser = new DOMParser();
xmlDoc = parser.parseFromString(testxml, "text/xml");
ready
XML2JSON
parser = new DOMParser();
xmlDoc = parser.parseFromString(testxml, "text/xml");
jsonDocString = xml2json(xmlDoc);
// jsonDoc = JSON.parse(jsonDocString);
ready
JKL
jkl = new JKL.ParseXML.FromString(testxml);
jsonDoc = jkl.parse();
ready
XMLObjectifier with DOM Parser
parser = new DOMParser();
xmlDoc = parser.parseFromString(testxml, "text/xml");
jsonDoc = $.xmlToJSON(xmlDoc);
ready
XMLObjectifier without DOM Parser
jsonDoc = $.xmlToJSON($.textToXML(testxml));
ready
xmlToJson
parser = new DOMParser();
xmlDoc = parser.parseFromString(testxml, "text/xml");
jsonDoc = xmlToJson(xmlDoc);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.