📄 node.php
字号:
* @return mixed The removed node, or PEAR_Error upon removal error.
* @access public
*/
function &removeChild($pos)
{
if (($pos < -count($this->children)) || ($pos >= count($this->children))) {
//return new PEAR_Error("Invalid remove position.");
return false;
}
// Using array_splice() instead of a simple unset() to maintain index-integrity
return array_splice($this->children, $pos, 1);
}
/**
* Register a namespace.
*
* @param string $name namespace
* @param string $path path
*
* @access public
*/
function registerName($name, $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.
* @access public
*/
function &get($use_cdata_section = false)
{
static $deep = -1;
static $do_ident = true;
$deep++;
$empty = false;
$ident = str_repeat(' ', $deep);
if ($this->name !== null) {
if ($do_ident) {
$out = $ident . '<' . $this->name;
} else {
$out = '<' . $this->name;
}
foreach ($this->attributes as $name => $value) {
$out .= ' ' . $name . '="' . $value . '"';
}
if (isset($this->namespace) && (is_array($this->namespace))) {
foreach ($this->namespace as $qualifier => $uri) {
if ($qualifier == '') {
$out .= " xmlns='$uri'";
} else {
$out .= " xmlns:$qualifier='$uri'";
}
}
}
if ($this->content == '' && sizeof($this->children) === 0 && $deep != 0) {
$out .= ' />';
$empty = true;
} else {
$out .= '>';
if ($this->use_cdata_section == true || ($use_cdata_section == true && $this->use_cdata_section !== false)) {
if (trim($this->content) != '') {
$out .= '<![CDATA[' .$this->content. ']]>';
}
} else {
if (trim($this->content) != '') {
$out .= $this->content;
}
}
}
if (sizeof($this->children) > 0) {
$out .= "\n";
foreach ($this->children as $child) {
$out .= $child->get($use_cdata_section);
}
} else {
$ident = '';
}
if ($do_ident && $empty != true) {
$out .= $ident . '</' . $this->name . ">\n";
} elseif ($empty != true) {
$out .= '</' . $this->name . '>';
}
$do_ident = true;
} else {
if ($this->use_cdata_section == true || ($use_cdata_section == true && $this->use_cdata_section !== false)) {
if (trim($this->content) != '') {
$out = $ident . '<![CDATA[' .$this->content. ']]>' . "\n";
}
} else {
if (trim($this->content) != '') {
$out = $ident . $this->content . "\n";
}
}
}
$deep--;
return $out;
}
/**
* Get an attribute by its name.
*
* @param string $name Name of attribute to retrieve
*
* @return string attribute, or null if attribute is unset.
* @access public
*/
function getAttribute($name)
{
if (isset($this->attributes[$name])) {
return $this->attributes[$name];
}
return null;
}
/**
* Sets an attribute for this node.
*
* @param string name Name of attribute to set
* @param string value Value of attribute
*
* @access public
*/
function setAttribute($name, $value = '')
{
$this->attributes[$name] = $value;
}
/**
* Unsets an attribute of this node.
*
* @param string $name Name of attribute to unset
*
* @access public
*/
function unsetAttribute($name)
{
if (isset($this->attributes[$name])) {
unset($this->attributes[$name]);
}
}
/**
* Sets the content for this node.
*
* @param string content Node content to assign
*
* @access public
*/
function setContent($content, $use_cdata_section = null)
{
$this->use_cdata_section = $use_cdata_section;
if ($use_cdata_section == true) {
$this->content = $content;
} else {
$this->content = $this->encodeXmlEntities($content);
}
}
/**
* 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
* be found.
* @access public
*/
function &getElement($path)
{
if (!is_array($path)) {
$path = array($path);
}
if (sizeof($path) == 0) {
return $this;
}
$path1 = $path;
$next = array_shift($path1);
if (isset($this->children[$next])) {
$x =& $this->children[$next]->getElement($path1);
if (!$x) {
return $x;
}
}
//return new PEAR_Error("Bad path to node: [".implode('-', $path)."]");
return false;
}
/**
* 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
* returned.
* @access public
*/
function &getNodeAt($path)
{
if (is_string($path))
$path = explode("/", $path);
if (sizeof($path) == 0) {
return $this;
}
$path1 = $path;
$next = array_shift($path1);
// Get the first children of this node whose name is '$next'
$child = null;
for ($i = 0; $i < count($this->children); $i++) {
if ($this->children[$i]->name == $next) {
$child =& $this->children[$i];
break;
}
}
if (!is_null($child)) {
$x =& $child->getNodeAt($path1);
if ($x) {
return $x;
}
}
// No node with that name found
//_debug("Bad path to node: [".implode('/', $path)."]");
// the line below is here to avoid a warning message...
$x = false;
return $x;
}
/**
* Escape XML entities.
*
* @param string xml Text string to escape.
*
* @return string xml
* @access public
*/
function encodeXmlEntities($xml)
{
$xml = str_replace(array('
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -