0

I started learning AJAX recently and I must say that it is very interesting. But I have a little problem understanding why nodeValue is not returning any value. But when I change the nodeValue to nodeName I get the right result. I am currently accessing the individual elements manually because I want to understand how these elements are accessed. I only need the why this isn't working. The problem is on line 26.

Here is the code for my html page

<html>
<head>
	<title></title>
	<script language = "javascript" type = "text/javascript">
	function getXMLDATA()
	{
		var xml;
		if(window.XMLHttpRequest)
		{
			xml = new XMLHttpRequest();
		}
		else if(window.ActiveXObject)
		{
			xml = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else
		{
			alert("Your browser does not support ajax");
		}
		
		xml.onreadystatechange = function()
		{
			if(xml.readyState == 4 && xml.status == 200)
			{
				
				document.getElementById("status").innerHTML = xml.responseXML.childNodes[0].childNodes[0].childNodes[0].nodeValue;
			}
			else
			{	
				document.getElementById("status").innerHTML = "Loading...";
			}
		}
		
		xml.open("GET", "xml.php", true);
		xml.send(null);
	}
	</script>
</head>
<body>
<a href = "#" onmouseover = "getXMLDATA()">Move your mouse over me!</a>
<div id = "status"></div>
</body>
</html>

and here is the code for PHP script (xml.php)

<?php
	header("Content-type: text/xml");
	echo "<?xml version = '1.0' encoding = 'UTF-8'?>";
	echo "<person>";
		echo "<student id = 'SC/ITC/07/0022'>";
			echo "<firstname>Cosman</firstname>";
			echo "<lastname>Agbitor</lastname>";
			echo "<age>25</age>";
			echo "<occupation>Student</occupation>";
		echo "</student>";
		echo "<student id = 'SC/ITC/07/0013'>";
			echo "<firstname>Adolf</firstname>";
			echo "<lastname>Fenyi</lastname>";
			echo "<age>24</age>";
			echo "<occupation>Student</occupation>";
		echo "</student>";
		echo "<student id = 'SC/ITC/07/0011'>";
			echo "<firstname>Daniel</firstname>";
			echo "<lastname>Quasah</lastname>";
			echo "<age>30</age>";
			echo "<occupation>Electronics</occupation>";
		echo "</student>";
		echo "<student id = 'SC/ITC/07/0017'>";
			echo "<firstname>Gideon</firstname>";
			echo "<lastname>Norvor</lastname>";
			echo "<age>27</age>";
			echo "<occupation>Farmer</occupation>";
		echo "</student>";
		echo "<student id = 'SC/ITC/07/0025'>";
			echo "<firstname>Gideon</firstname>";
			echo "<lastname>Evans</lastname>";
			echo "<age>33</age>";
			echo "<occupation>Pragrammer</occupation>";
		echo "</student>";
	echo "</person>";

?>
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by cossay
0

Cossay,

To gain insight, you could try exploring the node names and values as you go down through the tree, eg like this:

...
		if(xml.readyState == 4 && xml.status == 200) {
			var txt = [];//array inwhich to accumulate text fragments
			txt.push(xml.responseXML.childNodes[0].nodeName + ' : ' + xml.responseXML.childNodes[0].nodeValue);
			txt.push(xml.responseXML.childNodes[0].childNodes[0].nodeName + ' : ' + xml.responseXML.childNodes[0].childNodes[0].nodeValue);
			txt.push(xml.responseXML.childNodes[0].childNodes[0].childNodes[0].nodeName + ' : ' + xml.responseXML.childNodes[0].childNodes[0].childNodes[0].nodeValue);
			document.getElementById("status").innerHTML = txt.join('<br/>');
		}
		...

Airshow

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.