文章詳情頁
不同瀏覽器對(duì)XML的解析是不同的
瀏覽:118日期:2022-06-04 11:34:00
下面這段代碼在火狐能夠解析,在谷歌和IE就不行了,應(yīng)該如何來修改代碼?如何解析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)文章:
1. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法2. JavaScript Window瀏覽器對(duì)象模型原理解析3. XML解析錯(cuò)誤:未組織好 的解決辦法4. 基于Python模擬瀏覽器發(fā)送http請(qǐng)求5. 淺析IE瀏覽器關(guān)于ajax的緩存機(jī)制6. Python使用Pyqt5實(shí)現(xiàn)簡(jiǎn)易瀏覽器(最新版本測(cè)試過)7. java軟引用在瀏覽器使用實(shí)例講解8. python 獲取谷歌瀏覽器保存的密碼9. 詳解瀏覽器的緩存機(jī)制10. Python反爬蟲偽裝瀏覽器進(jìn)行爬蟲
排行榜
