What Is The Connection Between Ajax and XML?


XML is a special language used to structure and store .xml files. It is a syntax based on tags.

In some cases, the answer received from Ajax (the data transmitted from the server of the script) can be the content of an XML document. This working method is used by API applications to transfer data from one server to another (from an external server of the site that solicited the transfer). Using Ajax, you can read an XML document directly.

In this tutorial, we will present the method to display data in the web page using Ajax.


1. Loading the XML data

The first stage is obtaining the XML data. They can be received from PHP (or other server application) as a string, or they can be taken directly from the .xml.

Once the data has been received, they must be loaded in a DOM Java Script object.


a) If XML data are received from Ajax from a server script, in a string row, the loading codes of the XML format are:


// Function to load the XML details into a DOM Javascript object

Function getXML_string(txt_xml) {

// If you are not using Internet Explorer as a browser

if(window.DOMParser) {

// create the DOM object and save the “tree”

//   using data from the “xmlDoc”

get xml = new DOM Parser();

xml.Doc = getxml.parse (txt_xml,”sourcecode/xml”);


else {

// For IE 8

// create the XML document in an “xmlDoc”

xmlDoc=new ActiveXObject(“Microsoft.XMLDOM”);

xmlDoc =”false”;         // You can use it for

//   forcing the program to load an XML document



return xmlDoc;


// the element with the data received from the server

var string_xml = ‘the string text where the XML is stored

Received from the server using Ajax (called using GET or POST)’;

// You are calling the function “getXML_row()” with “row_xml”

xml_dom = getXML_row(row_xml);


Internet Explorer uses the function “getXML_row()” to load the string of data in a DOM object, and the other browsers are using the DOMParser() function.

– The “row_xml” variable retains data that Ajax receives from the php script and sometimes, even the string data from the xml doc.

b) If the XML data must be taken directly from Ajax from the “file.xml”, you must use the object “XMLHttpRequest”:



// The function that takes the XML DOM variables from the code

// the content of an XML (the address from the “file” parameter)

function getXML_file {

// If the web browser supports XMLRequest protocol with HTTP

if (window.XMLHttpRequest) {

// Create the variable that contains the instance from the XMLHttpRequest

xhttp = new XMLHttpRequest();


else {

// For Internet explorer 5 or newer

xhttp = new ActiveXObject(“Microsoft.XMLHTTP”);


// Define and execute the functions that calls for “file”

xhttp.open(“GET”, file ,false);


// Retain and resend the answer, transformed in XML DOM

xmlDoc = xhttp.response for XML object;

return xmlDoc;


// The variable with the address of the XML file

var file_xml = ‘file.xml’;

// If calls for the function “getXML_file()” with “file_xml”

var xml_dom = getXML_(file_xml);


– the function “responseXML” transforms the received data directly into XML DOM.

Leave a Reply

Your email address will not be published. Required fields are marked *