View Single Post
Old 01-04-2013, 11:39 AM   PM User | #1
juliushg
New Coder

 
Join Date: Jul 2012
Location: Mexico
Posts: 45
Thanks: 9
Thanked 0 Times in 0 Posts
juliushg is an unknown quantity at this point
Help with reading a XML file

I'm using javascript code (which I got from w3schools.com) to get elements from a xml file, but something goes wrong after the xml has been read. Please help me to find the error:

Code:
xmlhttp=new XMLHttpRequest();document.write("-1");

xmlhttp.open("GET","http://www.myurl.com/control/xml/myproducts2.xml",true);document.write("-2");
xmlhttp.send();document.write("-3");

xmlDoc=xmlhttp.responseXML;document.write("-4"+"<br/>");
document.write(xmlDoc);document.write("-5");

text=xmlDoc.getElementsByTagName("nombre")[0].childNodes[0].nodeValue;
document.write(text);document.write("-6");
Obviously I put the document.write statements like some sort of debugging. The problem in the code is that it does nothing after the -4 step, I tried to write the xmlDoc but appears "null".

My xml document is this:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<productos>
   <item>
      <nombre>Coca Cola 600 ml NR</nombre>
      <clave>360</clave>
      <costo>6.96</costo>
      <precio>10</precio>
   </item>
   <item>
      <nombre>Coca Cola Light 600 ml NR</nombre>
      <clave>389</clave>
      <costo>7</costo>
      <precio>10</precio>
   </item>
   <item>
      <nombre>Coca Cola 8 oz NR</nombre>
      <clave>452</clave>
      <costo>4.81</costo>
      <precio>6</precio>
   </item>
</productos>
The lines:

Code:
text=xmlDoc.getElementsByTagName("nombre")[0].childNodes[0].nodeValue;
document.write(text);
produce no output.

Sorry if this thread doesn't belong here but I didn't know if it goes in AJAX, XML or Javascript, please move it at will.
juliushg is offline   Reply With Quote