getAttribute
JavaScript XML
We can get value of the attributes used in xml file by using getAttribute of JavaScript parser. We have used XMLHttpRequest and we have used our sample xml file here.
The code is here
<script type="text/javascript">
if (window.XMLHttpRequest)
{ my_xml=new XMLHttpRequest();
}
else
{
my_xml=new ActiveXObject("Microsoft.XMLHTTP");
}
/////////////////////////////
my_xml.open("GET",'../php_tutorial/xml-sample3.xml',false);
my_xml.send();
xml_str=my_xml.responseXML;
var x=xml_str.getElementsByTagName("address"); // 1
document.write(x[0].getAttribute('type'));
</script>
To display all the attributes we have to use for loop.
for(i=0;i<x.length;i++){
document.write(x[i].getAttribute('type')); // working
document.write('<br>');
}
Here is the demo ( view source ) ro read attributes
← XML
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
plus2net.com