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.
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.