cvs: pear /XML_Tree Tree.php
| From: | Thomas V.V.Cox | Date: | Sun, 19 Aug 2001 15:50:48 +0000 |
| Subject: | cvs: pear /XML_Tree Tree.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-581@lists.php.net to get a copy of this message | ||
cox Sun Aug 19 11:50:48 2001 EDT
Modified files:
/pear/XML_Tree Tree.php
Log:
Here it is Sebastian:
- Added xml file to objects tree mapping
- Extends the class from XML/Parser.xml
- Renamed method "copy" to "clone"
Index: pear/XML_Tree/Tree.php
diff -u pear/XML_Tree/Tree.php:1.4 pear/XML_Tree/Tree.php:1.5
--- pear/XML_Tree/Tree.php:1.4 Thu Aug 16 06:02:54 2001
+++ pear/XML_Tree/Tree.php Sun Aug 19 11:50:47 2001
@@ -16,11 +16,14 @@
// | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> |
// | Bernd Römer <berndr@bonn.edu> |
// | Christian Kühn <ck@chkuehn.de> |
+// | Tomas V.V.Cox <cox@idecnet.com> (tree mapping from xml file)|
+// | |
// +----------------------------------------------------------------------+
//
-// $Id: Tree.php,v 1.4 2001/08/16 10:02:54 sebastian Exp $
+// $Id: Tree.php,v 1.5 2001/08/19 15:50:47 cox Exp $
//
+require_once 'XML/Parser.php';
require_once 'XML/Tree/Node.php';
/**
@@ -45,7 +48,8 @@
* @package XML_Tree
* @version 1.0 16-Aug-2001
*/
-class XML_Tree {
+class XML_Tree extends XML_Parser
+{
/**
* File Handle
*
@@ -97,17 +101,82 @@
* @param string name of root element
* @return object reference to root node
*/
- function &add_root($name) {
+ function &add_root($name) {
$this->root = new XML_Tree_Node($name);
return $this->root;
}
+ /*
+ * Maps a xml file to a objects tree
+ *
+ * @return object The objects tree or an Pear error
+ */
+ function &getTreeFromFile ()
+ {
+ $this->folding = false;
+ $this->XML_Parser(null, 'event');
+ $err = $this->setInputFile($this->filename);
+ if (PEAR::isError($err)) {
+ return $err;
+ }
+ $this->cdata = null;
+ $this->store_cdata = false;
+ $err = $this->parse();
+ if (PEAR::isError($err)) {
+ return $err;
+ }
+ return $this->root;
+ }
+
+ function StartHandler($xp, $elem, &$attribs)
+ {
+ // root elem
+ if (!isset($this->i)) {
+ $this->obj1 =& $this->add_root($elem);
+ $this->i = 2;
+ } else {
+ $obj_id = 'obj' . $this->i++;
+ $this->$obj_id =& new XML_Tree_Node($elem, null, $attribs);
+ }
+ $this->store_cdata = true;
+ return NULL;
+ }
+
+ function EndHandler($xp, $elem)
+ {
+ $this->i--;
+ if ($this->i > 1) {
+ $obj_id = 'obj' . $this->i;
+ // recover the node created in StartHandler
+ $node =& $this->$obj_id;
+ $node->set_content($this->cdata);
+ $parent_id = 'obj' . ($this->i - 1);
+ $parent =& $this->$parent_id;
+ // attach the node to its parent node children array
+ $parent->children[] = $node;
+ }
+ $this->cdata = null;
+ $this->store_cdata = false;
+ return NULL;
+ }
+
+ /*
+ * The xml character data handler
+ */
+ function cdataHandler($xp, $data)
+ {
+ // only store data inside tags
+ if ($this->store_cdata) {
+ $this->cdata .= $data;
+ }
+ }
+
/**
* Get a copy of this tree.
*
* @return object XML_Tree
*/
- function copy() {
+ function clone() {
return $this;
}