Home » Jquery » jquery – Reading attributes from an XML file using javascript

jquery – Reading attributes from an XML file using javascript

Posted by: admin February 22, 2020 Leave a comment

Questions:

I’m having a few issues reading the height and width attributes about an image that has been processed into individual tiles, from an xml file created during this process.

The contents of this file are as such:

<IMAGE_PROPERTIES WIDTH="61504" HEIGHT="25408" NUMTILES="32233" NUMIMAGES="1" VERSION="1.8" TILESIZE="256" />

To read the xml file I have used the following code:

<!-- language: lang-js -->

var xhr = new XMLHttpRequest();
xhr.open('GET', path, true);
xhr.timeout = 2000; 
xhr.onload = function () {
    var xmlDoc = this.responseXML;
    console.log(xmlDoc);
    var x = xmlDoc.getElementsByTagName("IMAGE_PROPERTIES")[0];
    console.log(x);
};

<!-- end snippet -->

How do I now access the height and width attributes?

How to&Answer:

Use the getAttribute function on the element (x in your case) that you found. See https://www.w3schools.com/xml/met_element_getattribute.asp