不同瀏覽器對XML的解析是不同的
更新時間:2012年12月10日 16:30:27 作者:
下面這段代碼在火狐能夠解析,在谷歌和IE就不行了,應(yīng)該如何來修改代碼?如何解析XML呢
下面這段代碼在火狐能夠解析,在谷歌和IE就不行了,應(yīng)該如何來修改代碼?如何解析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章的網(wǎng)站</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("返回的數(shù)據(jù)不正確。");
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>網(wǎng)址</td>
</tr>
</table>
</body>
</html>
JScript code:
復(fù)制代碼 代碼如下:
$("#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:
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<note>
<t1>
<title>孟子E章的網(wǎng)站</title>
<url>http://dotnet.aspx.cc/</url>
</t1>
<t1>
<title>孟憲會的博客</title>
<url>http://blog.csdn.net/net_lover/</url>
</t1>
</note>
HTML 代碼
HTML code:
復(fù)制代碼 代碼如下:
<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("返回的數(shù)據(jù)不正確。");
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>網(wǎng)址</td>
</tr>
</table>
</body>
</html>
相關(guān)文章
關(guān)聯(lián)數(shù)據(jù)入門——RDF應(yīng)用
語義網(wǎng)也不僅僅是把數(shù)據(jù)放在互聯(lián)網(wǎng)上,而是試圖將數(shù)據(jù)聯(lián)系起來,并產(chǎn)生數(shù)據(jù)與現(xiàn)實事物的聯(lián)系,以方便人與機器閱讀與理解這些數(shù)據(jù),感興趣的朋友可以了解下2013-02-02怎樣才能用js生成xmldom對象,并且在firefox中也實現(xiàn)xml數(shù)據(jù)島?
怎樣才能用js生成xmldom對象,并且在firefox中也實現(xiàn)xml數(shù)據(jù)島?...2006-12-12XML指南——XML 瀏覽器(Netscape、Explorer)
XML指南——XML 瀏覽器(Netscape、Explorer)...2006-09-09