Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 01-04-2013, 03:50 PM   PM User | #2
Alex Vincent
Moderator


 
Join Date: May 2002
Location: Hayward, CA
Posts: 1,428
Thanks: 1
Thanked 19 Times in 17 Posts
Alex Vincent is on a distinguished road
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.
__________________
"The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
June 30, 2001
author, Verbosio prototype XML Editor
author, JavaScript Developer's Dictionary
https://alexvincent.us/blog
Alex Vincent is offline   Reply With Quote
Old 01-04-2013, 05:40 PM   PM User | #3
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,495
Thanks: 18
Thanked 361 Times in 360 Posts
sunfighter is on a distinguished road
"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>
sunfighter is offline   Reply With Quote
Old 01-05-2013, 12:44 AM   PM User | #4
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
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.
juliushg is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:12 AM.


Advertisement
Log in to turn off these ads.