如,我在XmlConfigy文件夹下有一个有一个config.xml文件,内容如下:<?xml version="1.0" encoding="utf-8"?>
<Images>
<item>
<href>a.aspx?id=002</href>
<src>UpLoadFiles/52.jpg</src>
<alt>XXXXX</alt>
</item>
<item>
<href>b.aspx?id=235</href>
<src>UpLoadFiles/A.JPG</src>
<alt>zzzz</alt>
</item>
</Images>
在现在在根目录下有一个aspx文件,想在里面写一段javascript脚本,读取上面的XML文件
var href;
var src;
var alt; // 这里该怎么写
// 使其的结果是:
//
// href = 'a.aspx?id=002|b.aspx?id=235'
// src = 'UpLoadFiles/52.jpg|UpLoadFiles/A.JPG'
// alt = 'XXXXX|zzzz'
<Images>
<item>
<href>a.aspx?id=002</href>
<src>UpLoadFiles/52.jpg</src>
<alt>XXXXX</alt>
</item>
<item>
<href>b.aspx?id=235</href>
<src>UpLoadFiles/A.JPG</src>
<alt>zzzz</alt>
</item>
</Images>
在现在在根目录下有一个aspx文件,想在里面写一段javascript脚本,读取上面的XML文件
var href;
var src;
var alt; // 这里该怎么写
// 使其的结果是:
//
// href = 'a.aspx?id=002|b.aspx?id=235'
// src = 'UpLoadFiles/52.jpg|UpLoadFiles/A.JPG'
// alt = 'XXXXX|zzzz'
如果其中的item项总共超过6项,则取最后6项。
1.XML代码
<?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 代码
<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>