文章詳情頁(yè)
不同瀏覽器對(duì)XML的解析是不同的
瀏覽:107日期:2022-06-04 11:34:00
下面這段代碼在火狐能夠解析,在谷歌和IE就不行了,應(yīng)該如何來(lái)修改代碼?如何解析XML呢?
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>孟憲會(huì)的博客</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>
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>孟憲會(huì)的博客</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>
標(biāo)簽:
XML/RSS
上一條:關(guān)于html嵌入xml數(shù)據(jù)島如何穿過(guò)樹(shù)形結(jié)構(gòu)關(guān)系的問(wèn)題下一條:怎樣打開(kāi)XML文件?xml文件如何打開(kāi)?
相關(guān)文章:
1. 前端從瀏覽器的渲染到性能優(yōu)化2. 谷歌Chrome瀏覽器開(kāi)發(fā)者工具教程—JS調(diào)試篇3. Vue 禁用瀏覽器的前進(jìn)后退操作4. Python反爬蟲偽裝瀏覽器進(jìn)行爬蟲5. 如何使用瀏覽器擴(kuò)展篡改網(wǎng)頁(yè)中的JS 文件6. js開(kāi)發(fā)中的頁(yè)面、屏幕、瀏覽器的位置原理(高度寬度)說(shuō)明講解(附圖)7. JavaScript 判斷瀏覽器是否是IE8. 基于js實(shí)現(xiàn)判斷瀏覽器類型代碼實(shí)例9. vue使用lodop打印控件實(shí)現(xiàn)瀏覽器兼容打印的方法10. Vue項(xiàng)目頁(yè)面跳轉(zhuǎn)時(shí)瀏覽器窗口上方顯示進(jìn)度條功能
排行榜
