I know there are loads of examples online, but there is one particularity in my XML which is posing some problems. In fact, I need to loop through my XML and read the Tagname and the text for cars. Issue is that the tag name is different for all elements;
<Vehicles>
<cars>
<Toyota> Test 1 </Toyota>
<BMW> Test 2 </BMW>
<VW> Test 3 </VW>
<Ferrari> Test 4 </Ferrari>
</cars>
</Vehicles>
What I have here will loop through the Cars but will return only Toyota. How do I read the rest, since the tagnames are all different;
function myFunction(xml) {
var x, y, z, i, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.documentElement;
y = xmlDoc.getElementsByTagName("cars")[0];
z = xmlDoc.getElementsByTagName("Toyota");
// Output
for (i = 0; i < z.length; i++) {
txt += z[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("main").innerHTML = txt;
}
Any idea how I can loop through all the children of cars, without having to mention the tag name, and return me both the tagname itself and the text?
xmlDoc.getElementsByTagName("cars")[0].childrenis a collection of nodes that are children ofcars