We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,654 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

XML using DOM

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>
2
Contributors
1
Reply
1 Day
Discussion Span
2 Years Ago
Last Updated
2
Views
vijiraghs
Light Poster
30 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

tinymark
Junior Poster
158 posts since Feb 2010
Reputation Points: 11
Solved Threads: 17
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0810 seconds using 2.69MB