2

Im using ExtJs 4.2.1.

Is there an "easy" way to convert JS object to xml? I mean a simple function like:

Ext.JSON.encode(object);

To convert object to Json.

Lets say the following object for example:

Root:
  Child1
  Child2
  Child3

To the following xml:

<Root>
   <Child1> some value </Child1>
   <Child2> some value </Child2>
   <Child3> some value </Child3>
</Root>

I was trying to search it in the documentation, but didn't came to any solution like that.

Thanks.

yanivsh
  • 293
  • 1
  • 5
  • 16
  • As far as I know, there is nothing built in to ExtJS. You'll have to find a 3rd party script, or write your own. – forgivenson Nov 24 '14 at 18:40

4 Answers4

0

make a XML string using Json data and convert the XML string to XML object

To convert string to XML go through the following link How to convert string to XML object in JavaScript?

Community
  • 1
  • 1
Surya Prakash Tumma
  • 2,153
  • 4
  • 27
  • 47
0

Eventually I used this nice script for the conversion.

Hopefully Sencha will add built in functions for encoding xml in future versions.

yanivsh
  • 293
  • 1
  • 5
  • 16
0

One easy way to do it is using a middle tier Java class. Lot of java libraries available to convert JSON to XML like Jackson, eclipsemoxy

Chandru
  • 964
  • 11
  • 16
0

I did wrote one method while using EXT JS 4, i got the same problem for conversion of Javascript object to XML. this one handles Array Objects also. i have only considered my special cases non other.. so feel free to make any changes..

    convertJsToXML: function (rec, rootNode) {
    var xmlString = "";
    var withoutRoot = false;
    for (var object in rec) {
        if (!isNaN(object)) {
            withoutRoot = true;
            xmlString += this.convertJsToXML(rec[object], rootNode);
        } else if (typeof rec[object] == 'object') {
            xmlString += this.convertJsToXML(rec[object], object);
        } else if (rec[object] != null && rec[object] != "") {
            xmlString += "<" + object + ">" + rec[object] + "</" + object + ">";
        }
    }
    if (!withoutRoot)
        xmlString = "<" + rootNode + ">" + xmlString + "</" + rootNode + ">";
    return xmlString;
}