Source for file Node.php
Documentation is available at Node.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> |
// | Michele Manzato <michele.manzato@verona.miz.it> |
// +----------------------------------------------------------------------+
// $Id: Node.php 288859 2009-09-27 04:46:10Z clockwerx $
* @version 1.0 16-Aug-2001
* Attributes of this node
* Content (text) of this node
* Namespaces for the node
* Stores PEAR_Error upon error
* Whether to encapsulate the CDATA in a <![CDATA[]]> section
* @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 )
if (PEAR ::isError ($check_name)) {
$this->error = & $check_name;
foreach ($attributes as $attribute_name => $value) {
if (PEAR ::isError ($error)) {
* Append a child node to this node, after all other nodes
* @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 new child node
function &addChild($child, $content = '', $attributes = array (), $lineno = null , $use_cdata_section = null )
$this->children[$index] = $child->root ->getElement ();
$node = new XML_Tree_Node($child, $content, $attributes, $lineno, $use_cdata_section);
if (PEAR ::isError ($node->error )) {
* Get a copy of this node by clone this node and all of its children,
* @return object Reference to the cloned copy.
for($i=0; $i< $max_child; $i++ ) {
$clone->children []= $this->children[$i]->cloneNode ();
$temp=get_object_vars($this);
foreach($temp as $varname => $value)
if (!in_array($varname,array('name','content','attributes','children')))
* 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
* @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 Reference to the newly inserted node, or PEAR_Error upon insertion error.
function &insertChild($path,$pos,&$child, $content = '', $attributes = array ())
if (PEAR ::isError ($parent)) {
} elseif ($parent != $this) {
// Insert at the node found
return $parent->insertChild (null , $pos, $child, $content, $attributes);
return new PEAR_Error ("Invalid insert position.");
if (is_object($child)) { // child is an object
// insert a tree i.e insert root-element of tree
return new PEAR_Error ("Bad node (must be a XML_Tree or an XML_Tree_Node)");
} else { // child offered is a string
* 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.
* @return mixed The removed node, or PEAR_Error upon removal error.
return new PEAR_Error ("Invalid remove position.");
// Using array_splice() instead of a simple unset() to maintain index-integrity
* @param string $name namespace
* @param string $path path
$this->namespace[$name] = $path;
* Returns text representation of this node.
* @return string text (xml) representation of this node. Each tag is
* indented according to its level.
function &get($use_cdata_section = false )
if ($this->name !== null ) {
$out = $ident . '<' . $this->name;
$out = '<' . $this->name;
$out .= ' ' . $name . '="' . $value . '"';
if (isset ($this->namespace) && (is_array($this->namespace))) {
foreach ($this->namespace as $qualifier => $uri) {
$out .= " xmlns:$qualifier='$uri'";
$out .= '<![CDATA[' . $this->content. ']]>';
$out .= $child->get ($use_cdata_section);
if ($do_ident && $empty != true ) {
$out .= $ident . '</' . $this->name . ">\n";
} elseif ($empty != true ) {
$out .= '</' . $this->name . '>';
$out = $ident . '<![CDATA[' . $this->content. ']]>' . "\n";
$out = $ident . $this->content . "\n";
* Get an attribute by its name.
* @param string $name Name of attribute to retrieve
* @return string attribute, or null if attribute is unset.
* Sets an attribute for this node.
* @param string name Name of attribute to set
* @param string value Value of attribute
* Unsets an attribute of this node.
* @param string $name Name of attribute to unset
* Sets the content for this node.
* @param string content Node content to assign
function setContent($content, $use_cdata_section = null )
if ($use_cdata_section == true ) {
* Gets an element by its 'path'.
* @param array path path to element: sequence of indexes to the
* children. E.g. array(1, 2, 3) means "third
* child of second child of first child" of the node.
* @return object reference to element found, or PEAR_Error if node can't
$x = & $this->children[$next]->getElement ($path1);
if (!PEAR ::isError ($x)) {
return new PEAR_Error ("Bad path to node: [". implode('-', $path). "]");
* Get a reference to a node 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
* starting from this node. The first name in sequence
* is a child name, not the name of this node.
* @return object Reference to the XML_Tree_Node found, or PEAR_Error if
* the path does not match any node. Note that if more than
* one element matches then only the first matching node is
// Get the first children of this node whose name is '$next'
if ($this->children[$i]->name == $next) {
$x = & $child->getNodeAt ($path1);
if (!PEAR ::isError ($x)) {
// No node with that name found
return new PEAR_Error ("Bad path to node: [". implode('/', $path). "]");
* @param string xml Text string to escape.
array ('ü', 'Ü', 'ö',
'Ö', 'ä', 'Ä',
'ß', '<', '>',
"/\#\|\|([a-z\d\#]+)\|\|\#/i",
"/([^a-zA-Z\d\s\<\>\&\;\.\:\=\"\-\/\%\?\!\'\(\)\[\]\{\}\$\#\+\,\@_])/e"
* Decode XML entities in a text string.
* @param string xml Text to decode
* @return string Decoded text
static $trans_tbl = null;
$trans_tbl = get_html_translation_table (HTML_ENTITIES );
$trans_tbl = array_flip ($trans_tbl);
for ($i = 1; $i <= 255; $i++ ) {
return strtr($xml, $trans_tbl);
* Print text representation of XML_Tree_Node.
Documentation generated on Mon, 11 Mar 2019 15:34:45 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|