www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Ajax responseXML

  1. #1
    Join Date
    Aug 2008
    Posts
    61

    Ajax responseXML

    Ok so i have this
    Code:
    		if(xmlHttp.readyState == 4){
    			xmlStuff = xmlHttp.responseXML;
    			alert(xmlStuff.getElementsByTagName("key").length)
    			document.getElementById("blah").innerHTML = xmlStuff.getElementsByTagName("key")[0].nodeValue;
    		}
    and this is the response XML from the ajax request
    Code:
    <?xml version='1.0' encoding='ISO-8859-1'?>
    
    <respondeded>
    
    		<key>
    
    			imageSource
    
    		</key>
    
    		<value>
    
    			<img src=\'imagethingy.png\' border=\'0\' alt=\'test\' />
    
    		</value>
    
    	</respondeded>
    And when i alert the length for elements with tag name "key" it says the length is 0. I have no idea why so any help would be greatly appreciated

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    You should try:

    Code:
    xmlStuff = xmlHttp.responseXML.documentElement;
    If you alert(xmlStuff); what does it say? null?


    Eric

  3. #3
    Join Date
    Aug 2008
    Posts
    61
    no it returns object XMLdocument

  4. #4
    Join Date
    Aug 2008
    Posts
    61
    Got any idea? Cause I've been playing with it some more and i still cant get it to work properly

  5. #5
    Join Date
    Sep 2008
    Posts
    7
    Quote Originally Posted by A1ien51
    You should try:

    Code:
    xmlStuff = xmlHttp.responseXML.documentElement;
    If you alert(xmlStuff); what does it say? null?


    Eric
    That is exactly what mine is saying!!! I'm having the same issue, been working on this all week actually! I have a very simple xml doc that is being returned to responseXML. It's just:

    <root1>test<root1>

    I can't seem to pull the nodeValue from it however I try. Seems you have a solution to this issue but never posted it!!!

    I would be forever grateful of your assistance!

    Thank you,
    Frank

  6. #6
    Join Date
    Sep 2008
    Posts
    7
    Eric - I am having the exact same issue. I did what you requested and alerted the responseXML.documentElement and I got a 'null'. You seem to know what this implies, but never posted a solution. I would be very grateful for any help you may offer, as I've been working on this issue all week!

    Thanks, Frank

  7. #7
    Join Date
    Aug 2008
    Posts
    61
    Now its just returning object element instead of object xml document

  8. #8
    Join Date
    Mar 2007
    Location
    Athens, Greece
    Posts
    177
    Hi,

    I have a comlete working example ready for downloading here. Maybe is helpfull:
    (XMLHttpRequest: responseXML Example)

    http://in3d.eu/in3d_Programming/AJAX...sponseXML.html

    Cheers!

    Kostas

  9. #9
    Join Date
    Aug 2007
    Location
    Washington
    Posts
    219
    I googled the issue and here's what I got. If responseXML is returning null there can be several reasons one of the most common being that content you are reading is not set to text/html.

    As in Kostas Zotos's example, in php you can change this with:

    PHP Code:
    header("Content-type: text/xml"); 

  10. #10
    Join Date
    Aug 2008
    Posts
    61
    oh screw it im just gunna do responseText because this is just not working when it definitely should be

    I mean i have got it to the point in which it will return the length of some of the elements in the xml but i cant do like for loops on them and such and its just annoying id rather just do a responsetext and split it with javascripts split method

  11. #11
    Join Date
    Sep 2008
    Posts
    7
    posiden: If you've gotten that far where you can actually pull elements out of the returned XML, then the rest is easy. Just walk the document using DOM...

    var rootNode = ajaxResponse.responseXML.getElementsByTagName("root");

    Something like that.

    My issue is that I can't even get a valid XML document Element returned from the server.

    Kostas: I tried your example and it does not work on my server here. I am unable to get a valid XML file returned to browser from POST. Unfortunately server does not support PHP. Browser is IE. I tried with FF, and I can get from this code...

    Code:
    alert(ajaxResponse.getAllResponseHeaders());
    alert(ajaxResponse.responseXML.documentElement);
    alert(ajaxResponse.responseXML);
    alert(ajaxResponse.responseXML.getElementsByTagName("root");
    alert(ajaxResponse.responseXML.getNodeValue(ajaxResponse.responseXML.getElementsByTagName("root"));
    alert(typeof ajaxResponse.responseXML.getNodeValue(ajaxResponse.responseXML.getElementsByTagName("root"));
    This response:
    Code:
    Content-Type: text/xml
    Content-Length: 64
    
    [object Element]
    
    [object XMLDocument]
    
    [object HTMLCollection]
    
    ""
    
    string
    The XML file is simple:

    Code:
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <root>test</root>

  12. #12
    Join Date
    Mar 2007
    Location
    Athens, Greece
    Posts
    177
    Hi,

    Quote Originally Posted by fbolton
    Kostas: I tried your example and it does not work on my server here. I am unable to get a valid XML file returned to browser from POST. Unfortunately server does not support PHP.
    Yes the example needs PHP.. (JS sends the AJAX request to a PHP script that dynamically generates an XML response document)..
    There is also a php code section that allows you to load the XML through PHP and send it as response XML..

    -------------
    If want to use ready external XML files, as alternative you may just load the XML files directly inside JS and not use the XMLHttp request at all..

    I mean something like:
    Code:
    xmlDoc.async=false; // Assume xmlDoc is a new xml dom object
    xmlDoc.load("Test.xml")
    Then if needed you can load another XML file and parse it with the same DOM commands. You can set the "async=true" in conjunction with a "xmlDoc.onreadystatechange" (For IE) and an "xmlDoc.onload" (For non IE browsers) event handler so that the script continue without waiting the file to load..

    -------------
    My issue is that I can't even get a valid XML document Element returned from the server.
    Have you try:

    Code:
    var Root=ajaxResponse.responseXML.getElementsByTagName("root")[0] // The "getElementsByTagName" returns a nodeList. Need to specify an array index
    var Content=Root.firstChild.nodeValue  // The textNode's value  (the "test" string of your xml file)
    alert(Content)
    OR similarly:
    Code:
    var Root=ajaxResponse.responseXML.documentElement
    alert(Root.firstChild.nodeValue) // The textNode's value  (the "test" string of your xml file)
    Kostas
    Last edited by Kostas Zotos; 09-15-2008 at 02:34 PM.

  13. #13
    Join Date
    Sep 2008
    Posts
    7
    Kostas:

    Thank you! Thank you! Thank you!

    After spending a week trying to get this to work I've finally done it thanks to much help from this thread. I have a lot of work now cleaning up my test code.

    Thanks again for your help and patience.

    Frank

  14. #14
    Join Date
    Sep 2008
    Posts
    7
    Wow, this really sucks. Somehow I broke it when cleaning up the code. Now when I run it even with very basic XML file, and I do this:

    alert(ajaxResponse.responseXML.getElementsByTagName("root")[0]);

    I get null as response.

    Something's not right, and I can't figure out what I broke. Everything looks exactly the same - I even did the 'undo' feature until I got back to where it was working, and it now won't work...

    What a waste of time!

    Frank

  15. #15
    Join Date
    Sep 2008
    Posts
    7
    Quote Originally Posted by Kostas Zotos
    If want to use ready external XML files, as alternative you may just load the XML files directly inside JS and not use the XMLHttp request at all..
    The reason I have to use the XML file from the server is because I added tokens in the XML file that the server parses on the way out and replaces with values. Otherwise I would just load the file - but it has to come from the server.

    I still can't figure out why the firstChild element in the XML file is now null. I sent the request as txt and it comes back will full valid XML (in text form)... but when I return it as XML, I can't walk the nodes! This is so frustrating because it was just working an hour ago perfectly!!!

    When I test in FF, the firstChild element comes back as undefined, compared to null for IE.
    Last edited by fbolton; 09-15-2008 at 05:36 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles