字符串“AbfahrtszeitIst”无法显示在HTML文档中。当我使用其他字符串时没有问题。我不明白为什么,因为在XML Document标签“Haltepositionen”中所有的蜇都在工作,但字符串“AbfahrtszeitIst”没有。
<html>
<head>
<meta name="robots" content="noindex">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
</head>
<body onload="loadXMLDoc()">
<script>
function loadXMLDoc() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xmlhttp.open("GET", "https://start.vag.de/dm/api/v1/fahrten.xml/Bus/2049872", true);
xmlhttp.send();
}
function myFunction(xml) {
var i;
var xmlDoc = xml.responseXML;
var table="<tr><th style='text-align:left'></th><th></th></tr>";
var x = xmlDoc.getElementsByTagName("Halteposition");
var c = xmlDoc.getElementsByTagName("FahrtResponse");
var t = xmlDoc.getElementsByTagName("Fahrtverlauf");
for (i = 0; i <x.length; i++) {
table += "<tr><td>" +
x[i].getElementsByTagName("Haltestellenname")[0].childNodes[0].nodeValue +
"</td><td>" +
"<a target='_blank' href='https://www.google.com/maps?q=" + x[i].getElementsByTagName("Latitude")[0].childNodes[0].nodeValue + "," + x[i].getElementsByTagName("Longitude")[0].childNodes[0].nodeValue + "'><grau>Karte</a>" +
"</td><td>" + //PROBLEM:
x[i].getElementsByTagName("AbfahrtszeitIst")[0].childNodes[0].nodeValue +
"</td></tr>";
}
for (v = 0; v <c.length; v++) {
document.getElementById("fahrten-titel").innerHTML = c[v].getElementsByTagName("Linienname")[0].childNodes[0].nodeValue + " " + c[v].getElementsByTagName("Richtungstext")[0].childNodes[0].nodeValue;
}
for (m = 0; m <t.length; m++) {
var startzeit_hhmm = new Date(t[m].getElementsByTagName("AbfahrtszeitIst")[0].childNodes[0].nodeValue).toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
document.getElementById("startzeit").innerHTML = "Start um: " + startzeit_hhmm + " Uhr";
}
document.getElementById("fahrten-tabelle").innerHTML = table;
document.getElementById("fahrzeug").innerHTML = "Bus: " + (parameter_fnummer) ;
}
</script>
<div id="fahrten-table"></div>
</body>
</html>
答案 0 :(得分:0)
因为最后一个节点AbfahrtszeitIst
没有名称为Halteposition
的节点。您可以添加如下所示的支票:
for (i = 0; i <x.length; i++)
{
//Added line
var abfahrtszeitistvalue = x[i].getElementsByTagName("AbfahrtszeitIst")[0] ? x[i].getElementsByTagName("AbfahrtszeitIst")[0].childNodes[0].nodeValue : "";
table += "<tr><td>" +
x[i].getElementsByTagName("Haltestellenname")[0].childNodes[0].nodeValue +
"</td><td>" +
"<a target='_blank' href='https://www.google.com/maps?q=" + x[i].getElementsByTagName("Latitude")[0].childNodes[0].nodeValue + "," + x[i].getElementsByTagName("Longitude")[0].childNodes[0].nodeValue + "'><grau>Karte</a>" +
"</td><td>" + //PROBLEM:
abfahrtszeitistvalue +
"</td></tr>";
}