0

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?

4
  • 4
    i am getting an error in console and what is that error? Commented May 21, 2018 at 10:06
  • Use .children instead of .childNodes. .childNodes includes Text nodes which don’t have attributes. Also, this code can be shortened to Array.from(xmlDoc.querySelectorAll("Images > *"), (image) => image.getAttribute("ThumbUrl")).forEach((attr) => console.log("The value of x is " + attr));. Commented May 21, 2018 at 10:11
  • Possible duplicate of JavaScript getAttribute not working Commented May 21, 2018 at 10:17
  • Thank you. It now works. Thanks a lot Commented May 23, 2018 at 7:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.