日期:2014-05-16  浏览次数:20410 次

Java乔晓松-js解析xml文件实现select二级联动

xml文档:

<?xml version="1.0" encoding="UTF-8"?>
<china>
<province name="请选择城市">
<city>请选择市区</city>
</province>
<province name="北京市">
<city>海淀区</city>
<city>朝阳区</city>
<city>大兴区</city>
<city>昌平区</city>
</province>
<province name="天津市">
<city>北辰区</city>
<city>和平区</city>
<city>虹桥区</city>
<city>AA区</city>
</province>
<province name="上海市">
<city>BB区</city>
<city>CC区</city>
<city>DD区</city>
<city>MM区</city>
</province>
</china>




js文件的原码:

window.onload = function() {
var xmlDoc = getXmlDocument("./js/citys.xml");
// alert(xmlDoc);
var china = xmlDoc.documentElement;


// alert(china);


var provincesXMLNode = china.getElementsByTagName("province");


var provinceHTMLNode = $("province");
var cityHTMLNode = $("city");
for ( var i = 0; i < provincesXMLNode.length; i++) {
var provinceXMLNode = provincesXMLNode[i];
var provinceXMLNodeName = provinceXMLNode.getAttribute("name");
var option = document.createElement("option");
option.setAttribute("value", provinceXMLNodeName);
var optionText = document.createTextNode(provinceXMLNodeName);


option.appendChild(optionText);
provinceHTMLNode.appendChild(option);
}
provinceHTMLNode.onchange = function() {
var cNodes = provinceHTMLNode.childNodes;
for(var i=0;i<cNodes.length;i++){
var optionselect = cNodes[i];
if(optionselect.nodeType==1){
if(optionselect.selected){
var proname = optionselect.getAttribute("value");
for(var k=0;k<provincesXMLNode.length;k++){
var proXML = provincesXMLNode[k];
var provalue =