Is there a possibility to change JSON data to XML in client side(Java sript or in Jquery).
Asked
Active
Viewed 2.3k times
1
-
1Look here: http://stackoverflow.com/questions/1773550/xml-json-conversion-in-javascript – Daniel Conde Marin Feb 11 '14 at 06:40
-
@Daniel I have asked for the possibility in doing that with JQuery or Java script builtins. Is this broad ? – Muthukumar Palaniappan Feb 11 '14 at 06:45
-
In the question the OP found 2 tools using jQuery. Isn't that useful? – Daniel Conde Marin Feb 11 '14 at 06:50
-
I am afraid of the case handlings in this plugins, I read somewhere it doesnt work on data escape sequences. – Muthukumar Palaniappan Feb 11 '14 at 06:52
2 Answers
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