AJAX从带有For循环的XML嵌套节点获取节点值

时间:2013-12-26 17:42:21

标签: javascript jquery ajax

我有以下XML文件,我可以使用

获取非嵌套值
xmlHttp.responseXML.getElementsByTagName('bio_fname')[0].firstChild.nodeValue

我希望得到嵌套值类似于每个循环的东西这可能吗? 感谢

<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>

1 个答案:

答案 0 :(得分:1)

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);
}