Get Data From XML Content Using Javascript

Introduction
 
Often, we call web services that return data either in XML or JSON format. For JSON, we can easily get the data using JavaScript. But, what to do if we get it in XML format?

Well, we will discuss how to get data from XML using JavaScript and jQuery, in this blog.
 
Using Code

We will use the following XML data in our example. 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Events>  
  3.     <EventItem id="101">  
  4.         <Country>India</Country>  
  5.         <Phone Link="12345">Home</Phone>  
  6.     </EventItem>  
  7.     <EventItem id="102">  
  8.         <Country>USA</Country>  
  9.         <Phone Link="123">LandLine</Phone>  
  10.     </EventItem>  
  11. </Events>  
Using JavaScript

First, it checks for the availability of window.ActiveXObject object. This object is basically used for Internet Explorer (IE) browser. If it is IE, then it has different method (loadXML()) to parse the XML data. If it is non-IE, it creates DOM parser and calls the parseFromString() method for parsing the XML data.

Here is the code:
  1. var strXML = '<?xml version="1.0" encoding="utf-8"?><Events><EventItem id="101"><Country>India</Country><Phone Link="12345">homes</Phone></EventItem></Events>'
  2.  
  3. var doc;
  4. if(window.ActiveXObject)  
  5. {  
  6.     doc = new ActiveXObject('Microsoft.XMLDOM'); // For IE6, IE5  
  7.     doc.async = 'false';  
  8.     doc.loadXML(strXML);  
  9. }  
  10. else  
  11. {  
  12.     var parser = new DOMParser();  
  13.     doc = parser.parseFromString(strXML, 'text/xml'); // For Firefox, Chrome etc  
  14. }  
  15.   
  16. var x = doc.getElementsByTagName("EventItem");  
  17. for (i = 0;i < x.length; i++)  
  18. {  
  19.    alert(x[i].getElementsByTagName("Country")[0].childNodes[0].nodeValue); // India  
  20.    alert(x[i].getElementsByTagName("Phone")[0].getAttribute('Link') );// 12345  
  21. }  
Using jQuery

In jQuery, it uses parseXML() method to create a valid XML Document from a string. We can use find() method to get the data based on the node name. Additionally, if we have multiple node blocks, then it loops over to get the data for each node. In our example, we have multiple events. So, $.each() is required to get all the records.

If you want to use attribute value, then you can use attr(). Follow the below code:  
  1. <script type="text/javascript">  
  2. var strXML = '<?xml version="1.0" encoding="utf-8"?><Events><EventItem id="101"><Country>India</Country><Phone Link="12345">homes</Phone></EventItem></Events>';  
  3.       
  4. var doc = $.parseXML(strXML);  
  5. $eventItem = $(doc).find("EventItem");  
  6.   
  7. $eventItem.each(function(index, element) {   
  8.     alert("ID: " + element.attributes["id"].value);  
  9.     alert("Country: " + $(element).find('Country').text());  
  10.     alert("Phone: " + $(this).children('Phone').attr('Link'));  
  11. });  
  12.   
  13. $(doc).find('EventItem').each(function () {  
  14.     var id, cName, phoneNo;  
  15.       
  16.     id  = $(this).attr('id'); // 101  
  17.     cName = $(this).children('Country').text();   
  18.     phoneNo = $(this).children('Phone').attr('Link');  
  19.       
  20.     alert($(this).children('Phone[Link="12345"]').text());  
  21. });  
  22.   
  23. </script>  
Conclusion

In this article, we discussed how to get data from XML. Hope this helps you.
Ebook Download
View all
Learn
View all