Source for file Tree.php
Documentation is available at Tree.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 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. |
// +----------------------------------------------------------------------+
// | Sebastian Bergmann <sb@sebastian-bergmann.de> |
// | Tomas V.V.Cox <cox@idecnet.com> |
// | Michele Manzato <michele.manzato@verona.miz.it> |
// +----------------------------------------------------------------------+
// $Id: Tree.php 288859 2009-09-27 04:46:10Z clockwerx $
require_once 'XML/Parser.php';
require_once 'XML/Tree/Node.php';
* Allows for the building of XML data structures
* using a tree representation, without the need
* for an extension like DOMXML.
* $root =& $tree->addRoot('root');
* $foo =& $root->addChild('foo');
* @version $Version$ - 1.0
* Filename from which the XML_Tree was read
* Root node of the XML tree
* @var object XML_Tree_Node
* Whether to encapsulate the all CDATA in a <![CDATA[]]> section
* @param string filename Filename where to read the XML
* @param string version XML Version to apply
function XML_Tree($filename = '', $version = '1.0')
* Use <![CDATA[]]> for all CDATA sections
* @return object Root XML_Tree_Node, or PEAR_Error if there isn't any root node.
return $this->raiseError ("No root");
* Sets the root node of the XML tree.
* @param string name Name of root element
* @return object XML_Tree_Node Reference to the newly created root node
function &addRoot($name, $content = '', $attributes = array (), $lineno = null )
* Inserts a child/tree (child) into tree ($path,$pos) and maintains
* @param mixed path Path to parent node to add child (see
* getNodeAt() for format)
* @param integer pos Position where to insert the new child.
* 0 < means |$pos| elements before the end,
* e.g. -1 appends as last child.
* @param mixed child Child to insert (XML_Tree or XML_Tree_Node),
* @param string content Content (text) for the new node (only if
* $child is the node name)
* @param array attributes Attribute-hash for new node
* @return object Reference to the inserted child (node), or PEAR_Error upon error
function &insertChild($path, $pos, $child, $content = '', $attributes = array ())
if (PEAR ::isError ($parent)) {
$x = & $parent->insertChild (null , $pos, $child, $content, $attributes);
if (!PEAR ::isError ($x)) {
// update namespace to maintain namespace integrity
if ((array_slice($val,0 ,$count)== $path) && ($val[$count]>= $pos)) {
* Removes a child node from tree and maintains namespace integrity
* @param array path Path to the parent of child to remove (see
* getNodeAt() for format)
* @param integer pos Position of child in parent children-list
* 0 < means |$pos| elements before the end,
* e.g. -1 removes the last child.
* @return object Parent XML_Tree_Node whose child was removed, or PEAR_Error upon error
if (PEAR ::isError ($parent)) {
$x = & $parent->removeChild ($pos);
if (!PEAR ::isError ($x)) {
// Update namespace to maintain namespace integrity
if ($val[$count]== $pos) {
* Maps a XML file to a XML_Tree
* @param string $encoding XML Files encoding, use NULL (default) to use whatever the document specifies
* @return mixed The XML tree root (an XML_Tree_Node), or PEAR_Error upon error.
$this->XML_Parser ($encoding, 'event');
$err = $this->setInputFile ($this->filename);
if (PEAR ::isError ($err)) {
if (PEAR ::isError ($err)) {
* Maps an XML string to an XML_Tree.
* @param string $encoding XML Files encoding, use NULL (default) to use whatever the document specifies
* @return mixed The XML tree root (an XML_Tree_Node), or PEAR_Error upon error.
$this->XML_Parser ($encoding, 'event');
$err = $this->parseString ($str);
if (PEAR ::isError ($err)) {
* Handler for the xml-data
* Used by XML_Parser::XML_Parser() when parsing an XML stream.
* @param mixed xp ignored
* @param string elem name of the element
* @param array attribs attributes for the generated node
function startHandler ($xp, $elem, &$attribs)
$this->obj1 = & $this->addRoot($elem, null , $attribs, $lineno);
if (!empty ($this->cdata)) {
$parent_id = 'obj' . ($this->i - 1 );
$parent = & $this->$parent_id;
$parent->children [] = &new XML_Tree_Node(null , $this->cdata, null , $lineno);
$obj_id = 'obj' . $this->i++;
$this->$obj_id = &new XML_Tree_Node($elem, null , $attribs, $lineno);
* Handler for the xml-data
* Used by XML_Parser::XML_Parser() when parsing an XML stream.
* @param mixed xp ignored
* @param string elem name of the element
function endHandler ($xp, $elem)
$obj_id = 'obj' . $this->i;
// recover the node created in StartHandler
if (count($node->children ) > 0 ) {
if (trim($this->cdata) != '') {
$node->setContent ($this->cdata);
$parent_id = 'obj' . ($this->i - 1 );
$parent = & $this->$parent_id;
// attach the node to its parent node children array
$parent->children [] = $node;
if (count($node->children ) > 0 ) {
if (trim($this->cdata)) {
$node->setContent ($this->cdata);
* The xml character data handler
* Used by XML_Parser::XML_Parser() when parsing an XML stream.
* @param mixed xp ignored
* @param string data PCDATA between tags
function cdataHandler ($xp, $data)
* Get a copy of this tree by cloning and all of its nodes, recursively.
* @return object XML_Tree copy of this node.
$clone->root = $this->root->cloneTree ();
foreach($temp as $varname => $value) {
if (!in_array($varname,array ('filename','version','root'))) {
* Print text representation of XML tree.
* @param bool xmlHeader if true then generate also the leading XML
* 'Content-type' header directive, e.g. for
* direct output to a web page.
function dump($xmlHeader = false )
header('Content-type: text/xml');
* Get text representation of XML tree.
* @return string Text (XML) representation of the tree
$out = '<?xml version="' . $this->version . "\"?>\n";
return $this->raiseError ("Bad XML root node");
* @param string name namespace
$name_parts = explode(':',$node->name );
if (sizeof($name_parts) > 1 ) {
$namespace = $name_parts[0 ];
if (isset ($node->namespace [$namespace])) {
return $node->namespace [$namespace];
} elseif (isset ($this->root->namespace [$namespace])) {
return $this->root->namespace [$namespace];
* Get a reference to a node. Node is searched by its 'path'.
* @param mixed path Path to node. Can be either a string (slash-separated
* children names) or an array (sequence of children names) both
* of them starting from node. Note that the first name in sequence
* must be the name of the document root.
* @return object Reference to the XML_Tree_Node found, or PEAR_Error if
* the path does not exist. If more than one element matches
* then only the first match is returned.
return $this->raiseError ("XML_Tree hasn't a root node");
return $this->raiseError ("Path to node is empty");
if ($this->root->name != $rootName) {
return $this->raiseError ("Path does not match the document root");
$x = & $this->root->getNodeAt ($path1);
if (!PEAR ::isError ($x)) {
// No node with that name found
return $this->raiseError ("Bad path to node: [". implode('/', $path). "]");
* Gets all children that match a given tag name.
* @return array An array of Node objects of the children found,
* @author Pierre-Alain Joye <paj@pearfr.org>
return $this->raiseError ('Empty tag name');
foreach ($this->root->children as $child) {
if ($child->name == $tagName) {
* Gets all children that match a given tag name from node
* @param string $tagName Tag Name
* @param object &$node Node in which to search
* @see XML_Tree::getElementsByTagName()
* @return array An array of found Node objects, empty is none found.
* @author Pierre-Alain Joye <paj@pearfr.org>
* @author Davey Shafik <davey@php.net>
return $this->raiseError ('Empty tag name');
foreach ($node->children as $child) {
if ($child->name == $tagName) {
* Checks if $name is a valid XML name
* @param string $name String to check for validity as an XML Name
// check for invalid starting character
return new PEAR_Error ( ucfirst($type) . " ('$name') has an invalid name, an XML name may only start with a letter or underscore" );
if (!preg_match("/^([a-zA-Z_]([a-zA-Z0-9_\-\.]*)?:)?[a-zA-Z_]([a-zA-Z0-9_\-\.]+)?$/", $name)) {
return new PEAR_Error ( ucfirst($type) . " ('$name') has an invalid name, an XML name may only contain alphanumeric chars, period, hyphen, colon and underscores" );
Documentation generated on Mon, 11 Mar 2019 15:34:45 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|