Parsing di un file XHTML in PHP e AJAX

I seguenti test sul parsing di XHTML servito come application/xhtml+xml sono stati realizzati utilizzando AJAX e PHP. I risultati mostrano come questo tipo di contenuto sia compatibile con tutte i linguaggi usati per i test, il che dimostra come XHTML resti a tutti gli effetti il miglior candidato per un web interoperabile.

File XHTML


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>Sample XHTML file</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
 </head>
 <body>
  <div>
      <h1>Test heading</h1>
      <p>Test paragraph.</p>
   </div>
 </body>
</html>

Codice JavaScript


function XMLDoc() {
	var me = this;
	var req = null;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	}
	
	else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject("MSXML2.XMLHttp.6.0");
		}
		catch(e) {
			try {
				req = new ActiveXObject("MSXML2.XMLHttp.3.0");
			}
		catch(e) {
			req = null;
		}
		
		}
	}
	
	
	this.request = req;
	this.loadXMLDoc = function (url, handler) {
		if (this.request) {
			this.request.open ("GET", url, true);
			this.request.onreadystatechange = function () {
				handler(me);
			};
			this.request.setRequestHeader("Content-Type", "application/xhtml+xml");
			this.request.send(null);
		}
	};
}

function initXML () {
	var newrequest = new XMLDoc();
	newrequest.loadXMLDoc("test.xht", showData);
}

function showData(req) {
	req = req.request;
	var content = document.getElementById("content");
	
	
	
	if (req.readyState == 4 && req.status == 200) {
		var root = req.responseXML.documentElement;
		var h1 = root.getElementsByTagName("h1")[0].firstChild.nodeValue;
		var p = root.getElementsByTagName("p")[0].firstChild.nodeValue;
		var html = "<div><h1>" + h1 +"</h1><p>" + p + "</p></div>";
		content.innerHTML = html;
	}
}


window.onload = initXML;

Codice PHP

Estensione DOM

 
$dom = new DOMDocument();
$dom->load('test.xht');
echo '<pre>'. "\n";
print_r($dom);
echo '</pre>' . "\n";


$dom = new DOMDocument();
$dom->load('test.xht');

$h1 = $dom->getElementsByTagName('h1')->item(0)->firstChild->nodeValue;
$p = $dom->getElementsByTagName('p')->item(0)->firstChild->nodeValue;

SimpleXML


$xhtml = simplexml_load_file('test.xht');
echo '<pre>'. "\n";
print_r($xhtml);
echo '</pre>' . "\n";

$xhtml = simplexml_load_file('test.xht');
$h1 = $xhtml->body->div->h1;
$p = $xhtml->body->div->p;

Test

Parsing di XHTML

Torna su