How to convert XML to JSON without CommonJS?

You must Login before you can answer or comment on any questions.

I've found several subpar answers out there, and the only ones that seem to be reliable require CommonJS.

Is there any way for my iOS app to convert XML to JSON without using CommonJS?

— asked 11 months ago by Freddie C
0 Comments

2 Answers

function xmlToJson(xml) {
    var attr, child, attrs = xml.attributes, children = xml.childNodes, key = xml.nodeType, obj = {}, i = -1;
 
    if(attrs && key == 1 && attrs.length) {
        obj[ key = '@attributes'] = {};
        while( attr = attrs.item(++i)) {
            obj[key][attr.nodeName] = attr.nodeValue;
        }
        i = -1;
    } else if(key == 3) {
        obj = xml.nodeValue;
    }
    for ( var i = 0; i < children.length; i++) {
        var child = children.item(i);
        key = child.nodeName;
        if(obj.hasOwnProperty(key)) {
            if(obj.toString.call(obj[key]) != '[object Array]') {
                obj[key] = [obj[key]];
            }
            obj[key].push(xmlToJson(child));
        } else {
            obj[key] = xmlToJson(child);
        }
    }
 
    return obj;
}
found this lying around on my computer... think it works

— answered 11 months ago by Aaron Saunders
answer permalink
3 Comments
  • Hi Aaron, I tried this, but it doesn't seem to work. It's not returning anything. I'm trying to use Yahoo YQL for fantasy sports: https://developer.yahoo.com/yql/console/

    — commented 11 months ago by Freddie C

  • After some more digging in with this, i see that you are defining attrs and children. However, if I console log either of those, they come back empty... which is probably why no json is returned. Where are the "attributes" and "childNodes" properties supposed to come from?

    — commented 11 months ago by Freddie C

  • I didn't think Ti's XML implementation supports attributes?

    — commented 11 months ago by Mark Henderson

commonJS is a just a way to structure code. The code you end up with for the conversion process should be the same. I googled "convert xml to json using javascript" and got a bunch of results. The first result pertained to titanium. Here is the link.

— answered 11 months ago by Darren Adams
answer permalink
6 Comments
  • Hi Darren, thanks for the help. I actually tried this one already, but it doesn't return anything. I get no errors, it just seems to not return any data. However, if I log the data I pass in to that, i get my XML block no problem...

    — commented 11 months ago by Freddie C

  • Yep, I should have tested this first. I get an error:

    [ERROR] Script Error = 'undefined' is not a function (evaluating 'xml.hasChildNodes()') at app.js (line 22).

    I'm running out of time for today, but I'll check it out again tomorrow.

    — commented 11 months ago by Darren Adams

  • Thanks Darren, I would love to see what you come up with. If I nail anything down today with this, I'll report back.

    — commented 11 months ago by Freddie C

  • Show 3 more comments

Your Answer

Think you can help? Login to answer this question!