Here’s the whole script:
file test.php:
<?php
//--------------- The Stack --------------------//
$stack=array();
function
push($data) {
// This function puts the data in the argument onto the stack
global $stack;
array_push($stack,$data);
}
function
pop() {
// This function takes the uppermost data
// from the stack and returns it
global $stack;
// Error checking
if(count($stack)==0) {
die("Error: Buffer Underflow!");
}
return array_pop($stack);
}
//--------------- XML converter-----------------//
function create_xml_tag($name,$attrs, $contents) {
//returns a valid XML tag
$buffer="<$name";
$attributestring="";
foreach($attrs as $attr=>$value) {
$buffer.=' '.$attr.'="'.$value.'"';
}
// Is this tag empty?
if(strlen($contents)==0) {
$buffer.=' />';
}
else {
$buffer.='>'.$contents.'</'.$name.'>';
}
return $buffer;
}
function
LoadAndExec($filename) {
// This function opens a file, executes its PHP functions,
// and returns the output.
// start an output buffer
ob_start();
// include the file and execute its PHP code
include($filename);
// Stop buffer and return its contents
$content=ob_get_contents();
ob_end_clean();
return $content;
}
function
doParse($xml)
{
// This function initializes the stack,
// starts the SAX parser
// and returns the bottom stack element
// Put an empty element onto the stack - this will contain the
// output of the parsing process.
push(array("contents"=>""));
// Initialize SAX parser
$xmlparser=xml_parser_create("ISO-8859-1");
// Assign element handling functions
xml_set_element_handler($xmlparser,"tag_open","tag_close");
xml_set_character_data_handler($xmlparser,"cdata");
xml_set_default_handler($xmlparser,"cdata");
// Start the parsing process
if (!xml_parse($xmlparser,$xml)) {
die("Error parsing XML!");
}
// Destroy the parser
xml_parser_free($xmlparser);
// return contents of the bottom stack element
$first=pop();
return $first["contents"];
}
function
tag_open($parser, $name, $attrs)
{
//Push the attribute list onto the stack
push(array("attrs"=>$attrs, "contents"=>""));
}
function
cdata($parser,$string) {
// Fetch from Stack, insert the character data, and put it back
$data=pop();
$data["contents"].=$string;
push($data);
}
function
tag_close($parser,$name) {
// This function first looks for a handling function for the
// current tag. If there is none, the tag gets passed through.
// If a handling function exists, execute it and add its return data
// to the contents of the stack element under it
// The name of the tag handling function
$function="handle_".strtolower($name);
// Fetch the content and attributes of the current tag from the stack
$data=pop();
if(function_exists($function)) {
// The tag handling function exists. Execute it!
$buffer=call_user_func($function,$data["contents"],$data["attrs"]);
}
else {
// No handling function for this tag. Pass it through.
$buffer=create_xml_tag($name, $data["attrs"], $data["contents"]);
// Create a string with the attributes in XML format
}
// Take the converted tag and add it to the contents of the tag around it
$sublevel=pop();
$sublevel["contents"].=$buffer;
push($sublevel);
}
//--------------- Tag handling functions ------------------//
function handle_doc($contents,$attrs) {
$buffer='';
$buffer.='<html>';
$buffer.='<head><title>'.$attrs["TITLE"].'</title></head>';
$buffer.='<table border="1" bgcolor="'.$attrs["BGCOLOR"].'" width="90%" align="center" cellpadding="15">';
$buffer.='<tr><td>';
$buffer.=$contents;
$buffer.='</td></tr></table></html>';
return $buffer;
}
function
handle_dayofweek($contents,$attrs) {
return(date("l"));
}
function
handle_bigheadline($contents,$attrs) {
return('<b><font size="5" face="Verdana">'.$contents.'</font></b>');
}
function
handle_nicebox($contents,$attrs) {
$buffer="";
$buffer.='<table border="0" cellspacing="0" cellpadding="1"';
$buffer.=' bgcolor="'.$attrs["BORDERCOLOR"].'"><tr><td>';
$buffer.='<table border="0" cellspacing="5" cellpadding="0"';
$buffer.=' bgcolor="white">';
$buffer.='<tr><td>';
$buffer.=$contents;
$buffer.='</td></tr></table></td></tr></table>';
//uncomment this to get a dump of the stack in action
//echo "<pre>";var_dump($GLOBALS["stack"]);echo "</pre>";
return $buffer;
}
function
handle_product($contents,$attrs) {
$names=array( 0=>"Quattro Stagioni",
1=>"Diavola",
2=>"Margherita",
3=>"Tonno",
4=>"Capricciosa"
);
$buffer="";
$buffer.='<a href="products.php?id='.$attrs["ID"].'">';
$buffer.=$names[$attrs["ID"]].' - click here to buy!';
$buffer.='</a>';
return $buffer;
}
//--------------- Main program------------------//
// Read in the XML file and execute its PHP code
$xml=LoadAndExec("test.pxml");
//Convert the XML to HTML using the tag handling functions
$html=doParse($xml);
// Output the converted XML to the browser
echo $html;
?>