文章詳情頁
不同瀏覽器對XML的解析是不同的
瀏覽:145日期: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>孟憲會的博客</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>孟憲會的博客</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. XML解析錯誤:未組織好 的解決辦法3. Python反爬蟲偽裝瀏覽器進(jìn)行爬蟲4. JS跨瀏覽器解析XML應(yīng)用過程詳解5. 詳解瀏覽器的緩存機(jī)制6. vue監(jiān)聽瀏覽器原生返回按鈕,進(jìn)行路由轉(zhuǎn)跳操作7. selenium+python配置chrome瀏覽器的選項的實現(xiàn)8. PHP擴(kuò)展之XML操作(二)——XML解析器安裝及概述9. 使用css實現(xiàn)全兼容瀏覽器的三角形10. 一個用xslt樣式將xml解析為xhtml的類TransformBinder(兼容FF和IE7.0)
排行榜
