cvs: php4 /pear/Experimental/XML Xml2obj.php
| From: | Thomas V.V.Cox | Date: | Thu, 03 May 2001 22:06:36 +0000 |
| Subject: | cvs: php4 /pear/Experimental/XML Xml2obj.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-110@lists.php.net to get a copy of this message | ||
cox Thu May 3 15:06:36 2001 EDT
Added files:
/php4/pear/Experimental/XML Xml2obj.php
Log:
This class takes an xml file and maps it to an objects tree. It doesn't
need DOM support, and the resultant tree is more easy (and limited) to handle.
Note that it's experimental and waiting for comments.
Index: php4/pear/Experimental/XML/Xml2obj.php
+++ php4/pear/Experimental/XML/Xml2obj.php
<?php
//
// +----------------------------------------------------------------------+
// | PHP version 4.0 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2001 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt.
|
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Tomas V.V.Cox <cox@idecnet.com> |
// | |
// +----------------------------------------------------------------------+
//
require 'XML/Parser.php';
/*
* This class takes and xml file and maps it to an objects tree. It doesn't need
* DOM support, and the resultant tree is more easy (en limited) to handle.
* Example of use:
*
* require './Xml2obj.php';
* PEAR::setErrorHandling(PEAR_ERROR_DIE);
* $parser = new Xml2obj('./Pear_Package_Definition.xml');
* $tree = $parser->getTree();
* // View the resultant tree
* print_r($tree);
* // Direct access to values:
* echo $tree->MAINTAINER->INITIALS->value;
* // Direct acces to attribs (returns an array):
* print_r($tree->MAINTAINER->INITIALS->atts);
*
* @author Tomas V.V.Cox <cox@idecnet.com>
*
*/
class Xml2obj extends XML_Parser
{
/*
* Constructor
* @param $file string The xml file to parse
*/
function Xml2obj ($file)
{
$this->folding = true;
$this->XML_Parser(null, 'event');
$this->xmlfile = $file;
$GLOBALS['_XML_Xml2obj_data'] = array();
$this->store = &$GLOBALS['_XML_Xml2obj_data'];
}
/*
* Parses the xml file and returns the objects tree
*
* @return object The object tree or an object error
*/
function &getTree ()
{
$err = $this->setInputFile($this->xmlfile);
if (PEAR::isError($err)) {
return $err;
}
$err = $this->parse();
if (PEAR::isError($err)) {
return $err;
}
return $this->store;
}
function StartHandler($xp, $elem, &$attribs)
{
$new = & new Xml2obj_branch;
$new->atts = $attribs;
$new->name = $elem;
// root element
if (!isset($this->childs)) {
$this->store = $new;
// this will store the array of childs. For example:
// $this->childs = array('MAINTAINER', 'INITIALS');
$GLOBALS['_XML_Xml2obj_childs'] = array();
$this->childs = &$GLOBALS['_XML_Xml2obj_childs'];
} else {
$eval_str = $this->evalize();
eval ("\$curr = & $eval_str;");
// If you are in root->MAINTAINER, this will create
// a new property called $elem, with an object as value
$curr->$elem = $new;
$this->childs[] = $elem;
}
return NULL;
}
function EndHandler($xp, $elem)
{
$eval_str = $this->evalize();
eval("\$new = & $eval_str;");
if (trim($this->cdata)) {
// this will assign the "value" property for the
// current object, only if it have one
$new->value = $this->cdata;
}
$this->cdata = '';
array_pop($this->childs);
return NULL;
}
/*
* Returns the object path to pass to eval. For example:
* $this->store->PACKAGE->MAINTAINER->NAME
*
* @return string
*/
function evalize ()
{
foreach ($this->childs as $child) {
$eval_str .= "->$child";
}
return '$this->store' . $eval_str;
}
/*
* The xml character data handler
*/
function cdataHandler($xp, $data)
{
$this->cdata .= $data;
}
}
/*
* All objects of the Xml2obj tree are of this type. They'll
* only have properties, no methods.
*
* @author Tomas V.V.Cox <cox@idecnet.com>
*/
class Xml2obj_branch
{
/*
* The attributes of this element
*/
var $atts = array();
/*
* The name of the element. Perhaps useful in conjuction
* with get_class_vars()
*/
var $name = null;
/*
* Elements with value will have also this property
*/
//var $value = null;
}
?>