1.11M Members

XML using DOM

 
0
 

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>
 
0
 

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.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: