2

I'm developing a cordova/phonegap app and I want to read an xml in order to show some data to user.

I found a way to get my xml file:

getXML.onclick=function(){
var url = 'http://....cloudfront.net/TestFile.xml';
var xmlhttp;
var x,xx,i;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      x=xmlhttp.response;
      alert(x); // Here it shows my xml file
      for (i=0;i<x.length;i++) {
         alert(x[i]); // This shows letter by letter my file
      }
    }
  }
xmlhttp.open("GET",url,true);
xmlhttp.send();

}

So, how should I parse my xml file?

Thanks

5
  • 2
    1) I think one can drop IE6/IE5 support nowadays 2) If you have control over the data: take JSON instead of XML. 3) Modern browsers have an XML parser on board, but that just gives you an XML Document - I guess that is not exactly what you need? 4) jQuery has a parseXML method you could use as well. Commented Jul 11, 2014 at 15:51
  • I don't have control over the data. How could I use jQuery to parse the xml? Commented Jul 11, 2014 at 16:28
  • May almighty google be with you: api.jquery.com/jquery.parsexml Commented Jul 11, 2014 at 16:29
  • 1
    God bless almighty google and bless you @Christoph. You don't know how much you helped me. ;) Commented Jul 11, 2014 at 17:00
  • Thank you & you're welcome! You can either accept Pablo's answer if it helped you or write an answer on your own how you solved the problem and then accept it, so the SO community knows your problem is solved. Either way is fine. Have a nice weekend. Commented Jul 11, 2014 at 17:07

1 Answer 1

2

You could use this in order to parse your xml from a string:

if (window.DOMParser) {
    parser=new DOMParser();
    xmlDoc=parser.parseFromString(txt,"text/xml");
}
else // Internet Explorer
{
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.loadXML(txt); 
}

Where txt is your xml string

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

Comments

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.