📄 tree.php
字号:
$this->i = 2;
} else {
// mixed contents
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);
}
$this->cdata = null;
return null;
}
/**
* 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
*
* @access private
*/
function endHandler($xp, $elem)
{
$this->i--;
if ($this->i > 1) {
$obj_id = 'obj' . $this->i;
// recover the node created in StartHandler
$node =& $this->$obj_id;
// mixed contents
if (count($node->children) > 0) {
if (trim($this->cdata) != '') {
$node->children[] = &new XML_Tree_Node(null, $this->cdata);
}
} else {
$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;
} else {
$node =& $this->obj1;
if (count($node->children) > 0) {
if (trim($this->cdata)) {
$node->children[] = &new XML_Tree_Node(null, $this->cdata);
}
} else {
$node->setContent($this->cdata);
}
}
$this->cdata = null;
return null;
}
/**
* 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
*
* @access private
*/
function cdataHandler($xp, $data)
{
$this->cdata .= $data;
}
/**
* Get a copy of this tree by cloning and all of its nodes, recursively.
*
* @return object XML_Tree copy of this node.
* @access public
*/
function cloneTree()
{
$clone = new XML_Tree($this->filename, $this->version);
if (!is_null($this->root)) {
$clone->root = $this->root->cloneTree();
}
// clone all other vars
$temp = get_object_vars($this);
foreach($temp as $varname => $value) {
if (!in_array($varname,array('filename','version','root'))) {
$clone->$varname=$value;
}
}
return $clone;
}
/**
* 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.
*
* @access public
*/
function dump($xmlHeader = false)
{
if ($xmlHeader) {
header('Content-type: text/xml');
}
echo $this->get($this->use_cdata_sections);
}
/**
* Get text representation of XML tree.
*
* @return string Text (XML) representation of the tree
* @access public
*/
function &get()
{
$out = '<?xml version="' . $this->version . "\"?>\n";
if (!is_null($this->root))
{
if(!is_object($this->root) || (strtolower(get_class($this->root)) != 'xml_tree_node'))
return $this->raiseError("Bad XML root node");
$out .= $this->root->get($this->use_cdata_sections);
}
return $out;
}
/**
* Get current namespace.
*
* @param string name namespace
* @return string
*
* @access public
*/
function &getName($name) {
return $this->root->getElement($this->namespace[$name]);
}
/**
* Get A Nodes Namespace
*/
function getNodeNamespace(&$node) {
$name_parts = explode(':',$node->name);
if (sizeof($name_parts) > 1) {
$namespace = $name_parts[0];
} else {
$namespace = '';
}
if (isset($node->namespace[$namespace])) {
return $node->namespace[$namespace];
} elseif (isset($this->root->namespace[$namespace])) {
return $this->root->namespace[$namespace];
} else {
return '';
}
}
/**
* 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.
* @access public
*/
function &getNodeAt($path)
{
if (is_null($this->root)){
//_debug("XML_Tree hasn't got a root node");
return false;
}
if (is_string($path))
$path = explode("/", $path);
if (sizeof($path) == 0) {
//_debug("Path '$path' to node is empty");
return false;
}
$path1 = $path;
$rootName = array_shift($path1);
if ($this->root->name != $rootName) {
//_debug("Path '$path' does not match the document root");
return false;
}
$x =& $this->root->getNodeAt($path1);
if ( $x ) {
return $x;
}
// No node with that name found
//_debug("Bad path '$path' to node 2: [".implode('/', $path)."]");
$x = false;
return $x;
}
/**
* Gets all children that match a given tag name.
*
* @param string Tag name
*
* @return array An array of Node objects of the children found,
* an empty array if none
* @access public
* @author Pierre-Alain Joye <paj@pearfr.org>
*/
function &getElementsByTagName($tagName)
{
if (empty($tagName)) {
return $this->raiseError('Empty tag name');
}
$result = array();
foreach ($this->root->children as $child) {
if ($child->name == $tagName) {
$result[] = $child;
}
}
return $result;
}
/**
* 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.
* @access public
* @author Pierre-Alain Joye <paj@pearfr.org>
* @author Davey Shafik <davey@php.net>
*/
function &getElementsByTagNameFromNode($tagName, &$node)
{
if (empty($tagName)) {
return $this->raiseError('Empty tag name');
}
$result = array();
foreach ($node->children as $child) {
if ($child->name == $tagName) {
$result[] = $child;
}
}
return $result;
}
/**
* Checks if $name is a valid XML name
*
* @static
* @param string $name String to check for validity as an XML Name
* @return mixed
*/
function isValidName($name, $type) {
if (trim($name) == '') {
return true;
}
// check for invalid starting character
if (!preg_match("/[[:alpha:]_]/", $name{0})) {
//return new XML_Parser_Error( ucfirst($type) . " ('$name') has an invalid name, an XML name may only start with a letter or underscore");
return false;
}
if (!preg_match("/^([a-zA-Z_]([a-zA-Z0-9_\-\.]*)?:)?[a-zA-Z_]([a-zA-Z0-9_\-\.]+)?$/", $name)) {
//return new XML_Parser_Error( ucfirst($type) . " ('$name') has an invalid name, an XML name may only contain alphanumeric chars, period, hyphen, colon and underscores");
return false;
}
return true;
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -