Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes HTML, CSS and JavaScript and the fly likes getting null from XML (AJAX) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "getting null from XML (AJAX)" Watch "getting null from XML (AJAX)" New topic

getting null from XML (AJAX)

achayya matta
Ranch Hand

Joined: Oct 27, 2005
Posts: 111
hello ranchers,

i have one servlet which returns some piece of xml.

response.setHeader("Cache-Control", "no-cache"); 0ut.write("<message><mes1>ashok</mes1><mes2>koti</mes2></message>");

The code i wrote in javascript is as follows

var message = req.responseXML.getElementsByTagName("message")[0];

alert(message.nodeName); <--ouput is : message -->
alert(message.nodeValue); <--output is : null -->
alert(message.hasChildNodes()); <--output is : true -->
alert(message.childNodes.length); <--output is : 2 -->

document.write(message.childNodes[0].nodeValue) <--output is : null -->

Can anyone help me , why i am getting null value when i print "message.childNodes[0].nodeValue"

Thanks in advance
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

Please don't post the same question to more than one forum. See here for an explanation of why I just deleted the other copy of this thread.

[Jess in Action][AskingGoodQuestions]
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15383
Because you are not writing a well formatted XML document.

Liu Zhixiang
Ranch Hand

Joined: Aug 12, 2006
Posts: 32
About nodeValue
"If the object is a TextNode, the nodeValue property returns a string representing the text contained by the node. If the object is an element, the nodeValue returns null."

So message.nodeValue is null;

<mes1>ashok</mes1>------ "ashok" is a TextNode,but not "<mes1>ashok</mes1>"
please care the method to creat a xmlnode:
var createTexttitle=xmlDoc.createTextNode(titletext);
var createTitle=xmlDoc.createElement("title");

can you tell me what is the "message.childNodes[0]"?
sure ,It is not a TextNode just like "<mes1>ashok</mes1>"
you can write like this :"message.childNodes[0].firstChild.nodeValue"

[ August 13, 2006: Message edited by: snowwolf ]
[ August 14, 2006: Message edited by: Liu Zhixiang ]

I Think Therefore I Am
achayya matta
Ranch Hand

Joined: Oct 27, 2005
Posts: 111
Thank you so much saved my life
I agree. Here's the link:
subject: getting null from XML (AJAX)