views:

48

answers:

3

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 realy appriciate your help :-)

A: 

I know this is a tired and cliche answer for questions like this, but consider using jQuery.

It makes pulling data via AJAX much nicer, and things like your second question much easier.

What's the status code returned when you try to access via absolute URL?

jeremiahd
thank you so much, i will try it :-)
andthenme
It really is much, much nicer than raw JS for most dom manipulation and AJAX tasks in most web(sites|apps).
jeremiahd
Thanks so much :-)
andthenme
A: 

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?

Answer: You're being tripped by the same domain security feature of browsers. The Ajax call must be to the same domain that the page itself was loaded from.

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.

Answer: First off, you want the equality test (==) not assignment (=).

Second, type coercion may work (but you might have to reverse the operands):

if (2.00 == valuereturned)
  {do sth}

But much better is to convert explicitly:

if (parseFloat(valuereturned) == 2.00)
  {do sth}

If you really want to compare to 2 (rather than a float 2.0), then you'll be safer (more reliable) comparing integers:

if (parseInt(valuereturned) === 2)
  {do sth}

If you stick with floats, you'll probably want to do some rounding since looking for exactly matching float values is usually not what you want.

Larry K
thank you so much Larry :-)
andthenme
A: 

Try this quick and dirty solution.

  function includeJS(filename, callback, responcedata){
        var page = document.getElementsByTagName('head')[0],
            js = document.createElement('script'),
            url =   "http://query.yahooapis.com/v1/public/yql?q="+
                "select%20*%20from%20xml%20where%20url%3D'"+encodeURIComponent(filename)+
                "'%20and%20columns%3D'question%2Canswer'&format='+responcedata'+&callback=?";

            js.setAttribute('type', 'text/javascript');
            js.setAttribute('src', url);
            page.appendChild(js);
    }

window.onload = includeJS;

Arguments:

      filename - the url of your xml file.
      responcedata - json or xml
      callback - a function that accepts your responcedata, json returns an object, xml returns xml.

YQL For more information.

Q_the_dreadlocked_ninja
thanx a lot, i am trying this :-)
andthenme
sorry but i have a question: since I am quite new in javascript, could you please tell me more clearly that where I have to add this function? do I replace it instead of the whole httprequest part ? and if I choose to receive an xml document can I use it just like the xmldoc in my example? and access the data in the same way like below ?xmlDoc.getElementsByTagName("dimention")[i].getAttribute("x")
andthenme