• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

parsing the xml returned from ajax call

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to parse the below xml string passed from the servlet:

<messages>
<messages>
<id>TATASCR</id>
<lname>Sha</lname>
<fname>Lee</fname>
</messages>
</messages>

using the code:



But xmlHttp.responseXML does not work. Can anyone tell me what is the problem?
[ April 21, 2008: Message edited by: Lee Sha ]
 
Sheriff
Posts: 67693
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lee Sha:
But xmlHttp.responseXML does not work. Can anyone tell me what is the problem


"does not work" doesn't tell us what the problem is. What does "does not work" mean?

Also, please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
Lee Sha
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
xmlHttp.responseXML does not give me the xml which is passed from the servlet.
 
Bear Bibeault
Sheriff
Posts: 67693
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you know? I'm not doubting you, but it would be instructive to know what your evidence is in order to help diagnose the problem.

Some things to always check: is the content type of the response correct? Is the XML well-formed with the <?xml?> header line?
 
Lee Sha
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh. I don't know about that. The only string which the servlet builds is this -

<messages><message><id>TATASCR</id><lname>Sha</lname><fname>Lee</fname></message></messages>

Can you tell me what I have to append to this string before passing it to the html page?
 
Sheriff
Posts: 27525
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks like well-formed XML to me. But you don't pass it to the HTML page (whatever you meant by that), all you have to do is return it as the response to the AJAX request.
 
Lee Sha
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. That is what I did. I passed it as a response to xmlHttp request.
 
Paul Clapham
Sheriff
Posts: 27525
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. So then, what is the value of xmlHttp.responseXML once it actually gets the response?

(If it were up to me I would just send the table as a table instead of sending something else and having Javascript turn it into a table. But there may be good reasons for doing what you are doing.)
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
That looks like well-formed XML to me. But you don't pass it to the HTML page (whatever you meant by that), all you have to do is return it as the response to the AJAX request.



That does not look like it to me. it is missing the <xml....> which Bear pointed out.

Links that may help:
http://www.w3schools.com/xml/xml_tree.asp
http://radio.javaranch.com/pascarello/2006/09/12/1158096122600.html

Eric
 
Lee Sha
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now this is what the XML string which is passed as response to xmlHttp request object:

<?xml version="1.0" encoding="ISO-8859-1"?><messages><message><id>TATASCR</id><lname>Cheriyil</lname><fname>Samal</fname></message></messages>

Still, I do not receive it in xmlHttp.responseXML
 
Lee Sha
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried using xmlHttp.responseXML.xml and now it has the xml string which is passed as a response. I get a script error when I do the below operation on it:



Can anyone tell me what I am missing here?
 
Bear Bibeault
Sheriff
Posts: 67693
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You still haven't answered my question regarding content type.
 
Lee Sha
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I set the content type as "text/xml"
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had such problem several times. It was the wrong content type in every single case.

Double-check the content type of the response.
 
Lee Sha
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I checked it. I set the content type in the servlet code as below:

 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you read this:

http://radio.javaranch.com/pascarello/2006/09/12/1158096122600.html

Eric
 
Lee Sha
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I followed all the steps which you have listed. I can get the responseXML, but I have problem only when I try to parse it. In the below piece of code, I have given comment where it throws script error.

Can you confirm if I am parsing it properly?
 
Vilmantas Baranauskas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've saved your XML into a.xml file and then make AJAX request to the static a.xml file. Following code works fine for response processing:



BTW, invoking response.setContentType("text/xml") does not guarantee that content type will be correct. I suggest using Firefox with LiveHTTPHeaders plugin to see if the content type is really correct.
 
Lee Sha
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying this with IE. With which browser did you try it?
 
Vilmantas Baranauskas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've used Firefox.

BTW, I recommend to rename your second "messages" tag to "message".

<messages>
<message>
<id>TATASCR</id>
<lname>Sha</lname>
<fname>Lee</fname>
</message>
</messages>
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also you should not have

responseXML.xml

it should be

responseXML.document

Eric
 
Lee Sha
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your suggestions. This is the final code which worked both in Firefox and IE.
 
Bear Bibeault
Sheriff
Posts: 67693
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whew! Now that that's all over with... in the future you might want to consider JSON as a data exchange format which is much easier to digest in JavaScript.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic