Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    Mexico
    Posts
    45
    Thanks
    9
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts
    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

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,710
    Thanks
    25
    Thanked 660 Times in 659 Posts
    "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>

  • #4
    New Coder
    Join Date
    Jul 2012
    Location
    Mexico
    Posts
    45
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

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