Search code examples
javajavascriptxmljsondynatree

Conversion of JSON to XML


I am converting JSON Values to XML. Instead of getting JSON properties as elements of XML I am getting &quot;title&quot;:&quot;source&quot;. The output I wanted is <title>source</title>. What is the mistake I am doing? I am writing this code in JavaScript function.

I am using x2js plugin for conversion and I have included it using script tag.

My code to convert dynatree to JSON and JSON to XML is:

var x2js = new X2JS();

var tree = $("#source").dynatree("getTree").toDict();
alert("  tree:"+tree);
var jsonObject = JSON.stringify(tree);//dynatree to JSON
alert(" jsonObject :"+jsonObject);       
var xmlAsStr = x2js.json2xml_str( jsonObject );//JSON to XML
alert("xml "+xmlAsStr);

Solution

  • Try to not use JSON.stringify(tree); this escapes the string.

    Set var xmlAsStr = x2js.json2xml_str(tree);