XML localName, nodeValue returning null

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

I'm trying to loop through what seems to be a simple xml file and can't get anything to work at all. I've exhausted all the resources I can find but It seems like I'm just doing this wrong. All I want to do is fetch each node titled 'row' and loop through it's children to get localNames and nodeValues. ATM the Ti.API.info call returns null and it's also telling me that each 'row' element has 15 children.

Here is the code:

var xml = this.responseXML;
        nodes = xml.documentElement.getElementsByTagName("ROW");
        Ti.API.info("Nodes = " + nodes + ", length: " + nodes.length);
        for(var i = 0; i < nodes.length; i++) {
            if(nodes.item(i).hasChildNodes()) {
                for(var i2=0; i2 < nodes.item(i).childNodes.length; i2++) {
                    Ti.API.info('In row '+i+': '+nodes.item(i).childNodes.item(i2).localName);
                }
            } else {
                Ti.API.info('node length: '+nodes.item(i).length);
            }
        }
and the xml:
<ROW>
<ID>307</ID>
<SECTION>1</SECTION>
<SECTION_TITLE>Here's how we will inform and engage</SECTION_TITLE>
<MANDATORY_NUM>6</MANDATORY_NUM>
<DISPLAY>1</DISPLAY>
<CHECKABLE>0</CHECKABLE>
<REQUIREMENT>
aasdf
</REQUIREMENT>
</ROW>
Any insight on this would be greatly appreciated. Thanks in advance!

1 Answer

I'm not sure which api call is returning null. I can run the following code:

var c = Ti.Network.createHTTPClient();
c.onload = function() {
    var xml = this.responseXML;
    var nodes = xml.documentElement.getElementsByTagName("ROW");
    for (var i = 0; i < nodes.length; i++) {
        if(nodes.item(i).hasChildNodes()) {
            for(var i2=0; i2 < nodes.item(i).childNodes.length; i2++) {
                Ti.API.info('In row '+i+': '+nodes.item(i).childNodes.item(i2).localName);
            }
        } else {
            Ti.API.info('node length: '+nodes.item(i).length);
        }
    }
}
c.open('GET', 'http://mindelusions.com/test/test.xml');
c.send();
and I get the following output:
[INFO] In row 0: ID
 
[INFO] In row 0: SECTION
 
[INFO] In row 0: SECTION_TITLE
 
[INFO] In row 0: MANDATORY_NUM
 
[INFO] In row 0: DISPLAY
 
[INFO] In row 0: CHECKABLE
 
[INFO] In row 0: REQUIREMENT

Your Answer

Think you can help? Login to answer this question!