I am displaying an XML document in an HTML file using JavaScript. So far everything is displaying well, but I want to display the second level of XML tags for <date>
My XML snippet looks like this:
<date>
<dow>Monday</dow>
<month>08</month>
<day>10</day>
<year>2011</year>
</date>
To display first level tags, I have been writing the following document write:
document.write(x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);
This however is not going to display <date>. I need to target the childNodes <dow>, <month>,<day>, and <year>. Can I tweak my current document.write to make this happen? I’m getting a bit stumped on how to direct this issue as I don’t want to rewrite my entire code when first level elements are displaying just fine.
Any help would be greatly appreciated. Thank you in advance for your time.
You can use
getElementsByTagNameon any DOMElement:Optimally, you should also be checking if the nodes exist before accessing them with
foo[0].http://jsfiddle.net/RT4Qr/