• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting null from XML (AJAX)

 
achayya matta
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ranchers,

i have one servlet which returns some piece of xml.

response.setContentType("text/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"

Regards
Thanks in advance
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are not writing a well formatted XML document.

Eric
 
Liu Zhixiang
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
createTitle.appendChild(createTexttitle);
------------------------------------------------------

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 ]
 
achayya matta
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much sir.you saved my life
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic