SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Access an XML File from a Server

    Hi,

    I am trying to access an XML file from a server in my JavaScript code.
    I have an XML file like this:

    -<stream version="1.2">
    -<room id="simulator" time="128168557915">
    -<dimention id=0 x="1.25" y="2.00">
    <m mcu="160" sid="75">
    </dimention>
    </room>
    -<room id="simulator" time="128168557928">
    -<dimention id=0 x="1.95" y="1.86">
    <m mcu="160" sid="55">
    </dimention>
    </room>
    </stream>
    this file is generated by an application and I can access it from a URL ( since I am using the simulator for this application the XML is accessible from http://localhost:8081/feed/demo)
    This xml file is updated every few seconds and constantly growing.
    I have a javascript code which I've added the following code to it in order to use the data from XML file:

    <script type="text/javascript">
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET","http://localhost:8081/feed/demo",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;
    document.write("<table border='1'>");
    var x=xmlDoc.getElementsByTagName("room");
    for (i=0;i<x.length;i++)
    {
    document.write("<tr><td>");
    document.write(xmlDoc.getElementsByTagName("dimention")[i].getAttribute("x"));
    document.write("</td><td>");
    }
    document.write("</table>");

    </script>

    Now here comes my problem: if I have the XML file saved on same drive as html page and I address it like this:
    xmlhttp.open("GET","floor.xml",false);
    it works fine, but when I pass the URL it doesn't. is there anything else I should do in case of loading the xml from URL?

    my second question is that I want to use the text values returned by xmlDoc.getElementsByTagName("dimention")[i].getAttribute("x") in an if statement like this :
    if (valuereturned = 2.00)
    {
    do sth
    }
    what is the best way to do that, since the returned value is a text.

    I need the answer ASAP and I really appreciate your help, thanks :-)

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried to test it on a live server? If I remember correctly, there is an issue that occurs when working locally.

    As for your second question, you can do:

    Code:
    var value = parseInt(xmlDoc.getElementsByTagName("dimention")[i].getAttribute("x"));
    
    if (value == 2.0) {
      //etc...
    }
    Follow me @YaderHernandez

  3. #3
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much, I havent tried it on a live server. It was only locally, but now you said so, I will try it on a live server.

    And thanx for the answer for the second part :-)

  4. #4
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Are you loading the file from the same host and port?

    If you're not, you won't be able to load it like that (xmlhttprequest enforces a same-origin policy).

  5. #5
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the hint. No I am loading it from different host and port. But now you said so, I will test it from the same ones. Thanks :-)


Bookmarks

Posting Permissions

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