473,799 Members | 3,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AJAX XML parsing problem

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=GetXmlH ttpObject()
xmlHttp.onready statechange=sta teChanged;
xmlHttp.open("G ET",url,true) ;
xmlHttp.send(nu ll);
}

function stateChanged()
{
if (xmlHttp.readyS tate==4)
{
getById('firstn ame').value=xml Http.responseXM L.getElementsBy TagName("firstn ame")[0].childNodes[0].nodeValue;
getById('lastna me').value=xmlH ttp.responseXML .getElementsByT agName("lastnam e")[0].childNodes[0].nodeValue;
}
}

function GetXmlHttpObjec t()
{
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.XMLHT TP");
}
}
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>';e cho $record->firstname;ec ho '</firstname>';
echo '<lastname>';ec ho $record->lastname;ech o '</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
1 2079
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>';e cho $record->firstname;ec ho '</firstname>';
echo '<lastname>';ec ho $record->lastname;ech o '</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($x mlDocument->createElement( 'root'));
$firstname = $xmlDocument->createElement( 'firstname');
$firstname->appendChild($x mlDocument->createTextNode ('Kibo & Xibo'));
$xmlDocument->documentElemen t->appendChild($f irstname);
$lastname = $xmlDocument->createElement( 'lastname');
$lastname->appendChild($x mlDocument->createTextNode ('Godlies'));
$xmlDocument->documentElemen t->appendChild($l astname);
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1719
by: shadow.demon | last post by:
So ajax is the new black, i've had a couple ideas about how to implement it. I've alook at ajax.net and few other implementations but to me they're a bit complex. Maybe it's the way to go to have better coding. In the standard case of a dropdownlist, or listbox, that needs to be filled as the user types (aka google suggest), i thought it might be best handle the following way. javascript engine <-> xml request <-> asp.net web service...
1
3634
by: babucsedu | last post by:
Hello Everyone, This is my first post here. I am trying to parse a xml chunk returned by server as a response of ajax type request in this way: // code for IE if (window.ActiveXObject)
5
3850
by: noddy | last post by:
The full code relating to this question can be found at http://marc.info/?l=php-general&m=112198633625636&w=2 It describes a bare bones way of making an AJAX request. It works fine for me except at the last step where data is put into an innerHTML element within <div id="foo"> </div> Here is the function to do that:
3
3133
by: equazcion | last post by:
Hi, I have an image reference (IMG) in my page that changes depending on the value of a database field. Clicking the image triggers an Ajax call to change the database field (toggles the field value between 1 and 0). I've made it work so that the image source changes to reflect the new value once the Ajax call is complete. In other words, the image reflects the value of the database field in real-time. Kind of like a real-time checkbox. This...
3
3955
by: dhsieh | last post by:
I am trying out nested AJAX calls for the first time, but I seem to have hit a snag. The code snippet is the outer AJAX call and a function, it gathers information about a company. As we get towards the bottom of the onComplete function, there is a call to doGetAddressContacts(), which accepts an address ID. This then returns a list of contacts at that address. What I am trying to do is for each address that it retrieves and outputs, as it...
2
1753
by: willl69 | last post by:
Hi All, Ive been having a problem of late with one of my sites that uses PHP5 / Ajax. The problem is that periodically the ajax functions lock up and it gets stuck in the loading phase of the request. If i restart the apache server everything goes back to normal (i assume it severs the connection to the ajax called function). I think this is a problem with the javascript part as i have tried it using a straight post request and have have...
3
2478
by: willl69 | last post by:
Hi All, Ive been having a problem of late with one of my sites that uses PHP5 / Ajax. The problem is that periodically the ajax functions lock up and it gets stuck in the loading phase of the request. If i restart the apache server everything goes back to normal (i assume it severs the connection to the ajax called function). I think this is a problem with the javascript part as i have tried it using a straight post request and have...
6
1950
by: John Doe | last post by:
Here's my issue: I have an instant messenger type feature for my site, its basically an ajax IM feature. I run with a mysql backend on the site, i have a div on my main page that runs a javascript timer to load the php page via ajax every 15 seconds which checks the "pinguser" table to see if the user has any chat requests, then echoes them to a div on the page. what i REALLY want to do is have the ajax page see if there is a ping for the...
8
3146
by: cyqotiq | last post by:
First, let me state that this is not necessarily a Firefox problem, as I haven't fully tested in IE just yet. Second, let me state that this is not the typical "getElementById not working Firefox" post. Third, there are pieces of this code that I am not at liberty to display, change, discuss, or re-implement. As far as this question is concerned, this means that (1) I cannot use 3rd party libraries, which is why I've implemented my own...
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10268
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10247
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.