<?php
/** Class XML definition
*
* @author Tobias H. Michaelsen <hide@address.com>
* @date 1.0 (2001-06-13)
*
* $Id: class.XML.php,v 1.2 2001/08/01 07:40:21 phps Exp $
*
*/
/**
* Class for accessing XML data.
*
* This class offers methods for accessing the nodes of a XML document using
* the saxo methode. No additional PHP extensions like DOM XML or something
* similar are required to use these features.
*
* To use this class, please write an extension class that overwrites the
* handle_start_element(), handle_end_element() and handle_character_data()
* methodes.
*
* @link http://www.phpsitebuilder.dk/ Latest release of this class
* @link http://www.w3.org/TR/xml W3C XML Recommendation
* @copyright Copyright (c) 2001 Tobias H. Michaelsen. All rights reserved.
* @author Tobias H. Michaelsen <hide@address.com>
* @version 1.0 (2001-06-13)
* @access public
*/
class XML
{
/**
* Constructor of the class.
*
* This constructor initializes the class and, when a filename is given,
* tries to read and parse the given file.
*
* @access public
* @author Michael P. Mehl <hide@address.com>
* @param string $file Path and name of the file to read and parsed.
* @see load_file()
*/
function XML($file = "")
{
// Check whether a file was given.
if (!empty($file))
{
// Load the XML file.
$this->load_file($file);
}
}
/**
* Reads a file and parses the XML data.
*
* This method reads the content of a XML file, tries to parse its
* content and upon success stores the information retrieved from
* the file into an array.
*
* @access public
* @author Michael P. Mehl <hide@address.com>
* @param string $file Path and name of the file to be read and parsed.
* @see handle_start_element(), handle_end_element(),
* handle_character_data()
*/
function load_file($file)
{
// Check whether the file exists and is readable.
if (file_exists($file) && is_readable($file))
{
// Read the content of the file.
$content = implode("", file($file));
// Check whether content has been read.
if (!empty($content))
{
// Create an XML parser.
$parser = xml_parser_create();
// Set the options for parsing the XML data.
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
// Set the object for the parser.
xml_set_object($parser, $this);
// Set the element handlers for the parser.
xml_set_element_handler($parser, "handle_start_element",
"handle_end_element");
xml_set_character_data_handler($parser,
"handle_character_data");
// Parse the XML file.
if ( !xml_parse($parser, $content, true) )
{
// Display an error message.
$this->display_error("XML error in file %s, line %d: %s",
$file, xml_get_current_line_number($parser),
xml_error_string(xml_get_error_code($parser)));
}
// Free the parser.
xml_parser_free($parser);
}
}
else
{
// Display an error message.
$this->display_error("File %s could not be found or read.", $file);
}
}
/**
* Handles opening XML tags while parsing.
*
* While parsing a XML document for each opening tag this method is
* called.
*
* @access private
* @author Michael P. Mehl <hide@address.com>
* @edited Tobias H. Michaelsen <hide@address.com>
* @param int $parser Handler for accessing the current XML parser.
* @param string $name Name of the opening tag found in the document.
* @param array $attributes Associative array containing a list of
* all attributes of the tag found in the document.
* @see handle_end_element(), handle_character_data()
*/
function handle_start_element($parser, $name, $attributes)
{
// Dummy function.
return null;
}
/**
* Handles closing XML tags while parsing.
*
* While parsing a XML document for each closing tag this method is
* called.
*
* @access private
* @author Michael P. Mehl <hide@address.com>
* @edited Tobias H. Michaelsen <hide@address.com>
* @param int $parser Handler for accessing the current XML parser.
* @param string $name Name of the closing tag found in the document.
* @see handle_start_element(), handle_character_data()
*/
function handle_end_element($parser, $name)
{
// Dummy function.
return null;
}
/**
* Handles character data while parsing.
*
* While parsing a XML document for each character data this method
* is called.
*
* @access private
* @author Michael P. Mehl <hide@address.com>
* @edited Tobias H. Michaelsen <hide@address.com>
* @param int $parser Handler for accessing the current XML parser.
* @param string $text Character data found in the document.
* @see handle_start_element(), handle_end_element()
*/
function handle_character_data($parser, $text)
{
// Dummy function.
return null;
}
/**
* Displays an error message.
*
* This method displays an error messages and stops the execution of the
* script. This method is called exactly in the same way as the printf
* function. The first argument contains the message and additional
* arguments of various types may be passed to this method to be inserted
* into the message.
*
* @access private
* @author Michael P. Mehl <hide@address.com>
* @param string $message Error message to be displayed.
*/
function display_error ( $message )
{
// Check whether more than one argument was given.
if ( func_num_args() > 1 )
{
// Read all arguments.
$arguments = func_get_args();
// Create a new string for the inserting command.
$command = "\$message = sprintf(\$message, ";
// Run through the array of arguments.
for ( $i = 1; $i < sizeof($arguments); $i++ )
{
// Add the number of the argument to the command.
$command .= "\$arguments[".$i."], ";
}
// Replace the last separator.
$command = eregi_replace(", $", ");", $command);
// Execute the command.
eval($command);
}
// Display the error message.
echo "<b>phpXML error:</b> ".$message;
// End the execution of this script.
exit;
}
}
?>