📄 node.php
字号:
<?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> |
// | Christian K黨n <ck@chkuehn.de> (escape xml entities) |
// | Michele Manzato <michele.manzato@verona.miz.it> |
// +----------------------------------------------------------------------+
//
// $Id: Node.php,v 1.22 2004/05/26 15:03:25 davey Exp $
//
/**
* \ingroup XML
*
* PEAR::XML_Tree_Node
*
* @author Bernd R鰉er <berndr@bonn.edu>
* @package XML_Tree
* @version 1.0 16-Aug-2001
*/
class XML_Tree_Node {
/**
* Attributes of this node
*
* @var array
*/
var $attributes;
/**
* Children of this node
*
* @var array
*/
var $children;
/**
* Content (text) of this node
*
* @var string
*/
var $content;
/**
* Name of the node
*
* @var string
*/
var $name;
/**
* Namespaces for the node
*
* @var array
*/
var $namespaces = array();
/**
* Stores PEAR_Error upon error
*
* @var object PEAR_Error
*/
var $error = null;
/**
* Whether to encapsulate the CDATA in a <![CDATA[]]> section
*
* @var boolean
*/
var $use_cdata_section = null;
/**
* Constructor
*
* @param string name Node name
* @param string content Node content (text)
* @param array attributes Attribute-hash for the node
*/
function XML_Tree_Node($name, $content = '', $attributes = array(), $lineno = null, $use_cdata_section = null)
{
$check_name = XML_Tree::isValidName($name, 'element');
if (!$check_name) {
$this->error =& $check_name;
return;
}
if (!is_array($attributes)) {
$attributes = array();
}
foreach ($attributes as $attribute_name => $value) {
$error = XML_Tree::isValidName($attribute_name, 'Attribute');
if (!$error) {
$this->error =& $error;
return;
}
}
$this->name = $name;
$this->setContent($content, $use_cdata_section);
$this->attributes = $attributes;
$this->children = array();
$this->lineno = $lineno;
}
/**
* Append a child node to this node, after all other nodes
*
* @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 new child node
* @access public
*/
function &addChild($child, $content = '', $attributes = array(), $lineno = null, $use_cdata_section = null)
{
$index = sizeof($this->children);
if (is_object($child)) {
if (strtolower(get_class($child)) == 'xml_tree_node' || is_subclass_of($child,"xml_tree_node")) {
$this->children[$index] = $child;
}
if (strtolower(get_class($child)) == 'xml_tree' && isset($child->root)) {
$this->children[$index] = $child->root->getElement();
}
} else {
$node = new XML_Tree_Node($child, $content, $attributes, $lineno, $use_cdata_section);
/*if (PEAR::isError($node->error)) {
return $node->error;
}*/
$this->children[$index] = $node;
}
return $this->children[$index];
}
/**
* Get a copy of this node by clone this node and all of its children,
* recursively.
*
* @return object Reference to the cloned copy.
* @access public
*/
function &cloneNode()
{
$clone = new XML_Tree_Node($this->name,$this->content,$this->attributes);
$max_child=count($this->children);
for($i=0;$i<$max_child;$i++) {
$clone->children[]=$this->children[$i]->cloneNode();
}
/* for future use....
// clone all other vars
$temp=get_object_vars($this);
foreach($temp as $varname => $value)
if (!in_array($varname,array('name','content','attributes','children')))
$clone->$varname=$value;
*/
return $clone;
}
/**
* Inserts child ($child) to a specified child-position ($pos)
*
* @param mixed path Path to parent node to add child (see getNodeAt()
* for format). If null child is inserted in the
* current node.
* @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 Reference to the newly inserted node, or PEAR_Error upon insertion error.
* @access public
*/
function &insertChild($path,$pos,&$child, $content = '', $attributes = array())
{
$parent =& $this->getNodeAt($path);
if (!$parent) {
// $path was not found
return $parent;
} elseif ($parent != $this) {
// Insert at the node found
return $parent->insertChild(null, $pos, $child, $content, $attributes);
}
// if we don't care where, let's do it in the end
if( $pos == -1 )
$pos = count($this->children);
if (($pos < -count($this->children)) || ($pos > count($this->children))) {
//_debug("Invalid insert position.");
return false;
}
if (is_object($child)) { // child is an object
// insert a single node
//
// OSCAR 20041012
// added "is_subclass_of()" to the condition so that we can subclass the XML_Tree_Class and still
// use its methods!
//
if (strtolower(get_class($child)) == 'xml_tree_node' || is_subclass_of($child,"xml_tree_node")) {
array_splice($this->children, $pos, 0, 'dummy');
if ($pos < 0) {
$pos = count($this->children) + $pos - 1;
}
$this->children[$pos] = &$child;
// insert a tree i.e insert root-element of tree
} elseif (strtolower(get_class($child)) == 'xml_tree' && isset($child->root)) {
array_splice($this->children, $pos, 0, 'dummy');
if ($pos < 0) {
$pos = count($this->children) + $pos - 1;
}
$this->children[$pos] = $child->root;
} else {
//_debug("Bad node (must be a XML_Tree or an XML_Tree_Node)");
return false;
}
} else { // child offered is a string
array_splice($this->children, $pos, 0, 'dummy');
if ($pos < 0) {
$pos = count($this->children) + $pos - 1;
}
$this->children[$pos] = new XML_Tree_Node($child, $content, $attributes);
}
return $this;
}
/**
* Removes child at a given position
*
* @param integer pos position of child to remove in children-list.
* 0 < means |$pos| elements before the end,
* e.g. -1 removes the last child.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -