1

Is there a possibility to change JSON data to XML in client side(Java sript or in Jquery).

Muthukumar Palaniappan
  • 1,622
  • 5
  • 25
  • 49

2 Answers2

5

Try using JQuery http://api.jquery.com/jQuery.parseXML/

You can create an empty xml document like:

$.parseXML("<xml></xml>") and then set properties on the document got from this.

An example to demonstrate:

var doc = $.parseXML("<xml/>")
var json = {key1: 1, key2: 2}
var xml = doc.getElementsByTagName("xml")[0]
var key, elem

for (key in json) {
  if (json.hasOwnProperty(key)) {
    elem = doc.createElement(key)
    $(elem).text(json[key])
    xml.appendChild(elem)
  }
}

console.log(xml.outerHTML) // logs <xml><key1>1</key1><key2>2</key2></xml>
closure
  • 7,412
  • 1
  • 23
  • 23
0

Use jQuery.parseJSON to parse JSON and then parseXML() to create an empty XML structure and createElement(),createTextNode() and appendChild() to append nodes to it.

anna
  • 585
  • 1
  • 6
  • 22