js 操作xml文档
<script type="text/javascript">
<!--
var xml='<province><city id="520100" name="贵阳" /><city id="520200" name="六盘水" /><city id="520300" name="遵义" /><city id="520400" name="安顺" /><city id="522200" name="铜仁" /><city id="522300" name="黔西南" /><city id="522400" name="毕节" /><city id="522600" name="黔东南" /><city id="522700" name="黔南" /></province>';
function getxml(){
var drp2 = document.getElementById("DropDownList2");
function RemoveAll(oElem) { //清除DropDownList2的所有项
var i = 0;
for (i = oElem.length; i >= 0; i--){
oElem.options.remove(i);
}
}
RemoveAll(drp2)
var oDoc = new ActiveXObject("MSXML2.DOMDocument");
oDoc.loadXML(xml);
items1 = oDoc.selectNodes("//province/city");
var itemsLength=items1.length;
alert(itemsLength);
for(i=0;i<itemsLength;i++)
{
//alert(items1.GetAttributeNode("name").NodeValue);
var newOption = document.createElement("OPTION");
newOption.text=items1.GetAttributeNode("id").NodeValue;
newOption.value= items1.GetAttributeNode("name").NodeValue;
drp2.options.add(newOption);
}
}
getxml();
//-->
</script>
<select name="DropDownList2" id="DropDownList2"></select>