I'm having trouble with nodeValue property.
No matter what..it gives me value null.
All other propeties are executing fine.

XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<details>
	<player>
		<name>MSchu</name>
		<poles>91</poles>
	</player>
	<player>
		<name>Alonso</name>
		<poles>25</poles>
	</player>
</details>

And I'm running this code:

<html>
<head>
	<script>
	function loadXMLDoc(){
		if (window.XMLHttpRequest){
			xhttp=new XMLHttpRequest();
		}
		else{
			xhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xhttp.open("GET","details.xml",false);
		xhttp.send();
		return xhttp.responseXML;
	}
	
	function show(){
		xmlDoc = loadXMLDoc();
		var x = xmlDoc.getElementsByTagName("player");
		alert(x[0].nodeName);		//This is printing player
		alert(x[0].firstChild.nodeName)	//This is printing name
		y=x[0].firstChild;
		alert(y.nodeValue);		//This is printing null
	}
	</script>
</head>
<body>
	<script>
		show();
	</script>
</body>
</html>

Recommended Answers

All 3 Replies

Raul8,

You may already know this but false in xhttp.open("GET","details.xml",false); means that you are trying to handle the response synchronously, which will not work in most browsers (IIRC, only IE will handle synchronous requests).

I'm not exactly experienced at handling XML in javascript but here's my best guesses ....

1. alert(y.nodeType); . If it's not 3 (TEXT_NODE) then go one level deeper alert(y.firstChild.nodeValue); .

2. If that doesn't work, then try alert(y.nodeValue || y.text); .

Airshow

Raul8,

You may already know this but false in xhttp.open("GET","details.xml",false); means that you are trying to handle the response synchronously, which will not work in most browsers (IIRC, only IE will handle synchronous requests).

I'm not exactly experienced at handling XML in javascript but here's my best guesses ....

1. alert(y.nodeType); . If it's not 3 (TEXT_NODE) then go one level deeper alert(y.firstChild.nodeValue); .

2. If that doesn't work, then try alert(y.nodeValue || y.text); .

Airshow

Hi ha ha ha
It Worked
But weired....point 1 & 2 both worked..but how?...tell me if u know how

Thanks

Raul,

I'm learning here myself. It would appear that .....

For XML <name>xxxx</name> , "xxxx" is not the nodeValue of <name>. Instead, <name> contains a text node, with the nodeValue "xxxx".

Also, <name>.text (pseudocode) gives direct access to the <name>'s text, bypassing the need to find the text node itself.

My point 2 can therefore be simplified to alert(y.text); .

Airshow

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.