I have an XML file with table tennis rules I want to parse to HTML with JavaScript. Heres the XML:
<rulesection>
<sectiontitle>A let</sectiontitle>
<rule>
<point>2.09.01</point>
<description>
The rally shall be a let
</description>
</rule>
<subrule>
<point>2.09.01.01</point>
<description>
if in service the ball, in passing over or around the net assembly, touches it, provided the service is otherwise correct or the ball is obstructed by the receiver or his or her partner;
</description>
</subrule>
</rule>
</rulesection>
I have some JavaScript code so I can get the sectiontitle, point and description. Unfortunately I can´t seem to get the subrules and Google wont help me with this. With the code I have now I only get the first rule of every rulesection.
<script>
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "fuckrules.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
var x = xmlDoc.getElementsByTagName("rulesection");
for (i = 0; i < x.length; i++) {
document.write("<br>");
document.write(x[i].getElementsByTagName("sectiontitle")[0].childNodes[0].nodeValue);
document.write("<br>");
document.write(x[i].getElementsByTagName("point")[0].childNodes[0].nodeValue);
document.write("<br>");
document.write(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
}
</script>
I have tried to use multiple for-loops but I still cant get the points and descriptions of the subrules inside a rule. If anyone could give me some pointers that would be really helpful :)
No comments:
Post a Comment