3

I want to convert a JSON object to a XML String and I can't figure a proper way to do it. I've found a neat little jQuery plugin called json2xml at https://gist.github.com/c4milo/3738875 but it doesn't escape the data.

How can I escape the data properly so that the browser's XML parser will work?

4 Answers 4

2

You can try this small library http://code.google.com/p/x2js/

Sign up to request clarification or add additional context in comments.

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
1

There is no unique way of doing this. You should be using XML with a schema only, and JSON doesn't have such a schema. Any such transformation when done naively is likely to break.

Why don't you just use XML or JSON consequently?

2 Comments

The conversion is just for using the XML with XSLT ... not my idea. The server-side scripts only output JSON and I have to somehow convert the response in a valid XML. Is this doable?
Of course it is doable. XML is not that hard to produce, in particular when you have a DOM implementation available (I'd avoid producing a string just to feed it into a parser once). However, I would not use an approach such as the one on the website you gave, but actually try to do a /semantic/ transformation. You want the resulting XML to be valid according so some schema, after all.
1

You can use the external js available by google named x2js.js

You can see the demo over here.

jsFiddle Demo

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
0

you can use this function in your code to convert JSON to XML in js

var json2xml = function (o) {

            var tab = "\t";
            var toXml = function (v, name, ind) {
                var xml = "";
                if (v instanceof Array) {
                    for (var i = 0, n = v.length; i < n; i++)
                        xml += ind + toXml(v[i], name, ind + "\t") + "\n";
                }
                else if (typeof (v) == "object") {
                    var hasChild = false;
                    xml += ind + "<" + name;
                    for (var m in v) {
                        if (m.charAt(0) == "@")
                            xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\"";
                        else
                            hasChild = true;
                    }
                    xml += hasChild ? ">" : "/>";
                    if (hasChild) {
                        for (var m in v) {
                            if (m == "#text")
                                xml += v[m];
                            else if (m == "#cdata")
                                xml += "<![CDATA[" + v[m] + "]]>";
                            else if (m.charAt(0) != "@")
                                xml += toXml(v[m], m, ind + "\t");
                        }
                        xml += (xml.charAt(xml.length - 1) == "\n" ? ind : "") + "</" + name + ">";
                    }
                }
                else {
                    xml += ind + "<" + name + ">" + v.toString() + "</" + name + ">";
                }
            }
            return xml;
        };

you get XML DOM in return, which in return you need to serialize

so in the main

            var xmlDOM = json2xml(eval(jsonObj));
            var oSerializer = new XMLSerializer();
            var sXML = oSerializer.serializeToString(xmlDOM);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.