Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Reading XML from URL using Javascript

    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? I've been stuck on this for a while so any help is appreciated!

    Thanks in advance!

    Code:
    <body>
    <div id="container" style="background-color:yellow"></div>
    
    <script>
    
    
    //load xml file
    
    var url = "http://api.wolframalpha.com/v2/query?input=distance%20from%20london%20to%20california&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 && 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<msgobj.childNodes.length;i++){
    if ((msgobj.childNodes[i].nodeType == 3)&&
    (!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
    }
    </script>
    
    </body>

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Is the only thing you want to know the distance from London to California? Because there are a whole bunch of easier ways to do that than what you are doing...

  • #3
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    Is the only thing you want to know the distance from London to California? Because there are a whole bunch of easier ways to do that than what you are doing...
    This is just a sample of what I plan to use the API for, but it must in the same format.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    from what I am reading, the wolfram API does not accommodate AJAX requests because of the same-origin policy and you would need to use server-side scripting if you wanted to do this

  • #5
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    from what I am reading, the wolfram API does not accommodate AJAX requests because of the same-origin policy and you would need to use server-side scripting if you wanted to do this
    Thanks for your help. If this won't work, what do you recommend I do instead?

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Quote Originally Posted by TJ56 View Post
    Thanks for your help. If this won't work, what do you recommend I do instead?
    well, it depends what you want to do. If you are stuck on using wolfram, like I say - from what I can tell you would have to do it server side. If all you need is distances between towns there are plenty of other options.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •