By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,457 Members | 1,334 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,457 IT Pros & Developers. It's quick & easy.

AJAX XML parsing problem

P: n/a
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.get ElementsByTagName("firstname")[0].childNodes[0].nodeValue;
getById('lastname').value=xmlHttp.responseXML.getE lementsByTagName("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?

Jan 18 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
danep wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 18 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.