CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   XML (http://www.codingforums.com/forumdisplay.php?f=3)
-   -   Help with reading a XML file (http://www.codingforums.com/showthread.php?t=285261)

juliushg 01-04-2013 11:39 AM

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.

Alex Vincent 01-04-2013 03:50 PM

I think it might have to do with your XMLHttpRequest.open() call - the third argument set to true indicates asynchronous calls. Which means when you try to read it, you haven't got the full document yet.

Please don't try to convert to synchronous XMLHttpRequests - they're not very nice. Instead, look into event handlers that XHR's provide.

sunfighter 01-04-2013 05:40 PM

"http://www.myurl.com/control/xml/myproducts2.xml" does not yield the xml file. So I loaded the file into my main area where the file that will read it lives and just called it as a file. Here is some code to get you going.
Code:

<!DOCTYPE html>
<html>
<body>
<script>
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}

xml=loadXMLDoc("myproducts2.xml"); // EDIT THIS WHEN YOU MOVE THE XML FILE
path="/productos/item/nombre"
// code for IE
if (window.ActiveXObject)
{
var nodes=xml.selectNodes(path);
alert(nodes.length);
for (i=0;i<nodes.length;i++)
  {
  document.write(nodes[i].childNodes[0].nodeValue);
  document.write("<br>");
  }
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();

while (result)
  {
  document.write(result.childNodes[0].nodeValue);
  document.write("<br>");
  result=nodes.iterateNext();
  }
}
</script>

</body>
</html>


juliushg 01-05-2013 12:44 AM

Sunfighter, just a question: are you aware that myurl.com is a fictitious url? I'm not an experienced coder, so I'm going to try that code and let you know.


All times are GMT +1. The time now is 10:37 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.