Talon06
Talon06

Reputation: 1796

AJAX Get Node Values From XML Nested Node with For Loop

I have the following XML file and I am able to get the non nested values out with

xmlHttp.responseXML.getElementsByTagName('bio_fname')[0].firstChild.nodeValue

I want to get the nested values with something like a for each loop is this possible? Thanks

<bio>
    <bio_exists>True</bio_exists>
    <bio_fname>Test</bio_fname>
    <bio_lname>Tester</bio_lname>
    <bio_pos>
        <pos>
            <name>White Collar</name>
            <company>Foo</company>
            <rank>1</rank>
        </pos>
        <pos>
            <name>Blue Collar</name>
            <company>BAR</company>
            <rank>2</rank>
        </pos>
    </bio_pos> 
</bio>

Upvotes: 0

Views: 344

Answers (1)

Harsha Venkataramu
Harsha Venkataramu

Reputation: 2914

var nodes = xmlDoc.selectNodes("/bio/biopos");
var ids = [],names = [] , designations = [];
for ( var i = 0; i < nodes.length; i++) 
{
    ids.push(nodes[i].selectSingleNode("name").firstChild.nodeValue);
    names.push(nodes[i].selectSingleNode("company").firstChild.nodeValue);
    designations.push(nodes[i].selectSingleNode("rank").firstChild.nodeValue);
}

Upvotes: 1

Related Questions