>> CODE
var XmlReader = function(){
this.GetHttpRequest = function(){
if (window.XMLHttpRequest) // Gecko
return new XMLHttpRequest();
else if (window.ActiveXObject) // IE
return new ActiveXObject("MsXml2.XmlHttp") ;
}
this.LoadUrl = function(urlToCall, asyncFunctionPointer){
var oXmlReader = this;
var oXmlHttp = this.GetHttpRequest();
oXmlHttp.open("GET", urlToCall, false);
oXmlHttp.send(null) ;
if (oXmlHttp.status == 200) this.DOMDocument = oXmlHttp.responseXML ;
else alert("XML request error: " + oXmlHttp.statusText + " (" + oXmlHttp.status + ")") ;
}
this.SelectNodes = function(xpath){
if (document.all) // IE
return this.DOMDocument.selectNodes(xpath) ;
else{ // Gecko
var aNodeArray = new Array();
var xPathResult = this.DOMDocument.evaluate(xpath, this.DOMDocument, this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ;
if (xPathResult){
var oNode = xPathResult.iterateNext() ;
while(oNode){
aNodeArray[aNodeArray.length] = oNode ;
oNode = xPathResult.iterateNext();
}
}
return aNodeArray ;
}
}
}
function show(field,pid,selid){
var node = xmldoc.SelectNodes("//root/node[@pid="+pid+"]");
field.innerHTML = null; //清除原来的option
for(var i=0;i<node.length;++i) //增加省份名称到下拉列表
{
var oOption = document.createElement('OPTION');
oOption.text = node[i].childNodes[0].nodeValue;
oOption.value = node[i].getAttribute("id");
if(oOption.value==selid) oOption.selected="selected";
field.options.add(oOption);
}
}
xml文档:
>> CODE
<?xml version="1.0" encoding="gb2312"?>
<root name="data_city" describe="省市联动">
<node id="1" pid="0">江西</node>
<node id="2" pid="1">九江</node>
<node id="3" pid="1">南昌</node>
<node id="4" pid="1">庐山</node>
<node id="5" pid="1">景德镇</node>
<node id="6" pid="0">北京</node>
<node id="7" pid="6">北京西</node>
<node id="8" pid="6">居庸关</node>
<node id="9" pid="6">清华园</node>
<node id="10" pid="6">周口店</node>
<node id="11" pid="0">福建</node>
<node id="12" pid="11">福州</node>
<node id="13" pid="11">厦门</node>
<node id="14" pid="11">漳州</node>
<node id="15" pid="0">甘肃</node>
<node id="16" pid="15">兰州</node>
<node id="17" pid="15">洛门</node>
<node id="18" pid="15">嘉峪关</node>
<node id="19" pid="0">广东</node>
<node id="20" pid="19">广州</node>
<node id="21" pid="19">深圳</node>
<node id="22" pid="19">东莞</node>
<node id="23" pid="19">中山</node>
<node id="24" pid="0">安徽</node>
<node id="25" pid="24">合肥</node>
<node id="26" pid="24">黄山</node>
<node id="27" pid="24">九龙岗</node>
<node id="28" pid="24">马鞍山</node>
</root>


