AJAX XML parsing problem

D

danep

Hi, I'm fairly new to AJAX, but I've been able to retrieve HTML and
plain-text documents without any trouble. However, I haven't figured
out how to retrieve it in XML format. Basically, here's the script
that's supposed to retrieve and parse the data when you pass it the url
of the page generating the XML data:

<script type="text/javascript">
var xmlHttp

function getresponse(url)
{
xmlHttp=GetXmlHttpObject()
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
getById('firstname').value=xmlHttp.responseXML.getElementsByTagName("firstname")[0].childNodes[0].nodeValue;
getById('lastname').value=xmlHttp.responseXML.getElementsByTagName("lastname")[0].childNodes[0].nodeValue;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>


Note that getById is just a function I made up to save space... the
problem is not with actually placing the content on the page I don't
think, so don't worry about it.

Here's the php file that generates the XML response:

<?php
(some database query returning a first and last name)
echo '<?xml version="1.0" ?>';
echo '<firstname>';echo $record->firstname;echo '</firstname>';
echo '<lastname>';echo $record->lastname;echo '</lastname>';
?>

I think the problem is either that (a) I'm not parsing the returned XML
file correctly, or (b) the file is not actually being returned in XML
format. Any ideas?
 
M

Martin Honnen

danep said:
Here's the php file that generates the XML response:

<?php
(some database query returning a first and last name)
echo '<?xml version="1.0" ?>';
echo '<firstname>';echo $record->firstname;echo '</firstname>';
echo '<lastname>';echo $record->lastname;echo '</lastname>';

String concatenation is an error prone way to construct well-formed XML.
Use an API that takes care of constructing the well-formed XML, PHP 5
has DOM support e.g.

$xmlDocument = new DOMDocument('1.0', 'UTF-8');
$xmlDocument->appendChild($xmlDocument->createElement('root'));
$firstname = $xmlDocument->createElement('firstname');
$firstname->appendChild($xmlDocument->createTextNode('Kibo & Xibo'));
$xmlDocument->documentElement->appendChild($firstname);
$lastname = $xmlDocument->createElement('lastname');
$lastname->appendChild($xmlDocument->createTextNode('Godlies'));
$xmlDocument->documentElement->appendChild($lastname);
header('Content-Type: application/xml');
$xmlDocument->save('php://output');

or use the XmlWriter API

header('Content-Type: application/xml');
$xmlWriter = new XmlWriter();
$xmlWriter->openURI('php://output');
$xmlWriter->startDocument('1.0', 'UTF-8');
$xmlWriter->startElement('root');
$xmlWriter->writeElement('firstname', 'Kibo & Xibo');
$xmlWriter->writeElement('lastname', 'Godlies');
$xmlWriter->endElement();
$xmlWriter->endDocument();


So currently the problem is not your client-side code but your
server-side attempt to construct well-formed XML that fails.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top