⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree.php

📁 一个用PHP编写的
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?php
//
// +----------------------------------------------------------------------+
// | PEAR :: XML_Tree                                                     |
// +----------------------------------------------------------------------+
// | 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.               |
// +----------------------------------------------------------------------+
// | Authors: Bernd R鰉er <berndr@bonn.edu>                               |
// |          Sebastian Bergmann <sb@sebastian-bergmann.de>               |
// |          Tomas V.V.Cox <cox@idecnet.com>                             |
// |          Michele Manzato <michele.manzato@verona.miz.it>             |
// +----------------------------------------------------------------------+
//
// $Id: Tree.php,v 1.35 2004/05/26 14:58:18 davey Exp $
//

require_once PLOG_CLASS_PATH."class/xml/parser/Parser.php";
require_once PLOG_CLASS_PATH."class/xml/tree/Node.php";

/**
 * \ingroup XML
 *
 * PEAR::XML_Tree
 *
 * Purpose
 *
 *    Allows for the building of XML data structures
 *    using a tree representation, without the need
 *    for an extension like DOMXML.
 *
 * Example
 *
 *    $tree  = new XML_Tree;
 *    $root =& $tree->addRoot('root');
 *    $foo  =& $root->addChild('foo');
 *
 *    $tree->dump(true);
 *
 * @author  Bernd R鰉er <berndr@bonn.edu>
 * @package XML
 * @version $Version$ - 1.0
 */
class XML_Tree extends XML_Parser
{
    /**
     * File Handle
     *
     * @var  resource
     */
    var $file = NULL;

    /**
     * Filename from which the XML_Tree was read
     *
     * @var  string
     */
    var $filename = '';

    /**
     * Namespace
     *
     * @var  array
     */
    var $namespace = array();

    /**
     * Root node of the XML tree
     *
     * @var  object XML_Tree_Node
     */
    var $root = NULL;

    /**
     * XML Version
     *
     * @var  string
     */
    var $version = '1.0';

    /**
     * Whether to encapsulate the all CDATA in a <![CDATA[]]> section
     *
     * @var boolean
     */

    var $use_cdata_sections = false;

    /**
     * Constructor
     *
     * @param  string  filename  Filename where to read the XML
     * @param  string  version   XML Version to apply
     */
    function XML_Tree($filename = '', $version = '1.0')
    {
        $this->filename = $filename;
        $this->version  = $version;
    }

    /**
     * Use <![CDATA[]]> for all CDATA sections
     *
     * @return void
     */

    function useCdataSections()
    {
        $this->use_cdata_sections = true;
    }

    /**
     * Gets the root node
     *
     * @return object    Root XML_Tree_Node, or PEAR_Error if there isn't any root node.
     *
     * @access public
     */
    function &getRoot()
    {
        if (!is_null($this->root)) {
            return $this->root;
        }
        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
     * @access public
     */
    function &addRoot($name, $content = '', $attributes = array(), $lineno = null)
    {
        $this->root = new XML_Tree_Node($name, $content, $attributes, $lineno);
        return $this->root;
    }

    /**
     * Inserts a child/tree (child) into tree ($path,$pos) and maintains
     * namespace integrity
     *
     * @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),
     *                                   or name of child 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
     * @access public
     * @see getNodeAt()
     */
    function &insertChild($path, $pos, $child, $content = '', $attributes = array())
    {
        $parent =& $this->getNodeAt($path);
        if (!$parent) {
            return $parent;
        }

        $x =& $parent->insertChild(null, $pos, $child, $content, $attributes);

        if (!$this->isError($x)) {
        // update namespace to maintain namespace integrity
            $count = count($path);
            foreach ($this->namespace as $key => $val) {
                if ((array_slice($val,0,$count)==$path) && ($val[$count]>=$pos)) {
                    $this->namespace[$key][$count]++;
                }
            }
        }
        return $x;
    }

    /**
     * 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
     * @access public
     * @see getNodeAt()
     */
    function &removeChild($path, $pos)
    {
        $parent =& $this->getNodeAt($path);
        if ($this->isError($parent)) {
            return $parent;
        }

        $x =& $parent->removeChild($pos);

        if (!$this->isError($x)) {
            // Update namespace to maintain namespace integrity
            $count=count($path);
            foreach($this->namespace as $key => $val) {
                if (array_slice($val,0,$count)==$path) {
                    if ($val[$count]==$pos) {
                        unset($this->namespace[$key]); break;
                    }
                    if ($val[$count]>$pos) {
                        $this->namespace[$key][$count]--;
                    }
                }
            }
        }

        return $x;
    }

    /**
     * Maps a XML file to a XML_Tree
     *
     * @return mixed The XML tree root (an XML_Tree_Node), or PEAR_Error upon error.
     * @access public
     */
    function &getTreeFromFile ()
    {
        $this->folding = false;
        $this->XML_Parser(null, 'event');
        $err = $this->setInputFile($this->filename);
        if ($this->isError($err)) {
            return $err;
        }
        $this->cdata = null;
        $err = $this->parse();
        if ($this->isError($err)) {
            return $err;
        }
        return $this->root;
    }

    /**
     * Maps an XML string to an XML_Tree.
     *
     * @return mixed The XML tree root (an XML_Tree_Node), or PEAR_Error upon error.
     * @access public
     */
    function &getTreeFromString($str)
    {
        $this->i = null;
        $this->folding = false;
        $this->XML_Parser(null, 'event');
        $this->cdata = null;
        $err = $this->parseString($str);
        if ($this->isError($err)) {
            return $err;
        }
        return $this->root;
    }

    /**
     * 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
     *
     * @access private
     */
    function startHandler($xp, $elem, &$attribs)
    {
        $lineno = xml_get_current_line_number($xp);
		
        // root elem
        if (!isset($this->i)) {
            $this->obj1 =& $this->addRoot($elem, null, $attribs, $lineno);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -