不用递归也可以的
=====================================
<html>
<head>
<script language="JavaScript">
<!--
function xmlTest(){
var xx=dsoTest.selectNodes('property/items/item[@id=\'100\']//*');
var p=new Array();
for(var iCnt=0;iCnt<xx.length;iCnt++){
p[iCnt]=xx[iCnt].getAttribute('name');
}
alert(p.join(','));
}
-->
</script>
<title></title>
</head>
<body>
<xml id="dsoTest">
<property>
  <items>
    <item id="100" name="新疆">
        <item type="area" index="0" id="1" name="阿克苏">
            <item type="area" index="1" id="5" name="地区名"/>
        </item>
        <item type="area" index="2" id="2" name="奎屯"/>
        <item type="area" index="3" id="3" name="喀什"/>
        <item type="area" index="4" id="4" name="阿勒泰"/>
    </item>
  </items>
</property>
</xml>
<button onclick="JavaScript:xmlTest();">测试按钮</button>
</body>
</html>

解决方案 »

  1.   

    上面的方法能求出新疆"节点下的所有字节的id属性值,可是如果不是求新疆下的,可能要求阿克苏节点下的所有字节的id属性值,或者....,反正是可以变化的,var xx=dsoTest.selectNodes('property/items/item[@id=\'100\']//*')这句话该怎么改?
      

  2.   

    var strName='新疆';xx=dsoTest.selectNodes('property/items/item[@name=\''+strName+'\']//*')
      

  3.   

    XPATH里面就有选择所有后代节点的语法可以不用递归就能实现,干嘛一定要用递归呢?
      

  4.   

    function getChildById(id)
    {
    xmlDoc = new ActiveXObject("msxml2.DOMDocument");
    xmlDoc.async = false;
    if(!xmlDoc.loadXML(xml))
    throw "The format of remote server return is not an Xml";
    var nodeArr=xmlDoc.selectNodes('property/items/item[@id=\''+id+'\']//*');
    var array=new Array();
    for(var i=0;i<nodeArr.length;i++){
    array[i]=nodeArr[i].getAttribute('id');
    }
    alert(array.join(','));
    }
    getChildById(100);我想求出上面xml字符串中满足我传进去的ID的值的那个那个节点下的所有字节的id属性值存到一个数组
      

  5.   

    var strName='新疆';xx=dsoTest.selectNodes('property/items/item[@name=\''+strName+'\']//*')
    如果把新疆改成阿克苏xx=dsoTest.selectNodes('property/items/item[@name=\''+strName+'\']//*')
    就要改成xx=dsoTest.selectNodes('property/items/item/item[@name=\''+strName+'\']//*')才行啊
    我是想让这部分代码'property/items/item/item,也动态变化
      

  6.   

    xx=dsoTest.selectNodes(//item[@name=\''+strName+'\']//*')
    这样就可以了
      

  7.   

    xx=dsoTest.selectNodes('//item[@name=\''+strName+'\']//*')
      

  8.   

    可以了,谢谢了
    '//item[@name=\''+strName+'\']//*'具体什么意思?