[JS] Javascript problemen
ik heb een paar problemen met Javascript.
Kunnen jullie me misschien helpen?
Mijn vraag:
Ik heb een code gebruikt om XML te parse.
Mijn code:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<script type="text/javascript">
function loadXMLDoc(XMLname)
//
{
var xmlDoc;
if (window.XMLHttpRequest)
{
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET",XMLname,false);
xmlDoc.send("");
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(XMLname);
return xmlDoc;
}
alert("Error loading document!");
return null;
}
</script>
function loadXMLDoc(XMLname)
//
{
var xmlDoc;
if (window.XMLHttpRequest)
{
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET",XMLname,false);
xmlDoc.send("");
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(XMLname);
return xmlDoc;
}
alert("Error loading document!");
return null;
}
</script>
Mijn body script:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
<script type="text/javascript">
xmlDoc=loadXMLDoc("ns.php?station=amersfoort");
var M = xmlDoc.getElementsByTagName("article");
for (i=0;i<M.length;i++){
document.write("<div style='width:450px;'>")
document.write("<h2>"+xmlDoc.getElementsByTagName("time")[i].childNodes[0].nodeValue+"</h2>");
document.write(""+xmlDoc.getElementsByTagName("besteming")[i].childNodes[0].nodeValue+" - <small>"+xmlDoc.getElementsByTagName("via")[i].childNodes[0].nodeValue+"</small>");
document.write("<img src='img/platform/"+xmlDoc.getElementsByTagName("spoor")[i].childNodes[0].nodeValue+".png' />");
document.write("</div>")
}
</script>
xmlDoc=loadXMLDoc("ns.php?station=amersfoort");
var M = xmlDoc.getElementsByTagName("article");
for (i=0;i<M.length;i++){
document.write("<div style='width:450px;'>")
document.write("<h2>"+xmlDoc.getElementsByTagName("time")[i].childNodes[0].nodeValue+"</h2>");
document.write(""+xmlDoc.getElementsByTagName("besteming")[i].childNodes[0].nodeValue+" - <small>"+xmlDoc.getElementsByTagName("via")[i].childNodes[0].nodeValue+"</small>");
document.write("<img src='img/platform/"+xmlDoc.getElementsByTagName("spoor")[i].childNodes[0].nodeValue+".png' />");
document.write("</div>")
}
</script>
Wat er nou fout gaat is dat als ik dit script test hij niet meer werkt als er een <naam></naam> niet is gevuld in de xml.
Hoe zorg ik dat hij dat stukje dan gewoon overslaat en wel zijn loopje af maakt?
Hier de XML maar soms zijn er geen <></> gevuld:
http://ns.bussenentreinenforum.nl/test/ns.php?station=amersfoort
Wie kan me helpen met deze vraag.
(ik gebruik dit topic om meerderen vragen te stellen vandaar de titel)
Alvast bedankt
Ps. er komen nog een paar anderen vragen aan
Toevoeging op 30/01/2013 20:41:24:
Zoals je ziet gaat het bij via fout...
Er zijn nog geen reacties op dit bericht.