title: Reading XML from URL using Javascript
body: <p>I am writing code to retrieve some part of an xml document. I am using Wolfram API (I am registered and I have an AppID.). So if I save the xml file locally after I execute the search command (variable "url" below),it works perfecly (if I have xmlDoc.load("query2.xml") where the xml is savedd in query2.url). However, I want to put a url instead, and get them on the fly rather than save the xml. I tried xmlDoc.load(url) but that didn't work, and after researching I found a function that's supposed to help retrieve this xml data from a tutorial website, but that didn't work either (it doesnt display anything on the page). How can I get the xml given a url?</p>
  
  <p>Thanks in advance!</p>
  
  <pre><code>&lt;body&gt;
  &lt;div id="container" style="background-color:yellow"&gt;&lt;/div&gt;
  
  &lt;script&gt;
  
  
  //load xml file
  
  var url = "http://api.wolframalpha.com/v2/query?input=distance%20from%20london%20to%20california&amp;appid=xxx";
  if (window.ActiveXObject){
  var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false; //Enforce download of XML file first. IE only.
  }
  else if (document.implementation &amp;&amp; document.implementation.createDocument)
  var xmlDoc= document.implementation.createDocument("","doc",null);
  if (typeof xmlDoc!="undefined") {
  
  
  function loadXMLDoc(filename)
  {
  if (window.XMLHttpRequest)
    {
    xhttp=new XMLHttpRequest();
    }
  else // code for IE5 and IE6
    {
    xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  xhttp.open("GET",filename,false);
  xhttp.send();
  return xhttp.responseXML;
  } 
  xmlDoc.load(loadXMLDoc(url));
  
  //xmlDoc.load("query2.xml");
  
  }
  else {
  
  }
  
  //Regular expression used to match any non-whitespace character
  var notWhitespace = /\S/
  
  function getnumber(){
  //Cache "messages" element of xml file
  var msgobj=xmlDoc.getElementsByTagName("plaintext")[1]
  
  //REMOVE white spaces in XML file. Intended mainly for NS6/Mozilla
  for (i=0;i&lt;msgobj.childNodes.length;i++){
  if ((msgobj.childNodes[i].nodeType == 3)&amp;&amp;
  (!notWhitespace.test(msgobj.childNodes[i].nodeValue))) {
  // that is, if it's a whitespace text node
  msgobj.removeChild(msgobj.childNodes[i])
  i--
  }
  }
  
  //Get answer and display it in DIV:
  document.getElementById("container").innerHTML=
  xmlDoc.getElementsByTagName("plaintext")[1].childNodes[0].nodeValue
  }
  
  if (typeof xmlDoc!="undefined"){
  if (window.ActiveXObject) //if IE, simply execute script (due to async prop).
  getdaily()
  else //else if NS6, execute script when XML object has loaded
  xmlDoc.onload=getnumber
  }
  &lt;/script&gt;
  
  &lt;/body&gt;
  </code></pre>