📄 xml_domit_parser.php
字号:
<?php/*** DOMIT! is a non-validating, but lightweight and fast DOM parser for PHP* @package domit-xmlparser* @subpackage domit-xmlparser-main* @version 1.01* @copyright (C) 2004 John Heinstein. All rights reserved* @license http://www.gnu.org/copyleft/lesser.html LGPL License* @author John Heinstein <johnkarl@nbnet.nb.ca>* @link http://www.engageinteractive.com/domit/ DOMIT! Home Page* DOMIT! is Free Software**/if (!defined('DOMIT_INCLUDE_PATH')) { define('DOMIT_INCLUDE_PATH', (dirname(__FILE__) . "/"));}/** current version of DOMIT! */define ('DOMIT_VERSION', '1.01');/** XML namespace URI */define ('DOMIT_XML_NAMESPACE', 'http://www.w3.org/xml/1998/namespace');/** XMLNS namespace URI */define ('DOMIT_XMLNS_NAMESPACE', 'http://www.w3.org/2000/xmlns/');/*** @global array Flipped version of $definedEntities array, to allow two-way conversion of entities** Made global so that Attr nodes, which have no ownerDocument property, can access the array*/$GLOBALS['DOMIT_defined_entities_flip'] = array();require_once(DOMIT_INCLUDE_PATH . 'xml_domit_shared.php');/*** The base class of all DOMIT node types** @package domit-xmlparser* @subpackage domit-xmlparser-main* @author John Heinstein <johnkarl@nbnet.nb.ca>*/class DOMIT_Node { /** @var string The name of the node, varies according to node type */ var $nodeName = null; /** @var string The value of the node, varies according to node type */ var $nodeValue = null; /** @var int The type of node, e.g. CDataSection */ var $nodeType = null; /** @var Object A reference to the parent of the current node */ var $parentNode = null; /** @var Array An array of child node references */ var $childNodes = null; /** @var Object A reference to the first node in the childNodes list */ var $firstChild = null; /** @var Object A reference to the last node in the childNodes list */ var $lastChild = null; /** @var Object A reference to the node prior to the current node in its parents childNodes list */ var $previousSibling = null; /** @var Object A reference to the node after the current node in its parents childNodes list */ var $nextSibling = null; /** @var Object A NodeList of attribute nodes */ var $attributes = null; /** @var Object A reference to the Document node */ var $ownerDocument = null; /** @var String A URI that identifies the XML namespace to which the node belongs */ var $namespaceURI = null; /** @var String The namespace prefix for the node */ var $prefix = null; /** @var String The local name of the node */ var $localName = null; /** @var string The unique node id */ var $uid; /** @var int The number of children of the current node */ var $childCount = 0; /** * Raises error if abstract class is directly instantiated */ function DOMIT_Node() { DOMIT_DOMException::raiseException(DOMIT_ABSTRACT_CLASS_INSTANTIATION_ERR, 'Cannot instantiate abstract class DOMIT_Node'); } //DOMIT_Node /** * DOMIT_Node constructor, assigns a uid */ function _constructor() { global $uidFactory; $this->uid = $uidFactory->generateUID(); } //_constructor /** * Appends a node to the childNodes list of the current node * @abstract * @param Object The node to be appended * @return Object The appended node */ function &appendChild(&$child) { DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR, ('Method appendChild cannot be called by class ' . get_class($this))); } //appendChild /** * Inserts a node to the childNodes list of the current node * @abstract * @param Object The node to be inserted * @param Object The node before which the insertion is to occur * @return Object The inserted node */ function &insertBefore(&$newChild, &$refChild) { DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR, ('Method insertBefore cannot be called by class ' . get_class($this))); } //insertBefore /** * Replaces a node with another * @abstract * @param Object The new node * @param Object The old node * @return Object The new node */ function &replaceChild(&$newChild, &$oldChild) { DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR, ('Method replaceChild cannot be called by class ' . get_class($this))); } //replaceChild /** * Removes a node from the childNodes list of the current node * @abstract * @param Object The node to be removed * @return Object The removed node */ function &removeChild(&$oldChild) { DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR, ('Method removeChild cannot be called by class ' . get_class($this))); } //removeChild /** * Returns the index of the specified node in a childNodes list * @param Array The childNodes array to be searched * @param Object The node targeted by the search * @return int The index of the target node, or -1 if not found */ function getChildNodeIndex(&$arr, &$child) { $index = -1; $total = count($arr); for ($i = 0; $i < $total; $i++) { if ($child->uid == $arr[$i]->uid) { $index = $i; break; } } return $index; } //getChildNodeIndex /** * Determines whether a node has any children * @return boolean True if any child nodes are present */ function hasChildNodes() { return ($this->childCount > 0); } //hasChildNodes /** * Determines whether a node has any attributes * @return boolean True if the node has attributes */ function hasAttributes() { //overridden in DOMIT_Element return false; } //hasChildNodes /** * Collapses adjacent text nodes in entire node subtree */ function normalize() { if (($this->nodeType == DOMIT_DOCUMENT_NODE) && ($this->documentElement != null)) { $this->documentElement->normalize(); } } //normalize /** * Copies a node and/or its children * @abstract * @param boolean True if all child nodes are also to be cloned * @return Object A copy of the node and/or its children */ function &cloneNode($deep = false) { DOMIT_DOMException::raiseException(DOMIT_ABSTRACT_METHOD_INVOCATION_ERR, 'Cannot invoke abstract method DOMIT_Node->cloneNode($deep). Must provide an overridden method in your subclass.'); } //cloneNode /** * Adds elements with the specified tag name to a NodeList collection * @param Object The NodeList collection * @param string The tag name of matching elements */ function getNamedElements(&$nodeList, $tagName) { //Implemented in DOMIT_Element. //Needs to be here though! This is called against all nodes in the document. } //getNamedElements /** * Sets the ownerDocument property of a node to the containing DOMIT_Document * @param Object A reference to the document element of the DOMIT_Document */ function setOwnerDocument(&$rootNode) { if ($rootNode->ownerDocument == null) { unset($this->ownerDocument); $this->ownerDocument = null; } else { $this->ownerDocument =& $rootNode->ownerDocument; } $total = $this->childCount; for ($i = 0; $i < $total; $i++) { $this->childNodes[$i]->setOwnerDocument($rootNode); } } //setOwnerDocument /** * Tests whether a value is null, and if so, returns a default value * @param mixed The value to be tested * @param mixed The default value * @return mixed The specified value, or the default value if null */ function &nvl(&$value,$default) { if (is_null($value)) return $default; return $value; } //nvl /** * Retrieves an element or DOMIT_NodeList of elements corresponding to an Xpath-like expression. * @abstract * @param string The query pattern * @param int If a single node is to be returned (rather than the entire NodeList) the index of that node * @return mixed A NodeList or single node that matches the pattern */ function &getElementsByPath($pattern, $nodeIndex = 0) { DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR, ('Method getElementsByPath cannot be called by class ' . get_class($this))); } //getElementsByPath /** * Retrieves an element or DOMIT_NodeList of elements corresponding to an Xpath-like attribute expression (NOT YET IMPLEMENTED!) * @abstract * @param string The query pattern * @param int If a single node is to be returned (rather than the entire NodeList) the index of that node * @return mixed A NodeList or single node that matches the pattern */ function &getElementsByAttributePath($pattern, $nodeIndex = 0) { DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR, ('Method getElementsByAttributePath cannot be called by class ' . get_class($this))); } //getElementsByAttributePath /** * Adds all child nodes of the specified nodeType to the NodeList * @abstract * @param Object The NodeList collection * @param string The nodeType of matching nodes */ function getTypedNodes(&$nodeList, $type) { DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR, ('Method getTypedNodes cannot be called by class ' . get_class($this))); } //getTypedNodes /** * Adds all child nodes of the specified nodeValue to the NodeList * @abstract * @param Object The NodeList collection * @param string The nodeValue of matching nodes */ function getValuedNodes(&$nodeList, $value) { DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR, ('Method getValuedNodes cannot be called by class ' . get_class($this))); } //getValuedNodes /** * Returns the concatented text of the current node and its children * @return string The concatented text of the current node and its children */ function getText() { return $this->nodeValue; } //getText /** * Indicates whether the specified feature is supported by the DOM implementation and this node * @param string The feature * @param string The version of the DOM implementation * @return boolean True if the specified feature is supported */ function isSupported($feature, $version = null) { //don't worry about parsing based on version at this point in time; //the only feature that is supported is 'XML'... if (($version == '1.0') || ($version == '2.0') || ($version == null)) { if (strtoupper($feature) == 'XML') { return true; } } return false; } //isSupported /** * Formats a string for presentation as HTML * @param string The string to be formatted * @param boolean True if the string is to be sent directly to output * @return string The HTML formatted string */ function forHTML($str, $doPrint = false) { require_once(DOMIT_INCLUDE_PATH . 'xml_domit_utilities.php'); return DOMIT_Utilities::forHTML($str, $doPrint); } //forHTML /** * Generates an array representation of the node and its children * @abstract * @return Array A representation of the node and its children */ function toArray() { DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR, ('Method toArray cannot be called by class ' . get_class($this))); } //toArray /** * A node event that can be set to fire upon document loading, used for node initialization * @abstract */ function onLoad() { //you can override this method if you subclass any of the //DOMIT_Nodes. It's a way of performing //initialization of your subclass as soon as the document //has been loaded (as opposed to as soon as the current node //has been instantiated). } //onLoad /** * Clears previousSibling, nextSibling, and parentNode references from a node that has been removed */ function clearReferences() { if ($this->previousSibling != null) { unset($this->previousSibling); $this->previousSibling = null; } if ($this->nextSibling != null) { unset($this->nextSibling); $this->nextSibling = null; } if ($this->parentNode != null) { unset($this->parentNode); $this->parentNode = null; } } //clearReferences /** * Removes the node from the document */ function delete () { if ($this->parentNode != null) { $this->parentNode->removeChild($node); } } //delete /** * Generates a normalized (formatted for readability) representation of the node and its children * @param boolean True if HTML readable output is desired * @param boolean True if illegal xml characters in text nodes and attributes should be converted to entities * @return string The formatted string representation */ function toNormalizedString($htmlSafe = false, $subEntities = false) { //require this file for generating a normalized (readable) xml string representation require_once(DOMIT_INCLUDE_PATH . 'xml_domit_utilities.php'); global $DOMIT_defined_entities_flip; $result = DOMIT_Utilities::toNormalizedString($this, $subEntities, $DOMIT_defined_entities_flip); if ($htmlSafe) $result = $this->forHTML($result); return $result; } //toNormalizedString} //DOMIT_Node/*** A parent class for nodes which possess child nodes** @package domit-xmlparser* @subpackage domit-xmlparser-main* @author John Heinstein <johnkarl@nbnet.nb.ca>*/class DOMIT_ChildNodes_Interface extends DOMIT_Node { /** * Raises error if abstract class is directly instantiated */ function DOMIT_ChildNodes_Interface() { DOMIT_DOMException::raiseException(DOMIT_ABSTRACT_CLASS_INSTANTIATION_ERR, 'Cannot instantiate abstract class DOMIT_ChildNodes_Interface'); } //DOMIT_ChildNodes_Interface /** * Appends a node to the childNodes list of the current node * @param Object The node to be appended * @return Object The appended node */ function &appendChild(&$child) { if ($child->nodeType == DOMIT_ATTRIBUTE_NODE) { DOMIT_DOMException::raiseException(DOMIT_HIERARCHY_REQUEST_ERR, ('Cannot add a node of type ' . get_class($child) . ' using appendChild')); } else if ($child->nodeType == DOMIT_DOCUMENT_FRAGMENT_NODE) { $total = $child->childCount; for ($i = 0; $i < $total; $i++) { $currChild =& $child->childNodes[$i]; $this->appendChild($currChild); } } else { if (!($this->hasChildNodes())) { $this->childNodes[0] =& $child;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -