📄 xml_domit_parser.php
字号:
function parsedBy() { return $this->parser; } //parsedBy /** * 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() { if ($this->documentElement != null) { $root =& $this->documentElement; return $root->getText(); } return ''; } //getText /** * Returns a doctype object * @return mixed The doctype object, or null if none exists */ function getDocType() { return $this->doctype; } //getDocType /** * Returns the xml declaration processing instruction * @return mixed The xml declaration processing instruction, or null if none exists */ function getXMLDeclaration() { return $this->xmlDeclaration; } //getXMLDeclaration /** * Returns a reference to the DOMIT_DOMImplementation object * @return Object A reference to the DOMIT_DOMImplementation object */ function &getDOMImplementation() { return $this->implementation; } //getDOMImplementation /** * Manages the firing of the onLoad() event * @param Object The parent node of the current recursion */ function load(&$contextNode) { $total = $contextNode->childCount; for ($i = 0; $i < $total; $i++) { $currNode =& $contextNode->childNodes[$i]; $currNode->ownerDocument->load($currNode); } $contextNode->onLoad(); } //load /** * Returns the current version of DOMIT! * @return Object The current version of DOMIT! */ function getVersion() { return DOMIT_VERSION; } //getVersion /** * Appends an array of entity mappings to the existing translation table * * Intended mainly to facilitate the conversion of non-ASCII entities into equivalent characters * * @param array A list of entity mappings in the format: array('&' => '&'); */ function appendEntityTranslationTable($table) { $this->definedEntities = $table; global $DOMIT_defined_entities_flip; $DOMIT_defined_entities_flip = array_flip($table); } //appendEntityTranslationTable /** * Generates an array representation of the node and its children * @return Array A representation of the node and its children */ function toArray() { $arReturn = array($this->nodeName => array()); $total = $this->childCount; for ($i = 0; $i < $total; $i++) { $arReturn[$this->nodeName][$i] = $this->childNodes[$i]->toArray(); } return $arReturn; } //toArray /** * Copies a node and/or its children * @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) { $className = get_class($this); $clone = new $className($this->nodeName); if ($deep) { $total = $this->childCount; for ($i = 0; $i < $total; $i++) { $currentChild =& $this->childNodes[$i]; $clone->appendChild($currentChild->cloneNode($deep)); if ($currentChild->nodeType == DOMIT_DOCUMENT_TYPE_NODE) { $clone->doctype =& $clone->childNodes[$i]; } if (($currentChild->nodeType == DOMIT_PROCESSING_INSTRUCTION_NODE) && ($currentChild->getTarget() == 'xml')) { $clone->xmlDeclaration =& $clone->childNodes[$i]; } } } return $clone; } //cloneNode /** * Generates a string 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 string representation */ function toString($htmlSafe = false, $subEntities = false) { $result = ''; $total = $this->childCount; for ($i = 0; $i < $total; $i++) { $result .= $this->childNodes[$i]->toString(false, $subEntities); } if ($htmlSafe) $result = $this->forHTML($result); return $result; } //toString} //DOMIT_Document/*** A class representing the DOM Element** @package domit-xmlparser* @subpackage domit-xmlparser-main* @author John Heinstein <johnkarl@nbnet.nb.ca>*/class DOMIT_Element extends DOMIT_ChildNodes_Interface { /** @var array An array of namespacesURIs mapped to prefixes */ var $namespaceURIMap = array(); /** * DOM Element constructor * @param string The tag name of the element */ function DOMIT_Element($tagName) { $this->_constructor(); $this->nodeType = DOMIT_ELEMENT_NODE; $this->nodeName = $tagName; $this->attributes = new DOMIT_NamedNodeMap_Attr(); $this->childNodes = array(); } //DOMIT_Element /** * Returns the tag name of the element * @return string The tag name of the element */ function getTagName() { return $this->nodeName; } //getTagName /** * 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) { if (($this->nodeName == $tagName) || ($tagName == '*')) { $nodeList->appendNode($this); } $total = $this->childCount; for ($i = 0; $i < $total; $i++) { $this->childNodes[$i]->getNamedElements($nodeList, $tagName); } } //getNamedElements /** * Creates an xmlns declaration at the current element * @param array An array of namespace declarations in the scope of the current element */ function declareNamespace($localname, $value) { //namespace URI for xmlns attribute is: http://www.w3.org/2000/xmlns/ $this->setAttributeNS(DOMIT_XMLNS_NAMESPACE, ('xmlns:' . $localname), $value); //add to local namespaceURI map $this->namespaceURIMap[$value] = $localname; } //declareNamespace /** * Creates a default xmlns declaration at the current element * @param array An array of namespace declarations in the scope of the current element */ function declareDefaultNamespace($value) { //namespace URI for xmlns attribute is: http://www.w3.org/2000/xmlns/ $this->setAttributeNS(DOMIT_XMLNS_NAMESPACE, 'xmlns', $value); //add to local namespaceURI map $this->namespaceURIMap[$value] = 'xmlns'; } //declareDefaultNamespace /** * Returns an array of namespace declarations in the scope of the current element * @return array An array of namespace declarations in the scope of the current element */ function &getNamespaceDeclarationsInScope() { $nsMap = array(); return $this->_getNameSpaceDeclarationsInScope($nsMap); } //getNamespacesInScope /** * Returns an array of namespace declarations in the scope of the current element * @return array An array of namespace declarations in the scope of the current element */ function &_getNamespaceDeclarationsInScope(&$nsMap) { //grab local xmlns declarations if not already present foreach ($this->namespaceURIMap as $key => $value) { if (!isset($nsMap[$key])) { $nsMap[$key] = $value; } } //move up the tree if not already at the top if ($this->parentNode->uid != $this->ownerDocument->uid) { $this->parentNode->_getNamespaceDeclarationsInScope($nsMap); } return $nsMap; } //_getNamespacesInScope /** * Returns the default xmlns declaration for the current element * @return string The default xmlns declaration for the current element */ function getDefaultNamespaceDeclaration() { if (in_array('xmlns', $this->namespaceURIMap)) { foreach ($this->namespaceURIMap as $key => $value) { if ($value == 'xmlns') { return $key; } } } else if ($this->parentNode->uid != $this->ownerDocument->uid) { return $this->parentNode->getDefaultNamespaceDeclaration(); } else { return ''; } } //getDefaultNamespaceDeclaration /** * Copies all namespace declarations in scope to the namespace URI map of the current element */ function copyNamespaceDeclarationsLocally() { $nsMap = $this->getNamespaceDeclarationsInScope(); //add xmlns declarations as attributes foreach ($nsMap as $key => $value) { if ($value == 'xmlns') { $this->declareDefaultNamespace($key); } else { $this->declareNamespace($value, $key); } } } //copyNamespaceDeclarationsLocally /** * Adds elements with the specified tag name to a NodeList collection * @param Object The NodeList collection * @param string The namespaceURI of matching elements * @param string The localName of matching elements */ function getNamedElementsNS(&$nodeList, $namespaceURI, $localName) { if ((($namespaceURI == $this->namespaceURI) || ($namespaceURI == '*')) && (($localName == $this->localName) || ($localName == '*'))) { $nodeList->appendNode($this); } $total = $this->childCount; for ($i = 0; $i < $total; $i++) { if ($this->childNodes[$i]->nodeType == DOMIT_ELEMENT_NODE) { $this->childNodes[$i]->getNamedElementsNS($nodeList, $namespaceURI, $localName); } } } //getNamedElementsNS /** * 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() { $text = ''; $numChildren = $this->childCount; for ($i = 0; $i < $numChildren; $i++) { $child =& $this->childNodes[$i]; $text .= $child->getText(); } return $text; } //getText /** * If a child text node exists, sets the nodeValue to $data. A child text node is created if none exists * @param string The text data of the node */ function setText($data) { switch ($this->childCount) { case 1: if ($this->firstChild->nodeType == DOMIT_TEXT_NODE) { $this->firstChild->setText($data); } break; case 0: $childTextNode =& $this->ownerDocument->createTextNode($data); $this->appendChild($childTextNode); break; default: //do nothing. Maybe throw error??? } } //setText /** * Retrieves a NodeList of child elements with the specified tag name * @param string The matching element tag name * @return Object A NodeList of found elements */ function &getElementsByTagName($tagName) { $nodeList = new DOMIT_NodeList(); $this->getNamedElements($nodeList, $tagName); return $nodeList; } //getElementsByTagName /** * Retrieves a NodeList of child elements with the specified namespaceURI and localName * @param string The namespaceURI * @param string The localName * @return Object A NodeList of found elements */ function &getElementsByTagNameNS($namespaceURI, $localName) { $nodeList = new DOMIT_NodeList(); $this->getNamedElementsNS($nodeList, $namespaceURI, $localName); return $nodeList; } //getElementsByTagNameNS /** * Returns the attribute node whose ID is given by elementId. * @param string The id of the matching element * @param boolean True if XML spec is to be strictly adhered to (only attributes xml:id are considered valid) * @return Object The found attribute or null */ function &_getElementByID($elementID, $isStrict) { if ($isStrict) { $myAttrNode =& $this->getAttributeNodeNS(DOMIT_XML_NAMESPACE, 'id'); if (($myAttrNode != null)&& ($myAttrNode->getValue() == $elementID)) return $myAttrNode; } else { $myAttrNode =& $this->getAttributeNodeNS('', 'ID'); if (($myAttrNode != null)&& ($myAttrNode->getValue() == $elementID)) return $myAttrNode; $myAttrNode =& $this->getAttributeNodeNS('', 'id'); if (($myAttrNode != null)&& ($myAttrNode->getValue() == $elementID)) return $myAttrNode; } $total = $this->childCount; for ($i = 0; $i < $total; $i++) { if ($this->childNodes[$i]->nodeType == DOMIT_ELEMENT_NODE) { $foundNode =& $this->childNodes[$i]->_getElementByID($elementID, $isStrict); if ($foundNode != null) { return $foundNode; } } } $null = null; return $null; } //_getElementByID /** * Retrieves an element or DOMIT_NodeList of elements corresponding to an Xpath-like expression. * @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) { require_once(DOMIT_INCLUDE_PATH . 'xml_domit_getelementsbypath.php'); $gebp = new DOMIT_GetElementsByPath(); $myResponse =& $gebp->parsePattern($this, $pattern, $nodeIndex); return $myResponse; } //getElementsByPath /** * Retrieves an element or DOMIT_NodeList of elements corresponding to an Xpath-like attribute expression (NOT YET IMPLEMENTED!) * @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 &getElementsByAtt
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -