processing XML with XMLHttpRequest

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • horndude77@gmail.com

    processing XML with XMLHttpRequest

    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 getElementsByTa gName() 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
    onreadystatecha nge function:

    getjournalready = function(req)
    {
    alert(req.respo nseText); //this shows the document correctly
    var xml = req.responseXML ; //xml is not null (I've tested for this)
    var i=0;
    var html="<h2>Journ al entries:</h2>";
    while(i >= 0) //write html
    {
    title = xml.getElements ByTagName("titl e")[i];
    username = xml.getElements ByTagName("user name")[i];
    ts = xml.getElements ByTagName("ts")[i];
    text = xml.getElements ByTagName("text ")[i];
    if(title != null)
    { //it never makes it into this inner loop
    html += "<h3>"+title.fi rstChild.data+" </h3>";
    html += "<p>"+username. firstChild.data +",
    "+ts.firstChild .data+"</p>";
    html += "<hr />";
    html += "<p>"+text.firs tChild.data+"</p>";
    i=i+1;
    }
    else{ i=-10; }
    }
    document.getEle mentById("conte nt").innerHTM L = 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.

  • horndude77@gmail.com

    #2
    Re: processing XML with XMLHttpRequest

    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!

    Comment

    • Matthew Lock

      #3
      Re: processing XML with XMLHttpRequest


      horndud...@gmai l.com wrote:[color=blue]
      > 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[/color]
      for[color=blue]
      > the help![/color]

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

      Comment

      Working...