Manas Kumar
Manas Kumar

Reputation: 2531

Get value from xml data using javascript

I am sending an ajax request to server and server gives data in xml format.

I am getting value from server and store it in a variable.

<pre>var strXML = '<?xml version="1.0" encoding="utf-8"?>
   <Events>
     <EventItem><Country>Hong Kong</Country></EventItem>
     <EventItem><Country>India</Country></EventItem>
   </Events>';
</pre>

How do I get no of all the information of EventItem using javascript.

Thanks.

Upvotes: 0

Views: 100

Answers (1)

Zzirconium
Zzirconium

Reputation: 471

You'll first to parse your string, turning into a real xml structure:

toXML = function(text){
    if(window.ActiveXObject)
    {
        var doc=new ActiveXObject('Microsoft.XMLDOM');
        doc.async='false';
        doc.loadXML(text);
    }
    else
    {
        var parser=new DOMParser();
        var doc=parser.parseFromString(text,'text/xml');
    }
    return doc;
}

[...]

var myXML = toXML(strXML);

Then you navigate through it. You'll find plenty of ways to do that there : http://www.w3schools.com/xml/xml_examples.asp

Upvotes: 1

Related Questions