Member Avatar for vijiraghs

I have an XML file that i am parsing using DOM. I dont know why my code is not working.

<html>
<head>
	<script type="text/javascript">
		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;
		}
	</script>
</head>
<body style="color:red">
	<script type="text/javascript">
		xmldoc=loadXMLDoc("food.xml");
		document.write(xmldoc.getElementsByTagName("name")[0].childNodes[0].nodeValue);
	</script>
</body>
</html>

The following is the "food.xml" file

<?xml version="1.0" encoding="ISO-8859-1"?>

<breakfast_menu>
	<food>
		<name>Belgian Waffles</name>
		<price>$5.95</price>
		<description>two glasses maple syrup</description>
		<calories>650</calories>
	</food>
        <food>
		<name>Strawberry Belgian Waffles</name>
		<price>$7.95</price>
		<description>light Belgian waffles cream</description>
		<calories>900</calories>
	</food>
</breakfast_menu>

Try this replacement.

<script type="text/javascript">
		xmldoc=loadXMLDoc("food.xml");
		document.write(xmldoc.childNodes[0].children[0].children[0].childNodes[0].nodeValue);
	</script>

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.