Creating custom XML or JSON structures with a server-side language from database data and returning it as a response to an XHR is a required step for creating Ajax-enabled database integration.
Although we have already briefly covered the structuring of dynamic data as an XML structure to be used as an Ajax response, in this section we will focus more on the server side of the process. Responding with valid XML requires a few easy steps that can be achieved by the following instructions.
Including an XML Declaration
In order to specify that our data structure is XML, we must add an XML declaration to the beginning of the file that we would like to return to the client side. The following code shows an example of how to add this declaration to the structure and how to also specify the format in which the XML data should be encoded by the server.
$declaration = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>";
Setting the Content Type to Text/XML
header("Content-Type: application/xml; charset=UTF-8");
The last thing to remember is to carefully structure the string that you want to ultimately use as the XML response with opening and closing XML tags surrounding the dynamic data. This may seem the most obvious part of the process, but is usually the most likely to be an issue.