Presented here is the content of an XML file named books.xml, which I am attempting to navigate:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Modified by XMLSpy® -->
<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>
I aim to access each book's data and extract all the information about them, but I'm encountering difficulties: Instead of the text node values, it consistently returns null. Here is the code snippet I've been using:
<html>
<head>
<script type="text/javascript" src="loadXML.js">
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
var x = xmlDoc.getElementsByTagName("book");
for(var i=0;i<x.length;i++)
{
var y = x[i].childNodes;
for(var j=0;j<y.length;j++)
{
document.write(y[j].nodeValue);
document.write("<br>");
}
document.write("<br>");
}
</script>
</body>
</html>