processing XML with XMLHttpRequest

H

horndude77

Ok, I've been reading around a lot and haven't found an answer for
this. I'm using XMLHttpRequest to get an XML document from a java
servlet. When the response is processed I can view the text of the XML
document, but when I try and getElementsByTagName() it is null. In fact
it seems that even firstChild is null. I'm just learning so go easy on
me.

Here is some example code. This is called by the XMLHttpRequest
onreadystatechange function:

getjournalready = function(req)
{
alert(req.responseText); //this shows the document correctly
var xml = req.responseXML; //xml is not null (I've tested for this)
var i=0;
var html="<h2>Journal entries:</h2>";
while(i >= 0) //write html
{
title = xml.getElementsByTagName("title");
username = xml.getElementsByTagName("username");
ts = xml.getElementsByTagName("ts");
text = xml.getElementsByTagName("text");
if(title != null)
{ //it never makes it into this inner loop
html += "<h3>"+title.firstChild.data+"</h3>";
html += "<p>"+username.firstChild.data+",
"+ts.firstChild.data+"</p>";
html += "<hr />";
html += "<p>"+text.firstChild.data+"</p>";
i=i+1;
}
else{ i=-10; }
}
document.getElementById("content").innerHTML = html;
}

Here is an example xml document that is sent back from the servlet:
<?xml version="1.0" encoding="UTF-8"?>
<journal>
<entry>
<title>a title</title>
<username>joe</username>
<ts>2005-05-17 22:34:44.281</ts>
<text>This is a journal entry</text>
</entry>
</journal>

It's rudamentary right now, but I can't see what wrong just yet. I've
checked to make sure the the mime type is "text/xml". I'm at a loss
right now. Any help? Thanks.
 
H

horndude77

Ok, I think I found most of the problem. Inside one of the entries was
a '<' character. This seemed to be messing the parse up. It is quite
annoying that no errors or warnings showed up on the javascript console
of firefox. That would have made the problem much easier to locate.
Doubly annoying is that when I change the '<' to a '&lt' it also
doesn't parse. Would anyone know why this is happening? ok, thanks for
the help!
 
M

Matthew Lock

Doubly annoying is that when I change the '<' to a '&lt' it also
doesn't parse. Would anyone know why this is happening? ok, thanks for
the help!

It's supposed to be &lt; to escape a "<" less than character. You
forgot the semicolon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top