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

    Access an XML File from a Server


    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">
    -<room id="simulator" time="128168557928">
    -<dimention id=0 x="1.95" y="1.86">
    <m mcu="160" sid="55">
    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();
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    document.write("<table border='1'>");
    var x=xmlDoc.getElementsByTagName("room");
    for (i=0;i<x.length;i++)


    Now here comes my problem: if I have the XML file saved on same drive as html page and I address it like this:
    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, thanx :-)

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,738 Times in 4,700 Posts
    If the page doing this is served up on the standard port 80, then the problem may be that the browser sees the use of port 8081 as an attempt to do cross-server access and so would deny access.

    If you would turn on all error reporting from JavaScript, you'd probably get a message that would tell you the reason.

    var valuereturned = parseFloat( xmlDoc.getElementsByTagName("dimention")[i].getAttribute("x") );
    if (valuereturned = 2.00)
        do sth
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


    Posting Permissions

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