📄 xml_domit_lite_parser.php
字号:
* @return Object A NodeList of found elements */ function &getElementsByTagName($tagName) { $nodeList = new DOMIT_NodeList(); if ($this->documentElement != null) { $this->documentElement->getNamedElements($nodeList, $tagName); } return $nodeList; } //getElementsByTagName /** * 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 /** * Parses an xml string; first encodes string as UTF-8 * @param string The xml text to be parsed * @param boolean True if SAXY is to be used instead of Expat * @param boolean False if CDATA Section are to be generated as Text nodes * @param boolean True if onLoad is to be called on each node after parsing * @return boolean True if parsing is successful */ function parseXML_utf8($xmlText, $useSAXY = true, $preserveCDATA = true, $fireLoadEvent = false) { return $this->parseXML(utf8_encode($xmlText), $useSAXY, $preserveCDATA, $fireLoadEvent); } //parseXML_utf8 /** * Parses an xml string * @param string The xml text to be parsed * @param boolean True if SAXY is to be used instead of Expat * @param boolean False if CDATA Section are to be generated as Text nodes * @param boolean True if onLoad is to be called on each node after parsing * @return boolean True if parsing is successful */ function parseXML($xmlText, $useSAXY = true, $preserveCDATA = true, $fireLoadEvent = false) { require_once(DOMIT_INCLUDE_PATH . 'xml_domit_utilities.php'); if ($this->doResolveErrors) { require_once(DOMIT_INCLUDE_PATH . 'xml_domit_doctor.php'); $xmlText = DOMIT_Doctor::fixAmpersands($xmlText); } if (DOMIT_Utilities::validateXML($xmlText)) { $domParser = new DOMIT_Lite_Parser(); if ($useSAXY || (!function_exists('xml_parser_create'))) { //use SAXY parser to populate xml tree $this->parser = 'SAXY_LITE'; $success = $domParser->parseSAXY($this, $xmlText, $preserveCDATA, $this->definedEntities); } else { //use Expat parser to populate xml tree $this->parser = 'EXPAT'; $success = $domParser->parse($this, $xmlText, $preserveCDATA); } if ($fireLoadEvent && ($this->documentElement != null)) $this->load($this->documentElement); return $success; } return false; } //parseXML /** * Parses an xml file; first encodes text as UTF-8 * @param string The xml file to be parsed * @param boolean True if SAXY is to be used instead of Expat * @param boolean False if CDATA Section are to be generated as Text nodes * @param boolean True if onLoad is to be called on each node after parsing * @return boolean True if parsing is successful */ function loadXML_utf8($filename, $useSAXY = true, $preserveCDATA = true, $fireLoadEvent = false) { $xmlText = $this->getTextFromFile($filename); return $this->parseXML_utf8($xmlText, $useSAXY, $preserveCDATA, $fireLoadEvent); } //loadXML_utf8 /** * Parses an xml file * @param string The xml file to be parsed * @param boolean True if SAXY is to be used instead of Expat * @param boolean False if CDATA Section are to be generated as Text nodes * @param boolean True if onLoad is to be called on each node after parsing * @return boolean True if parsing is successful */ function loadXML($filename, $useSAXY = true, $preserveCDATA = true, $fireLoadEvent = false) { $xmlText = $this->getTextFromFile($filename); return $this->parseXML($xmlText, $useSAXY, $preserveCDATA, $fireLoadEvent); } //loadXML /** * Establishes a connection, given an url * @param string The url of the data */ function establishConnection($url) { require_once(DOMIT_INCLUDE_PATH . 'php_http_client_generic.php'); $host = php_http_connection::formatHost($url); $host = substr($host, 0, strpos($host, '/')); $this->setConnection($host); } //establishConnection /** * Retrieves text from a file * @param string The file path * @return string The text contained in the file */ function getTextFromFile($filename) { if ($this->doUseHTTPClient && (substr($filename, 0, 5) == 'http:')) { $this->establishConnection($filename); } if ($this->httpConnection != null) { $response =& $this->httpConnection->get($filename); $this->httpConnection->disconnect(); return $response->getResponse(); } else if (function_exists('file_get_contents')) { //if (file_exists($filename)) { return file_get_contents($filename); //} } else { require_once(DOMIT_INCLUDE_PATH . 'php_file_utilities.php'); $fileContents =& php_file_utilities::getDataFromFile($filename, 'r'); return $fileContents; } return ''; } //getTextFromFile /** * Saves the current DOM document as an xml file; first encodes text as UTF-8 * @param string The path of the xml file * @param boolean True if xml text is to be normalized before saving * @return boolean True if save is successful */ function saveXML_utf8($filename, $normalized=false) { if ($normalized) { $stringRep = $this->toNormalizedString(false, true); //param 2 is $subEntities } else { $stringRep = $this->toString(false, true); } return $this->saveTextToFile($filename, utf8_encode($stringRep)); } //saveXML_utf8 /** * Saves the current DOM document as an xml file * @param string The path of the xml file * @param boolean True if xml text is to be normalized before saving * @return boolean True if save is successful */ function saveXML($filename, $normalized=false) { if ($normalized) { $stringRep = $this->toNormalizedString(false, true); } else { $stringRep = $this->toString(false, true); } if ($this->xmlDeclaration) { $stringRep = $this->xmlDeclaration . "\n" . $stringRep; } return $this->saveTextToFile($filename, $stringRep); } //saveXML /** * Saves text to a file * @param string The file path * @param string The text to be saved * @return boolean True if the save is successful */ function saveTextToFile($filename, $text) { if (function_exists('file_put_contents')) { file_put_contents($filename, $text); } else { require_once(DOMIT_INCLUDE_PATH . 'php_file_utilities.php'); php_file_utilities::putDataToFile($filename, $text, 'w'); } return (file_exists($filename) && is_writable($filename)); } //saveTextToFile /** * Indicates the SAX parser used to parse the current document * @return string Either "SAXY_LITE" or "EXPAT" */ 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 the doctype text * @return string The doctype text, or an emty string */ function getDocType() { return $this->doctype; } //getDocType /** * Returns the xml declaration text * @return mixed The xml declaration text, or an empty string */ function getXMLDeclaration() { return $this->xmlDeclaration; } //getXMLDeclaration /** * Returns the xml declaration text * @return mixed The xml declaration text, or an empty string */ function setXMLDeclaration( $decl ) { $this->xmlDeclaration = $decl; } //setXMLDeclaration /** * 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! Lite * @return Object The current version of DOMIT! Lite */ function getVersion() { return DOMIT_LITE_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)); } } 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_Lite_Document/*** A class representing the DOM Element** @package domit-xmlparser* @subpackage domit-xmlparser-lite* @author John Heinstein <johnkarl@nbnet.nb.ca>*/class DOMIT_Lite_Element extends DOMIT_Lite_ChildNodes_Interface { /** * DOM Element constructor * @param string The tag name of the element */ function DOMIT_Lite_Element($tagName) { $this->_constructor(); $this->nodeType = DOMIT_ELEMENT_NODE; $this->nodeName = $tagName; $this->attributes = array(); $this->childNodes = array(); } //DOMIT_Lite_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 /** * 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 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 /** * Gets the value of the specified attribute, if it exists * @param string The attribute name * @return string The attribute value */ function getAttribute($name) { if ($this->hasAttribute($name)) { return $this->attributes[$name]; } else { /* DOMIT_DOMException::raiseException(DOMIT_NOT_FOUND_ERR, ('No attribute named ' . $name . 'exists.')); */ // Joomla! hack return null; } } //getAttribute
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -