在下面的 JavaScript 代码中,它适用于第一个循环,但对于第二个循环,它始终显示 y.length 等于 0 并且永远不会进入循环。y.length 不为零,我的 xml 文件有很多我的东西。我尝试调试但无法继续。任何帮助将不胜感激。
function myXML(myText, myTarget) {
try {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(myText);
} catch (e) {
try {
parser = new DOMParser();
xmlDoc = parser.parseFromString(myText, "text/xml");
} catch (e) {
alert(e.message);
return;
}
}
var x = xmlDoc.getElementsByTagName("myThing");
for (var i = 0; i < x.length; i++) {
if (x[i].getElementsByTagName("firstItem")[0].hasChildNodes) {
document.getElementById(target).innerHTML = document.getElementById(myTarget).innerHTML + x[i].getElementsByTagName("secondThing")[0].childNodes[0].nodeValue + " - " + x[i].getElementsByTagName("firstItem")[0].childNodes[0].nodeValue + "<br>";
var response = "";
var sid = x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
$.ajax({
type: "GET",
url: "../sitman/requests/get_affected_nodes.cgi?sid=" + sid,
async: false,
success: function(text) {
response = text;
}
});
try {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(response);
} catch (e) {
try {
parser = new DOMParser();
xmlDoc = parser.parseFromString(response, "text/xml");
} catch (e) {
alert(e.message);
return;
}
}
var y = xmlDoc.getElementsByTagName("myThing");
for (var j = 0; j < y.length; j++) {
document.getElementById(myTarget).innerHTML = document.getElementById(myTarget).innerHTML + y[j].getElementsByTagName("myname")[0].childNodes[0].nodeValue + "<br>";
}
}
}
}
y 的 XML 如下。
<xml>
<entries>
<myThing>
<myName>xxx</myName>
<test1>test8</test1>
<test2>test6</test2>
</myThing>
<myThing>
<myName>xxx</myName>
<test1>test2</test1>
<test2>test3</test2>
</myThing>
</entries>
</xml>