Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I try to read xml files but I have a problem ... my code is :

JavaScript
<script >
    function myFunction(xml) {
          var i;
          var parser, xmlDoc;
          parser = new DOMParser();
          xmlDoc = parser.parseFromString(xml.responseText,"text/xml");
          alert(xmlDoc);
           var x = xmlDoc.getElementsByTagName("book");
           alert(x.length);
           alert(x.parentNode.nodeName);  
        }
 function loadDoc() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
          myFunction(this);
      };
      xhttp.open("GET", "books.xml", true);
      xmlHttp.onreadystatechange = handleServerResponse;
      xhttp.send();
    }

</script>

my problem is when I run then

alert(xmlDoc); // print [object xmldoucument]
alert(x.length); // print 0
why alert(x.length); print zero ? what wrong ? I want to obtain the name of books ... but x.length is zero ... my xml file :

XML
<bookstore>
   <book category="cooking">
       <title lang="en">Everyday Italian</title>
       <author>Giada De Laurentiis</author>
       <year>2005</year>
       <price>30.00</price> </book>
   <book category="children">
       <title lang="en">Harry Potter</title>
       <author>J K. Rowling</author>
       <year>2005</year>
       <price>29.99</price> </book>
   <book category="web">
       <title lang="en">XQuery Kick Start</title>
       <author>James McGovern</author>
       <author>Per Bothner</author>
       <author>Kurt Cagle</author>
       <author>James Linn</author>
       <author>Vaidyanathan Nagarajan</author>
       <year>2003</year>
       <price>49.99</price> </book>
   <book category="web" cover="paperback">
       <title lang="en">Learning XML</title>
       <author>Erik T. Ray</author>
       <year>2003</year>
       <price>39.95</price> </book>
</bookstore>

and I put my xml file in my project here here i put the file under WEB_INF folder

so any help plz ?

What I have tried:

my code is above so any help plz ?
Posted
Updated 28-Sep-16 3:18am

If you want to check how many books are there, you can do something like following-
JavaScript
var bookXML = new XML(xmlDoc)
var books = bookXML['book'];
var length = books.length();


Note: I haven't worked much on XML suffs so there can be some syntax errors.
Please let me know, it it doesn't work.
:)
 
Share this answer
 
v2
Comments
Member 12702368 28-Sep-16 8:37am    
it did not work @Suvendu Shekhar Giri
I solved the problem ... I use xmlhttp instead of xhttp and it work
 
Share this answer
 

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