Page 1 of 1

Ajax - Parsing XML with JavaScript

Posted: Sat Aug 20, 2005 4:19 pm
by Lars79
Hello,

I just played around with some Ajax examples, but have problems parsing XML data with JavaScript.

The XML file sent by the PHP script looks like this:

Code: Select all

<?xml version="1.0" encoding="ISO-8859-1" standalone="true" ?>

<update>
	<id>element</id>
	<content>some content...</content>
</update>
The header is set correctly:

Code: Select all

header("Content-Type: text/xml");
header("Vary: Accept");
The relevant part of the JavaScript code is here:

Code: Select all

function handleResponse() {
	if (reqObj.readyState == 4) {
		if (reqObj.status == 200) {
			var response = reqObj.responseXML.documentElement;
			
			var id = response.getElementsByTagName("id").item(0).firstChild.data;
			var content = response.getElementsByTagName("content").item(0).firstChild.data;
			
			document.getElementById(id).innerHTML = content;
		}
	}
}
That's the error message from the JavaScript console in Opera:

Code: Select all

AJAX
http://localhost/sandbox/AJAX/
Unknown thread
Error:
name: TypeError
message: Statement on line 57: Could not convert undefined or null to object
Backtrace:
  Line 57 of inline#1 script in http://localhost/sandbox/AJAX/
    var id = (response.getElementsByTagName("id").item(0)).firstChild.data;
  At unknown location
    [statement source code not available]
If I replace the line

Code: Select all

var response = reqObj.responseXML.documentElement;
with

Code: Select all

var response = reqObj.responseHTML;
alert(response);
it spits out the XML data correctly. I am using Apache 1.3.33 and PHP 5.0.4. I tested the example with Opera 8.0.2 and Firefox 1.0.6 on Slackware Linux 10.1. I have no idea what the problem is. The use of the DOM functions looks correct for me. Thanks.

Lars

Posted: Sat Aug 20, 2005 4:29 pm
by newmember
response.getElementsByTagName("id").item(0).firstChild.data
simplify this line by breaking it into simpler chunks...and then see if that helps you to find the problem :)

Posted: Sat Aug 20, 2005 4:32 pm
by Lars79
Thanks for your reply. I already tried several different approaches but always receive the same message. But thanks anyway.

Posted: Sat Aug 20, 2005 4:40 pm
by newmember
http://www.xml.com/pub/a/2005/02/09/xml ... quest.html

try to read here... maybe that will help :?

Posted: Sat Aug 20, 2005 4:50 pm
by Lars79
Thanks for the link =). Unfortunately they use the same way I did.

That's their XML file:

Code: Select all

<?xml version="1.0" encoding="UTF-8" 
  standalone="yes"?>
<response>
  <method>checkName</method>
  <result>1</result>
</response>
And that's their approach:

Code: Select all

function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
      response  = req.responseXML.documentElement;

      method    =
response.getElementsByTagName('method')[0].firstChild.data;

      result    = 
response.getElementsByTagName('result')[0].firstChild.data;

      eval(method + '(\'\', result)');
        } else {
            alert("There was a problem retrieving the XML 
                data:\n" + req.statusText);
        }
    }
}
That's exactly what I did. I really have no idea what's wrong with my code.