I am trying to get the Image attributes from an XML file below.
<Vehicle id="4120505">
<DealerName>Unitrans Volkswagen Mokopane</DealerName>
<Images>
<Image Id="6060247" ThumbUrl="http://image.blob.ix.co.za/20x90.jpg"/>
</Images>
</Vehicle>
In my JavaScript i used the following:
var M = xmlDoc.getElementsByTagName("Images");
for (i = 0; i < M.length; i++)
{
for(var j = 0; j < M[i].childNodes.length; j++)
{
console.log("The value of x is " +
M[i].childNodes[j].getAttribute('ThumbUrl'));
}
}
I am getting an error in console. How best can i get this attribute from the xml data?
.childreninstead of.childNodes..childNodesincludesTextnodes which don’t have attributes. Also, this code can be shortened toArray.from(xmlDoc.querySelectorAll("Images > *"), (image) => image.getAttribute("ThumbUrl")).forEach((attr) => console.log("The value of x is " + attr));.