xml解析读取xml文件中的内容.docx
- 文档编号:11156670
- 上传时间:2023-02-25
- 格式:DOCX
- 页数:8
- 大小:16.42KB
xml解析读取xml文件中的内容.docx
《xml解析读取xml文件中的内容.docx》由会员分享,可在线阅读,更多相关《xml解析读取xml文件中的内容.docx(8页珍藏版)》请在冰豆网上搜索。
xml解析读取xml文件中的内容
1.xmlFile/province.xml
110000
120000
130000
140000
150000
210000
2.xmlFile/city.xml
110100
120100
130100
130200
130300
130400
130500
130600
130700
130800
130900
131000
131100
140100
140200
140300
140400
140500
140600
142200
142300
142400
142600
142700
150100
150200
150300
150400
152100
152200
152300
152500
152600
152700
152800
152900
150500
210100
210200
210300
210400
210500
210600
210700
210800
210900
211000
211100
211200
211300
211400
3.index.jsp
ProvCity.setProvince(this);"> ProvCity.setCities(this);">
4.index.js
$(document).ready(function(){
//初始化默认值
ProvCity.getProvinces();
});
//xmlFile是xml文件的地址
varProvCity={
loadXmlFile:
function(xmlFile)
{
varxmlDom=null;
if(window.ActiveXObject)//IE浏览器中读取xml文件
{
xmlDom=newActiveXObject("Microsoft.XMLDOM");
xmlDom.async="false";
xmlDom.load(xmlFile);
}
elseif(document.implementation&&document.implementation.createDocument)
{
//Firefox,Chrome浏览器中读取xml文件,Chrome需要开服务器才能访问
varxmlhttp=newwindow.XMLHttpRequest();
xmlhttp.open("GET",xmlFile,false);
xmlhttp.send(null);
xmlDom=xmlhttp.responseXML;
}
else
{
xmlDom=null;
}
returnxmlDom;
},
//好了读取出来了xml我们就要获取的他的数据了
getProvinces:
function()//初始化省数据
{
//生成关键词链接
varxdoc=ProvCity.loadXmlFile("xmlFile/province.xml");//xml文件路径
//varlist=xdoc.selectNodes("//keys")//只能在Ie浏览器中获取
varlist=xdoc.getElementsByTagName("Province");//读取所有节点值
varhtmlContent="";
for(vari=0;i varprovinces=list[i].firstChild.nodeValue;//得到该结点中的内容 varprovincesNode=provinces.split(": ");//以": "做为分隔,分成多个字符串数组 htmlContent=htmlContent+" //将所有结点全部存放到该变量中 } $("#prov").html(htmlContent);//将结点加到select下 ProvCity.getCities(list[0].firstChild.nodeValue.split(": ")[1]);//根据省得到市,默认时为第一个 }, setProvince: function(obj){//改变省时,将选中的省设为被选中状态 varpro=$(obj).val(); $("#provoption").removeAttr("selected");//先去除属性selected $("#provoption[value="+pro+"]").attr("selected","selected");//将选中的结点设置属性selected ProvCity.getCities(pro); }, //根据省得到市 getCities: function(pro){ varxdoc=ProvCity.loadXmlFile("xmlFile/city.xml"); varlist=xdoc.getElementById(pro);//得到id值为pro的结点 varcityList=list.getElementsByTagName("city");//获取id值为pro结点的子结点(名为city的结点) varhtmlContent=""; for(vari=0;i varcities=cityList[i].firstChild.nodeValue; varcityNode=cities.split(": "); htmlContent=htmlContent+" } $("#city").html(htmlContent); }, //改变市时,将选中的市设为被选中状态 setCities: function(obj){ varcity=$(obj).val(); $("#cityoption").removeAttr("selected"); $("#cityoption[value="+city+"]").attr("selected","selected"); } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- xml 解析 读取 文件 中的 内容