我从XML
文件中获取数据并将其显示到HTML
文件中XML
文件包含节点的子子子节点我的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<projects>
<project title="title1" subtitle="subtitle1">
<text>First Tesxt</text>
<images>
<image url="1/176.png"></image>
<image url="1/178.png"></image>
<image url="1/173.png"></image>
</images>
</project>
<project title="title2" subtitle="subtitle2">
<text>Second Test</text>
<images>
<image url="2/164.png"></image>
<image url="2/175.png"></image>
<image url="2/1767.png"></image>
</images>
</project>
HTML Javascript 部分是
<html>
<head>
</head>
<body>
<script>
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send();
return xhttp.responseXML;
}
xmlDoc=loadXMLDoc("projects.xml");
var length = xmlDoc.getElementsByTagName("project").length;
for(i=0; i<length;i++)
{
alert(xmlDoc.getElementsByTagName("project")[i].getAttribute("title"));
alert(xmlDoc.getElementsByTagName("project")[i].getAttribute("subtitle"));
alert(xmlDoc.getElementsByTagName("text")[i].childNodes[0].nodeValue);
var lengthImage = xmlDoc.getElementsByTagName("images")[i].childNodes[0].length;
for(var j=0; j<lengthImage; j++)
{
alert(xmlDoc.getElementsByTagName("image")[j].getAttribute("url"));
}
}
</script>
</body>
</html>
但是图像节点无法正确显示它总是迭代 4 次loop