Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Struts and the fly likes Ajax repose as reponseXML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Ajax repose as reponseXML " Watch "Ajax repose as reponseXML " New topic

Ajax repose as reponseXML

Vani Bandargal
Ranch Hand

Joined: Oct 06, 2005
Posts: 82
I never had luck in getting responseXML from Ajax call. I have been following each and every post in this forum regrading the Ajax and I myself had partcipated in few.
I never had/have issue in getting reposeText. It works perfect. But I really really want to get my result as an xml now.
I know the basic thumb rule for reposeXML which says unless the xml is perfectly formatted, it will return blank.

I am pretty confident that my final xml is perfectly foramatted.
<?xml version="1.0" ?>
<entry>Please Pick One</entry>

I recently saw RoshiniG Gopals post and Merill's and other peoples valuable input on that topic .
Merill also suggested the same for me long back.

I am positng my code. Please let me know where it is wrong.
alerting responseXML gives me null and so the subsequent alert statements.
In one of the forum someone suggested that ignore whether or not alert of responseXML gives null or not and simply call getElementsByTagName. I tried that also but no luck.
alert('Value of XRows:'+xRows) gives me nothing

Please advice me where I am wrong.

Added code UDD for readability
[ September 22, 2006: Message edited by: Merrill Higginson ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864

As I look for problems in your code, nothing jumps out at me. Please look at the "Hello World" AJAX example that appears as the last post in this linkTry coding it and running it. I'd be interested to know if you get a responseXML when you run this example. Also, which browser are you using? I tried this example on both IE V6, and Firefox.

Consultant, Sima Solutions
Anay Nayak

Joined: Aug 15, 2006
Posts: 26
Well your xml is invalid.

You do not have a root element in your xml document.

Something like
<?xml version="1.0" ?>

would work properly.

Always test your xml in either Internet explorer or Firefox. It helps [ ]
Vani Bandargal
Ranch Hand

Joined: Oct 06, 2005
Posts: 82
Following are the observations that I noticed regarding reponseXML from Ajax call

1. When I added root element to the code that I shown above as per Anay Nayak's suggestion, it worked fine.
So my final xml is
<?xml version="1.0" ?>
<entry>Please Pick One</entry>

2. Merill's example code shown at this link worked fine for me.
But if you notice, he is not adding <?xml version="1.0" ?> . I tried adding/removing this to final xml response and does not make any difference.
I thought it is mandatory to have <?xml version="1.0" ?> to get xml response, is this correct ?(???.....)

3. When I added second element to Merill's code like this
out.write("<hello>hello world1</hello>");
out.write("<hello>hello world2</hello>");
His example stopped working and I started to get JavaScript error. This is because XML is not perfect as per the rule "only one top element is allowed in XML document"

I then changed like this to add root element
out.write("<hello>hello world1</hello>");
out.write("<hello>hello world2</hello>");
It started to work fine now.

4. These are some Javascript observations.
if you do this -> alert(req.responseXM)L you get -> [[object]] in javascript alert box
if you do this -> alert('I have response: ' + req.responseXML) you get only -> "I have response:" in javascript alert box (I was expecting to see "I have response: [[object]]" )
Can't we alert an object by concatenating to a string (I never had experience doing this in before OR may be I am wrong ???.....)

Earlier, I have not proceeded to work with my issue since I was not getting anything by doing alert('I have response' + req.responseXML). I wanted to first get this message : "I have response: [[object]]" )
in javascript alert box and then wanted to proceed.

Please confirm my observations/questions. Thank you all for helping me to fix my problem.
I agree. Here's the link:
subject: Ajax repose as reponseXML
Similar Threads
reponseXML empty Ajax/Struts
readyState=4 and Http 404 error in Ajax /Struts
Error:"Component returned failure code
Problem with Ajax Double Combo
Recursive call to a JavaScript method with AJAX call