cvs: php4 /pear/Experimental/XML Xml2obj.php

From: Date: Tue, 08 May 2001 17:16:55 +0000
Subject: cvs: php4 /pear/Experimental/XML Xml2obj.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-121@lists.php.net to get a copy of this message
cox Tue May 8 10:16:55 2001 EDT Modified files: /php4/pear/Experimental/XML Xml2obj.php Log: This is a complete rewrite of the class. Now the object tree returned by the class is very similiar to a DOM Doc. Also implemented some node methods. Index: php4/pear/Experimental/XML/Xml2obj.php diff -u php4/pear/Experimental/XML/Xml2obj.php:1.2 php4/pear/Experimental/XML/Xml2obj.php:1.3 --- php4/pear/Experimental/XML/Xml2obj.php:1.2 Fri May 4 05:50:54 2001 +++ php4/pear/Experimental/XML/Xml2obj.php Tue May 8 10:16:55 2001 @@ -22,7 +22,7 @@ /* * 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. +* DOM support, and the resultant tree is very similar to a DOM Doc. * Example of use: * *<Package> @@ -40,21 +40,13 @@ * $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); * * Current limitations: -* - It can only handle one equal element in the same tag. For ex: -* <root> -* <foo>Bar1</foo> -* <foo>Bar2</foo> <- -* </root> * * @todo - Ability to parse file descriptors and strings, besides files -* - Add methods for branch objects +* - Ability to seek for a value of a child node attribute (wish) * +* @version 0.1 * @author Tomas V.V.Cox <cox@idecnet.com> */ @@ -69,8 +61,8 @@ $this->folding = true; $this->XML_Parser(null, 'event'); $this->xmlfile = $file; - $GLOBALS['_XML_Xml2obj_root'] = array(); - $this->root = &$GLOBALS['_XML_Xml2obj_root']; + $GLOBALS['_XML_Xml2obj_root_elem'] = null; + $this->obj1 = &$GLOBALS['_XML_Xml2obj_root_elem']; } /* @@ -88,58 +80,42 @@ if (PEAR::isError($err)) { return $err; } - return $this->root; + return $this->obj1; } function StartHandler($xp, $elem, &$attribs) - { + { $new = & new Xml2obj_branch; - $new->atts = $attribs; + if (sizeof($attribs) > 0) { + $new->atts = $attribs; + } $new->name = $elem; // root element - if (!isset($this->childs)) { - $this->root = $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']; + if (!isset($this->i)) { + $this->obj1 = $new; + $this->i = 2; } 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) - { - if (trim($this->cdata)) { - $eval_str = $this->evalize(); - eval("\$new = & $eval_str;"); - // this will assign the "value" property for the - // current object, only if it have one - $new->value = $this->cdata; + $obj_id = 'obj' . $this->i++; + $this->$obj_id = $new; } - $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 () + function EndHandler($xp, $elem) { - foreach ($this->childs as $child) { - $eval_str .= "->$child"; + $this->i--; + if ($this->i > 1) { + $obj_id = 'obj' . $this->i; + $new = & $this->$obj_id; + if ($data = trim($this->cdata)) { + $new->value = $data; + } + $parent_id = 'obj' . ($this->i - 1); + $parent = & $this->$parent_id; + $parent->children[] = $new; } - return '$this->root' . $eval_str; + $this->cdata = ''; + return NULL; } /* @@ -152,8 +128,7 @@ } /* -* All objects of the Xml2obj tree are of this type. They'll -* only have properties, no methods yet. +* All objects of the Xml2obj tree are of this type. * * @author Tomas V.V.Cox <cox@idecnet.com> */ @@ -162,17 +137,45 @@ /* * The attributes of this element */ - var $atts = array(); + //var $atts; // type array /* - * The name of the element. Perhaps useful in conjuction - * with get_class_vars() + * The name of the element itself. */ - var $name = null; + //var $name = null; /* * Elements with value will have also this property + */ + //var $value; // type string + + /* + * Elemts with childrens will have this property */ - //var $value = null; + //var $children; // type array + /* + * Returns the array of attributes of this node or null + */ + function attributes() + { + return (isset($this->atts)) ? $this->atts : null; + } + + /* + * Returns the array of child nodes or null + */ + function &children() + { + return (isset($this->children)) ? $this->children : null; + } + + /* + * Returns the value of an especific attribute or null if not found + * @param $att string Attribute name + */ + function get_attribute ($att) + { + return (isset($this->atts[$att])) ? $this->atts[$att] : null; + } } ?>

« previous php.pear.cvs (#121) next »