<?
header('content-type: application/xhtml+xml; charset=utf-8');
$dom = new DomDocument('1.0');
$dom->load("somedoc.xml"); //load XML from local file
print $dom->saveXML(); //outputs the XML as response to client
?>
<?
$titles = $dom->getElementsByTagName("title");
foreach($titles as $node) {
print $node->textContent." ";
}
?>
<?xml version="1.0" encoding="iso-8859-1" ?>
<response>
<cell>
<title>Title AAA</title>
<description>This is AAA's description</description>
</cell>
<cell>
<title>Title BBB</title>
<description>This is BBB's description</description>
</cell>
<cell>
<title>Title CCC</title>
<description>This is CCC's description</description>
</cell>
</response>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Advanced AJAX Part 1</title>
<script language="javascript" type="text/javascript">
function processXML(xmlDoc) { //process the received XML data with DOM methods
try {
var cells = xmlDoc.getElementsByTagName("cell");
for (var x=0; x<cells.length; x++) {
var title = cells[x].getElementsByTagName("title")[0].firstChild.nodeValue;
var desc = cells[x].getElementsByTagName("description")[0].firstChild.nodeValue;
//Following lines place the data into our HTML page table
var cid = "cell"+(x+1);
document.getElementById(cid).innerHTML = title+"<br>"+desc;
}
} catch (e) {
alert("Error:"+e.name+"\n"+e.message);
}
}
function doHttpRequest() { // This function does the AJAX request
http.open("GET", "getxml.php", true);
http.onreadystatechange = getHttpRes;
http.send(null);
}
function getHttpRes() {
try {
if (http.readyState == 4 ) {
if (http.status == 200) {
var xmlres = http.responseXML.documentElement; //This gets the XML response
processXML(xmlres); //processXML() uses the XML by populating an HTML table
} else {
alert("There was a problem retrieving the XML data:\n" + req.statusText);
}
}
} catch (e) {
alert("Error:"+e.name+"\n"+e.message);
}
}
function getXHTTP() {
var xhttp;
try { // The following blocks get the XMLHTTP object for various browsers
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
// This block handles Mozilla/Firefox browsers...
try {
xhttp = new XMLHttpRequest();
} catch (e3) {
xhttp = false;
}
}
}
return xhttp; // Return the XMLHTTP object
}
var http = getXHTTP(); // This executes when the page first loads.
</script>
</head>
<body>
<input type="button" value="Press" name="btn" onClick="doHttpRequest();">
<br><br><br>
XML data...
<table width=100% border=1 cellspacing=0>
<tr><td id="cell1">cell1</td> <td id="cell2">cell2</td> <td id="cell3">cell3</td><tr>
</table>
</body>
</html>
var xmlres = http.responseXML.documentElement;
function processXML(xmlDoc) { //process the received XML data with DOM methods
try {
var cells = xmlDoc.getElementsByTagName("cell");
for (var x=0; x<cells.length; x++) {
var title = cells[x].getElementsByTagName("title")[0].firstChild.nodeValue;
var desc = cells[x].getElementsByTagName("description")[0].firstChild.nodeValue;
//Following lines place the data into our HTML page table
var cid = "cell"+(x+1);
document.getElementById(cid).innerHTML = title+"<br>"+desc;
}
} catch (e) {
alert("Error:"+e.name+"\n"+e.message);
}
}