blob: 6396166e8475636b6444a9c087e049cc14c3be66 [file] [log] [blame]
<?php
#*****************************************************************************
#
# xml_simple.php
#
# Author: Wayne Beaton
# Date: 2006-03-28
#
# Description: This code provides an approximation of the functionality
# provided by the simple_xml APIs in PHP 5.
#
#****************************************************************************
require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/xml_sax_parsing.php");
/*
* Use this method to load the XML content in the file named $file_name
* into an object structure.
*
* An object is created for each element in the file. The root object
* (the object returned by this call) represents the root XML element.
* Subelements can be obtained by asking for them by their element name
* as shown below. The returned value will always be an array (since an
* element can be represented multiple times). Attributes for an element
* can be obtained by asking the element for its 'attributes' property.
* The text associated with an element is obtained by asking for the
* 'text' property.
*
* Use the dump() method on the result of this method to display the
* structure of the object and determine how to query it. Note that
* PHP 4 restricts you from chaining property reads.
*
* For example:
* $content = load_file($_SERVER['DOCUMENT_ROOT'] . '/articles/Article-API use/about.xml');
* $content->dump(); // Dumps the content of the file to stdout.
* echo $content->attributes['LINK']; // "eclipse-api-usage-rules.html"
* $title = $content->TITLE[0];
* echo $title->text; // "How to Use the Eclipse API"
*/
function & load_xml_file($file_name) {
$handler = new XmlObjectFileHandler();
parse_xml_file($file_name, $handler);
return new XmlObjectWrapper($handler->object);
}
class XmlObject {
var $text = '';
var $properties = array();
var $attributes = array();
function dump($indent = 'this') {
echo $indent . '->text = ' . $this->text . '<br>';
$this->dumpAttributes($indent);
$this->dumpProperties($indent);
}
function dumpAttributes($indent) {
foreach($this->attributes as $key=>$value) {
echo $indent . '->attributes[\'' . $key. '\'] = ' . $value . '<br>';
}
}
function dumpProperties($indent) {
foreach ($this->properties as $key=>$values) {
$index = 0;
foreach ($values as $value) {
$value->dump($indent.'->'.$key.'['.$index++.']');
}
}
}
}
class XmlObjectWrapper {
var $wrapped;
function XmlObjectWrapper($object) {
$this->wrapped = $object;
}
function __get($prop_name, & $prop_value) {
$prop_name = strtoupper($prop_name);
if ($prop_name == 'TEXT')
$prop_value = $this->wrapped->text;
else if ($prop_name == 'ATTRIBUTES')
$prop_value = $this->wrapped->attributes;
else
$prop_value = $this->wrapped->properties[$prop_name];
return true;
}
function __set($prop_name, $prop_value) {
$prop_name = strtoupper($prop_name);
$this->wrapped->properties[$prop_name] = $prop_value;
return true;
}
function __call($function, $arguments, & $result) {
$result = call_user_func_array(array($this->wrapped, $function), $arguments);
return true;
}
}
overload("XmlObjectWrapper");
/*
* This class is used by the SAX parser to start parsing
* an XML file.
*/
class XmlObjectFileHandler extends XmlFileHandler {
var $object;
function XmlObjectFileHandler() {
parent :: XmlFileHandler();
}
/*
* This method returns the root handler for a event file
* The root handler essentially represents the file itself
* rather than any actual element in the file. The returned
* element handler will deal with any elements that may occur
* in the root of the XML file.
*/
function get_root_element_handler() {
return new XmlObjectHandler();
}
function end_root_element_handler($handler) {
$this->object= & $handler->object;
}
}
class XmlObjectHandler extends XmlElementHandler {
var $object;
function XmlObjectHandler() {
$this->object = new XmlObject();
}
function & get_next($name, $attributes) {
$handler = new XmlObjectHandler();
foreach($attributes as $key => $value) {
$handler->object->attributes[$key] = $value;
}
return $handler;
}
function end_handler($name, & $handler) {
$name = str_replace("-", "_", $name);
$object = new XmlObjectWrapper($handler->object);
if (is_array($this->object->properties[$name])) {
array_push($this->object->properties[$name], & $object);
} else {
$this->object->properties[$name] = array(& $object);
}
}
function data($data) {
$this->object->text .= $data;
}
}
?>