0

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.

1
  • Please have a look at stackoverflow.com/editing-help#code and format your code properly. edit: After your edit, your example doesn't contain XML anymore. Commented Oct 29, 2013 at 9:30

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I upvoted, however i suggest turning the if/else the other way around and check for window.DOMParser i.e. if (window.DOMParser){/*code*/}else{/*the ms stuff*/}
Thanks. While I searched over xpath things last time, I was a bit surprised how poor support is provided to xml stuff : whether we talk about xml AJAX handling (pb in IE), xml parsing, and even xpath selection, it looks like a lot of workarounds, and browser specific code is still to write each time... OK json is great, but xml is still around for a while, isn't it ?

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.