How to convert (or serialize) javascript data object (or model) to xml using ExtJs
Asked Answered
N

3

2
  1. How do I convert that instance to XML?

I am working with ExtJs 4.2. I have an instance of an ExtJs model.

  1. How do I convert that to XML?

I have an anonymous JavaScript object (with a couple of properties).

I am not sure if the answers for both of the above is same. The XML will be sent as body to a POST operation against a third party web service.

Nesselrode answered 4/11, 2013 at 16:59 Comment(1)
Ditch XML for the mor readable, lightweight data-interchange format, and de facto standard: JSONErnestoernestus
P
7

Converting JSON to XML in JavaScript is blasphemy!

But if I had to do it I would use: http://code.google.com/p/x2js/

Politesse answered 5/11, 2013 at 5:28 Comment(5)
Your answer helped me to find a good library for conversions. However, I am not trying to convert JSON to XML. I am trying to convert a JavaScript Object to XML. Am I missing something in my question?Nesselrode
what do you think JSON is? Look at the first usage example in the link : x2js.json2xml .Politesse
I had a different meaning of JSON prior to this conversation. I never knew that JavaScript Object is nothing but an evaluated JSON string. Thanks for letting me know about this. Also, it looks like there is no readily available library which does the conversion from ExtJS Model object to XML. Please add to this thread once you come across one.Nesselrode
Take a look at the XML grid example: docs.sencha.com/extjs/4.2.1/extjs-build/examples/grid/…Politesse
It looks like the reader works if and only if it is a http request through ajax. In my case I already have xml in a string objectNesselrode
R
3

I haven't used this myself, but it seems like a good solution for anyone doing for a front-/back-end agnostic approach.

https://github.com/michaelkourlas/node-js2xmlparser

Receptor answered 13/2, 2014 at 15:52 Comment(0)
N
3

I wrote the following function to do this job. Works pretty well when collections (arrays) inside your object are named with "s" suffix (as plurals for their content).

function serializeNestedNodeXML(xmlDoc, parentNode, newNodeName, obj) {
    if (Array.isArray(obj)) {
        var xmlArrayNode = xmlDoc.createElement(newNodeName);
        parentNode.appendChild(xmlArrayNode);
        obj.forEach(function (e) {
            serializeNestedNodeXML(xmlDoc, xmlArrayNode, newNodeName.substring(0, newNodeName.length - 1), e)
        });
        return;     // Do not process array properties
    } else if (obj) {
        var objType = typeof obj;
        switch (objType) {
            case 'string': case 'number': case 'boolean':
                parentNode.setAttribute(newNodeName, obj)
                break;
            case 'object':
                var xmlProp = xmlDoc.createElement(newNodeName);
                parentNode.appendChild(xmlProp);
                for (var prop in obj) {
                    serializeNestedNodeXML(xmlDoc, xmlProp, prop, obj[prop]);
                }
                break;
        }
    }
}

And I invoked it this way (as a function of the serialized class itself):

this.serializeToXML = function () {
    var xmlDoc = document.implementation.createDocument(null, "YourXMLRootNodeName", null);
    serializeNestedNodeXML(xmlDoc, xmlDoc.documentElement, 'YourSerializedClassName', this);
    var serializer = new XMLSerializer();
    return serializer.serializeToString(xmlDoc);
}

But an hour later I moved to JSON...

Nepean answered 24/2, 2018 at 20:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.