SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem parsing responseXML

    Hello. Let me confess this: I've never been able to properly wrap my brain around Javascript. It gives me headaches every time. I thought I was making progress with this one, then I got stumped on what I thought would be the easy part. I'm trying to use XMLHttpRequest to grab some PHP-generated XML from the server and slot the results into some form fields. I can get the XML, no problem, and I can pick out the first bit of data and stick it in a field, like so:

    Code:
    req.onreadystatechange = function checkReadyState () {
      if (req.readyState == 4) {
         if(req.status == 200)
            window.document.testform.field1.value = 
            req.responseXML.getElementsByTagName("firstname")[0].firstChild.data;
         else
          alert("Error");
      }
    }
    That works. When I add a line to grab the next bit of data, like so...

    Code:
    req.onreadystatechange = function checkReadyState () {
      if (req.readyState == 4) {
         if(req.status == 200)
            window.document.testform.field1.value = 
            req.responseXML.getElementsByTagName("firstname")[0].firstChild.data;
            window.document.testform.field2.value = 
            req.responseXML.getElementsByTagName("lastname")[0].firstChild.data;
         else
          alert("Error");
      }
    }
    ...I get no joy. In IE I get the first bit, but not the second. In Firefox I get nothing at all. Here's a sample of the XML, if that's any use:

    Code:
    <subform>
      <subformid>1849</subformid>
      <firstname>John</firstname>
      <lastname>Doe</lastname>
      <title>Wage Slave</title>
    </subform>
    Can anybody point out my obviously glaring error here? I'd be much obliged.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What errors do you get if any?

  3. #3
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No errors. The failure is silent, but deadly.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try sticking document in there:

    req.responseXML.documentElement.getElementsByTagName("lastname")[0].firstChild.data;

  5. #5
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    7stud, don't you mean just document or document.documentElement? documentElement is a property of the document object, inherited from the core document object (in the strict W3C model).

  6. #6
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, either document or documentElement or both, it didn't help. But thanks for the suggestions.

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well... I meant document, but then I saw an article on XMLHttpRequests that used the code I posted, so I thought I would also post that.

    Let me confess this: I've never been able to properly wrap my brain around Javascript.
    Then I suggest you not use XMLHttpRequests since you are probably misusing it anyway and you will probably drive users away from your website.

    Or, at the very least start with a simple XMLHttpRequest where you read a string instead of an XML document and see if you can get that to work. Then, progress to a non php generated XML document and see if you can read that with an XMLHttpRequest.

  8. #8
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gotta learn somehow. This won't be used by the general public anyway. In case you're interested, I already did the baby-steps stuff. I did pull in a text file. I did pull in some static XML. As I said, I thought I had got to the easy part. I've got this XMLresponse object. All I want to do is work with the data. Surely you're not suggesting I just give up?

  9. #9
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried loading the XML document into Moz or FF and looking at it with the DOM inspector to make sure you are correctly targeting the second element?

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In case you're interested, I already did the baby-steps stuff. In case you're interested, I already did the baby-steps stuff. I did pull in a text file. I did pull in some static XML.
    Then your problem has nothing to do with javascript. You are unable generate a correctly formed XML document using php. Why are you posting questions about your javascript when you already know the javascript works? You're just wasting everyone's time.

  11. #11
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahem. See that XML sample in my original post? That's what my PHP generates. I didn't post the whole thing because it's a bit lengthy and includes an inline DTD, but it validates just fine. If it looks incorrectly formed to you, tell me how please. Otherwise I'll stick to my theory that there is an error in my Javascript. If you consider this to be a waste of time, I request that you do not reply at all. I'll go elsewhere for answers.

  12. #12
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try using document.getElementsByTagNameNS().
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  13. #13
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See that XML sample in my original post? That's what my PHP generates.
    You said you were able to get a static XML document to work. Then compare your static XML doc your dynamically generated doc. Is there a difference? Are they in the same directories on the server? What the heck? Come on. How can it be the javascript if it worked with a static XML doc?
    Last edited by 7stud; Jun 5, 2005 at 22:51.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •