下面这段代码在火狐能够解析,在谷歌和ie就不行了,应该如何来修改代码?如何解析xml呢
下面这段代码在火狐能够解析,在谷歌和ie就不行了,应该如何来修改代码?如何解析xml呢?
jscript code:
代码如下:
$(#result).append(xmldoc.firstchild.localname + <br/><hr>);
for ( var i = 0; i < xmldoc.documentelement.childelementcount; i++) {
$(#result).append(
xmldoc.documentelement.children[i].localname + <br/>);
}
兼容 ie、firefox、chrome、safari、opera 等浏览器的xml文件加载方式的代码如下,xml文件名为 1.xml。
xml代码
xml code:
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<note>
<t1>
<title>孟子e章的网站</title>
<url>http://dotnet.aspx.cc/</url>
</t1>
<t1>
<title>孟宪会的博客</title>
<url>http://blog.csdn.net/net_lover/</url>
</t1>
</note>
html 代码
html code:
代码如下:
<script type="text/javascript">
var xmldoc = null, xmlhttp = null;
function loadxml() {
xmlhttp = window.xmlhttprequest ? new window.xmlhttprequest() : new activexobject(microsoft.xmlhttp);
if (xmlhttp == null) {
alert(你的浏览器不支持 xmlhttprequest);
return;
}
xmlhttp.open(get, 1.xml? + date.parse(new date()), true);
xmlhttp.setrequestheader(content-type, text/xml);
xmlhttp.onreadystatechange = getmessage;
xmlhttp.send(null);
}
function getmessage() {
if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {
xmldoc = xmlhttp.responsexml.documentelement;
if (xmldoc == null) {
alert(返回的数据不正确。);
return;
}
var nodes = xmldoc.getelementsbytagname(t1)
tb = document.getelementbyid(table_note);
tbody = document.createelement(tbody)
for (i = 0; i < nodes.length; i++) {
tr = document.createelement("tr")
td = document.createelement("td")
td.innerhtml = nodes[i].getelementsbytagname("title")[0].childnodes[0].nodevalue
tr.appendchild(td)
td = document.createelement("td")
url = nodes[i].getelementsbytagname("url")[0].childnodes[0].nodevalue;
td.innerhtml = "<a href='" + url + "'> + url + </a>
tr.appendchild(td)
tbody.appendchild(tr)
}
tb.appendchild(tbody)
}
}
</script>
</head>
<body onload="loadxml()">
<table id="table_note" border="1">
<tr>
<td>姓名</td>
<td>网址</td>
</tr>
</table>
</body>
</html>
以上就是不同浏览器对xml的解析是不同的的详细内容。