当前位置:编程学习 > JS >>

js解析xml文件 (兼职firefox ,ie7)

文章提供一款js解析xml文件 哦,很简单就利用了js microsoft.xmldom获取xml再解析,下面这款解析实例兼容ie,ff浏览吕

xml文件

<?xml version="1.0" encoding="gb2312" ?>
<root>
<citymessage>
<date>2008年4月29日</date>
<city>胡志明市</city>
<url>http://down.zzzyk.com/cn/journey/hochiminhcity/</url>
</citymessage>
<citymessage>
<date>2008年4月30日</date>
<city>香港</city>
<url>http://www.zzzyk.com/cn/journey/hongkong/</url>
</citymessage>
</root>

js文件

<script language="网页特效" type="text/javascript">
var xmldoc;
function showcurcity()
{
if(window.activexobject)
{
xmldoc=new activexobject("microsoft.xmldom");
xmldoc.async="false"
xmldoc.load("olycity.xml");
getvalue();
}
else if(document.implementation && document.implementation.createdocument)
{
xmldoc=document.implementation.createdocument("", "root", null);
xmldoc.load('olycity.xml');
xmldoc.onload=getvalueff;
}
else
{
return null;
}
}
function getvalue()
{
var nodes=xmldoc.documentelement.childnodes;
var now=new date();
for(i=0;i<nodes.length;i++){
var value=nodes.item(i).childnodes.item(0).text ;
if(value==now.tolocaledatestring())
{
window.document.getelementbyid('curcity').innertext=nodes.item(i).childnodes.item(1).text;
window.document.getelementbyid('curcity').href =nodes.item(i).childnodes.item(2).text;
}
}
}
function getvalueff(){
var ccode=xmldoc.getelementsbytagname("date");
var now = new date();
var nowvalue=now.getfullyear()+'年'+(now.getmonth()+1)+'月'+now.getdate()+'日';
for(i=0;i<ccode.length;i++)
{
var value=ccode[i].childnodes[0].nodevalue;
if(value==nowvalue)
{
window.document.getelementbyid('curcity').href =xmldoc.getelementsbytagname("url")[i].childnodes[0].nodevalue;;
window.document.getelementbyid('curcity').childnodes[0].nodevalue=xmldoc.getelementsbytagname("city")[i].childnodes[0].nodevalue;
}
}
}
setinterval(showcurcity,1000);
</script>

补充:网页制作,js教程 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,