Click here to Skip to main content
15,919,028 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
I encountered a problem, the following code can run properly in Microsoft IE browser, but can not run on Google Chrome browser.

XML document structure:
XML
<?xml version="1.0"?>
<bookstore>
  <book>
     <author>name1</author>
     <bookname>C# Programming</bookname>
     <price>$50</price>
  </book>
  <book>
     <author>name2</author>
     <bookname>C++ Programming</bookname>
     <price>$60</price>
  </book>
  <book>
     <author>name3</author>
     <bookname>Java Programming</bookname>
     <price>$60</price>
  </book>
</bookstore>


javascript codes:
<script type="text/javascript">
      function getXMLValue() {

          if (window.XMLHttpRequest) {
              xmlhttp = new XMLHttpRequest();
          }
          else {
              xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          }

          xmlhttp.open("GET", "books.xml", false);
          xmlhttp.send(null);
          xmlDoc = xmlhttp.responseXML;

          var bNumber = xmlDoc.getElementsByTagName("book");
          for (var i = 0; i < bNumber.length; i++) {
              for (var j = 0; j < 3; j++) {
                  // Visit belong to all child nodes  of 'book' .
                 window.alert(xmlDoc.getElementsByTagName("book")[i].childNodes[j].childNodes[0].nodeValue);
              }
          }

      }

  </script>


Google Chrome prompt:Uncaught TypeError: Cannot read property 'nodeValue' of undefined
Thanks.
Posted
Updated 26-May-14 4:17am
v5
Comments
Did you debug and see what is the exact issue?
Ken H123 26-May-14 10:02am    
Uncaught TypeError: Cannot read property 'nodeValue' of undefined

I cant see how it works in IE!
Your XML has a two level hierarchy...book->author/price.
xmlDoc.getElementsByTagName("book")[i] gets the book level
and xmlDoc.getElementsByTagName("book")[i].childNodes[j] gets the author/price level.
So xmlDoc.getElementsByTagName("book")[i].childNodes[j].childNodes[0] should be undefined...
Remove .childNodes[0]!!!
 
Share this answer
 
Comments
Ken H123 26-May-14 10:34am    
My problem has been updated.
You are getting issues because of cross-browsers differences. Very nice discussion is given here.

http://stackoverflow.com/questions/649614/xml-parsing-of-a-variable-string-in-javascript
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900