📄 xml_domit_lite_parser.php
字号:
/** * Sets the value of the specified attribute; creates a new attribute if one doesn't exist * @param string The attribute name * @param string The desired attribute value */ function setAttribute($name, $value) { $this->attributes[$name] = $value; } //setAttribute /** * Removes the specified attribute * @param string The name of the attribute to be removed */ function removeAttribute($name) { if ($this->hasAttribute($name)) { unset($this->attributes[$name]); } } //removeAttribute /** * Determines whether an attribute with the specified name exists * @param string The name of the attribute * @return boolean True if the attribute exists */ function hasAttribute($name) { return isset($this->attributes[$name]); } //hasAttribute /** * Collapses adjacent text nodes in entire element subtree */ function normalize() { if ($this->hasChildNodes()) { $currNode =& $this->childNodes[0]; while ($currNode->nextSibling != null) { $nextNode =& $currNode->nextSibling; if (($currNode->nodeType == DOMIT_TEXT_NODE) && ($nextNode->nodeType == DOMIT_TEXT_NODE)) { $currNode->nodeValue .= $nextNode->nodeValue; $this->removeChild($nextNode); } else { $currNode->normalize(); } if ($currNode->nextSibling != null) { $currNode =& $currNode->nextSibling; } } } } //normalize /** * 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("attributes" => $this->attributes)); $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); $clone->attributes = $this->attributes; 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) { require_once(DOMIT_INCLUDE_PATH . 'xml_domit_utilities.php'); global $DOMIT_defined_entities_flip; $result = '<' . $this->nodeName; //get attributes foreach ($this->attributes as $key => $value) { $result .= ' ' . $key . '="'; $result .= $subEntities ? DOMIT_Utilities::convertEntities($value, $DOMIT_defined_entities_flip) : $value; $result .= '"'; } //get children $myNodes =& $this->childNodes; $total = count($myNodes); if ($total != 0) { $result .= '>'; for ($i = 0; $i < $total; $i++) { $child =& $myNodes[$i]; $result .= $child->toString(false, $subEntities); } $result .= '</' . $this->nodeName . '>'; } else { if ($this->ownerDocument->doExpandEmptyElementTags) { if (in_array($this->nodeName, $this->ownerDocument->expandEmptyElementExceptions)) { $result .= ' />'; } else { $result .= '></' . $this->nodeName . '>'; } } else { if (in_array($this->nodeName, $this->ownerDocument->expandEmptyElementExceptions)) { $result .= '></' . $this->nodeName . '>'; } else { $result .= ' />'; } } } if ($htmlSafe) $result = $this->forHTML($result); return $result; } //toString} //DOMIT_Lite_Element/*** A class representing the DOM Text Node** @package domit-xmlparser* @subpackage domit-xmlparser-lite* @author John Heinstein <johnkarl@nbnet.nb.ca>*/class DOMIT_Lite_TextNode extends DOMIT_Lite_Node { /** * DOM Text Node constructor * @param string The text of the node */ function DOMIT_Lite_TextNode($data) { $this->_constructor(); $this->nodeType = DOMIT_TEXT_NODE; $this->nodeName = '#text'; $this->setText($data); } //DOMIT_Lite_TextNode /** * Returns the text contained in the current node * @return string The text of the current node */ function getText() { return $this->nodeValue; } //getText /** * Sets the text contained in the current node to $data. * @param string The text data of the node */ function setText($data) { $this->nodeValue = $data; } //setText /** * Generates an array representation of the node and its children * @return Array A representation of the node and its children */ function toArray() { return $this->toString(); } //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->nodeValue); 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 should be converted to entities * @return string The string representation */ function toString($htmlSafe = false, $subEntities = false) { require_once(DOMIT_INCLUDE_PATH . 'xml_domit_utilities.php'); global $DOMIT_defined_entities_flip; $result = $subEntities ? DOMIT_Utilities::convertEntities($this->nodeValue, $DOMIT_defined_entities_flip) : $this->nodeValue; if ($htmlSafe) $result = $this->forHTML($result); return $result; } //toString} //DOMIT_Lite_TextNode/*** A class representing the DOM CDATA Section** @package domit-xmlparser* @subpackage domit-xmlparser-lite* @author John Heinstein <johnkarl@nbnet.nb.ca>*/class DOMIT_Lite_CDATASection extends DOMIT_Lite_TextNode { /** * DOM CDATA Section node constructor * @param string The text of the node */ function DOMIT_Lite_CDATASection($data) { $this->_constructor(); $this->nodeType = DOMIT_CDATA_SECTION_NODE; $this->nodeName = '#cdata-section'; $this->setText($data); } //DOMIT_Lite_CDATASection /** * 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 should be converted to entities * @return string The string representation */ function toString($htmlSafe = false, $subEntities = false) { $result = '<![CDATA['; $result .= $subEntities ? str_replace("]]>", "]]>", $this->nodeValue) : $this->nodeValue; $result .= ']]>'; if ($htmlSafe) $result = $this->forHTML($result); return $result; } //toString} //DOMIT_Lite_CDATASection/*** Manages the generation of a DOMIT! document from SAX events** @package domit-xmlparser* @subpackage domit-xmlparser-lite* @author John Heinstein <johnkarl@nbnet.nb.ca>*/class DOMIT_Lite_Parser { /** @var Object A reference to the resulting xmldoc */ var $xmlDoc = null; /** @var Object A reference to the current node in the parsing process */ var $currentNode = null; /** @var Object A reference to the last child in the parsing process */ var $lastChild = null; /** @var boolean True if currently parsing a CDATA Section */ var $inCDATASection = false; //flag for Expat /** @var boolean True if currently parsing a Text node */ var $inTextNode = false; /** @var boolean True is CDATA Section nodes are not to be converted into Text nodes */ var $preserveCDATA; /** @var string A container for holding the currently parsed text data */ var $parseContainer = ''; /** @var string The current docutype text */ var $parseItem = ''; /** * Parses xml text using Expat * @param Object A reference to the DOM document that the xml is to be parsed into * @param string The text to be parsed * @param boolean True if CDATA Section nodes are not to be converted into Text nodes * @return boolean True if the parsing is successful */ function parse (&$myXMLDoc, $xmlText, $preserveCDATA = true) { $this->xmlDoc =& $myXMLDoc; $this->lastChild =& $this->xmlDoc; $this->preserveCDATA = $preserveCDATA; //create instance of expat parser (should be included in php distro) if (version_compare(phpversion(), '5.0', '<=')) { $parser = xml_parser_create(''); } else { $parser = xml_parser_create(); } //set handlers for SAX events xml_set_object($parser, $this); xml_set_element_handler($parser, 'startElement', 'endElement'); xml_set_character_data_handler($parser, 'dataElement'); xml_set_default_handler($parser, 'defaultDataElement'); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); if (!$this->xmlDoc->preserveWhitespace) { xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); } else { xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0); } //parse out whitespace - (XML_OPTION_SKIP_WHITE = 1 does not //seem to work consistently across versions of PHP and Expat if (!$this->xmlDoc->preserveWhitespace) { $xmlText = eregi_replace('>' . "[[:space:]]+" . '<' , '><', $xmlText); } $success = xml_parse($parser, $xmlText); $this->xmlDoc->errorCode = xml_get_error_code($parser); $this->xmlDoc->errorString = xml_error_string($this->xmlDoc->errorCode); xml_parser_free($parser); return $success; } //parse /** * Parses xml text using SAXY * @param Object A reference to the DOM document that the xml is to be parsed into * @param string The text to be parsed * @param boolean True if CDATA Section nodes are not to be converted into Text nodes * @return boolean True if the parsing is successful */ function parseSAXY(&$myXMLDoc, $xmlText, $preserveCDATA, $definedEntities) { require_once(DOMIT_INCLUDE_PATH . 'xml_saxy_lite_parser.php'); $this->xmlDoc =& $myXMLDoc; $this->lastChild =& $this->xmlDoc; //create instance of SAXY parser $parser = new SAXY_Lite_Parser(); $parser->appendEntityTranslationTable($definedEntities); //not yet implemented in SAXY!!! $parser->preserveWhitespace = $this->xmlDoc->preserveWhitespace; $parser->xml_set_element_handler(array(&$this, 'startElement'), array(&$this, 'endElement')); $parser->xml_set_character_data_handler(array(&$this, 'dataElement')); if ($preserveCDATA) { $parser->xml_set_cdata_section_handler(array(&$this, 'cdataElement')); } $success = $parser->parse($xmlText); $this->xmlDoc->errorCode = $parser->xml_get_error_code(); $this->xmlDoc->errorString = $parser->xml_error_string($this->xmlDoc->errorCode); return $success; } //parseSAXY /** * Generates and appends a new text node from the parseContainer text */ function dumpTextNode() { //traps for mixed content $currentNode =& $this->xmlDoc->createTextNode($this->parseContainer); $this->lastChild->appendChild($currentNode); $this->inTextNode = false; $this->parseContainer = ''; } //dumpTextNode /** * Catches a start element event and processes the data * @param Object A reference to the current SAX parser * @param string The tag name of the current element * @param Array An array of the element attributes */ function startElement(&$parser, $name, $attrs) { if ($this->inTextNode) { $this->dumpTextNode(); } $currentNode =& $this->xmlDoc->createElement($name); $currentNode->attributes = $attrs; $this->lastChild->appendChild($currentNode); $this->lastChild =& $currentNode; } //startElement /** * Catches an end element event and processes the data * @param Object A reference to the current SAX parser * @param string The tag name of the current element */ function endElement(&$parser, $name) { if ($this->inTextNode) { $this->dumpTextNode(); } $this->lastChild =& $this->lastChild->parentNode; } //endElement /** * Catches a data event and processes the text * @param Object A reference to the current SAX parser * @param string The current text data */ function dataElement(&$parser, $data) { if (!$this->inCDATASection) $this->inTextNode = true; $this->parseContainer .= $data; } //dataElement /** * Catches a CDATA Section event and processes the text * @param Object A reference to the current SAX parser * @param string The current text data */ function cdataElement(&$parser, $data) { $currentNode =& $this->xmlDoc->createCDATASection($data); $this->lastChild->appendChild($currentNode); } //cdataElement /** * Catches a default data event and processes the data * @param Object A reference to the current SAX parser * @param string The current data */ function defaultDataElement(&$parser, $data) { if (strlen($data) > 2){ $pre = strtoupper(substr($data, 0, 3)); switch ($pre) { case '<![': //cdata section coming if ($this->preserveCDATA) { $this->inCDATASection = true; } break; case ']]>': //cdata remnant - ignore if ($this->preserveCDATA) { $currentNode =& $this->xmlDoc->createCDATASection($this->parseContainer); $this->lastChild->appendChild($currentNode); $this->inCDATASection = false; $this->parseContainer = ''; } else { $this->dumpTextNode(); } break; } } } //defaultDataElement} //DOMIT_Lite_Parser?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -