//--------------- 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"=>""));
// 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 ------------------//