<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
读取服务器上xml内容
</title>
<Script Language="JavaScript">
function loadXML(fileName) { // 定义一个xml dom对象
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.load(fileName);
xmlObj = xmlDoc.documentElement; if(xmlObj.childNodes(0).getAttribute("id")==document.all.select1.options[select1.selectedIndex].text)
{
alert(xmlObj.childNodes(0).getAttribute("id"));
document.all.select2.options.add(new Option(xmlObj.childNodes(0).childNodes(0).text,document.all.select1.options[select1.selectedIndex].text));
}}
</Script>
</head>
<body>
<select name="select1" onChange="loadXML('parseXML.xml')">
<option>0001</option>
<option>0002</option>
<option>0003</option>
<option>0004</option>
</select>
<select name="select2"></select>
</body>
</html>

解决方案 »

  1.   

    <?xml version="1.0" encoding="gb2312"?>
    <root>
        <product id="0001" name="product1" price="10.00">
            <description>
                Detail Descriptionss
            </description>
        </product>
        <product id="0002" name="product2" price="20.00">
            <description>
                Detail Description
            </description>
        </product>
    </root>
      

  2.   

    谢谢 如果XML里有多个<description>
       Detail Descriptionss
    </description> 应该怎么用FOR 循环写
      

  3.   

    选择002的时候如何清空001里的内容
    for(var i= 0; i<xmlObj.childNodes.length;i++)
    if(xmlObj.childNodes(i).getAttribute("id")==document.all.select1.options[select1.selectedIndex].text)
    {
    alert(xmlObj.childNodes(i).getAttribute("id"));
    for(var j=0; j<xmlObj.childNodes.length;j++)
    {
    document.all.select2.options.add(new Option(xmlObj.childNodes(i).childNodes(j).text,document.all.select1.options[select1.selectedIndex].text));
    }

    }