铁血霸主 2007-8-2 13:29
PHP高亮显示 XML 源代码
<p>它将说明如何外部实体指向处理器来包含和解析其它文档,如何处理 PIs,以及一种确定包含有 PIs 的<a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, '%B4%FA%C2%EB');" target="_self"><u><strong>代码</strong></u></a>的可信度。 <P> 能被该范例使用的的 <a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, 'XML');" target="_self"><u><strong>XML</strong></u></a> 文档(xmltest.xml 和 xmltest2.xml)被列在该范例之后。 </P><P> 外部实体范例</P><DIV class=code><P><?php<BR>$file = "xmltest.xml";</P><P>function trustedFile($file) {<BR> // only trust local files owned by ourselves<BR> if (!eregi("^([a-z]+)://", $file) <BR> && fileowner($file) == getmyuid()) {<BR> return true;<BR> }<BR> return false;<BR>}</P><P>function startElement($parser, $name, $attribs) {<BR> print "&lt;<font color=\"#0000cc\">$name</font>";<BR> if (sizeof($attribs)) {<BR> while (list($k, $v) = each($attribs)) {<BR> print " <font color=\"#009900\">$k</font>=\"<font <BR> color=\"#990000\">$v</font>\"";<BR> }<BR> }<BR> print "&gt;";<BR>}</P><P>function endElement($parser, $name) {<BR> print "&lt;/<font color=\"#0000cc\">$name</font>&gt;";<BR>}</P><P>function characterData($parser, $data) {<BR> print "<b>$data</b>";<BR>}</P><P>function PIHandler($parser, $target, $data) {<BR> switch (strtolower($target)) {<BR> case "<a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, 'php');" target="_self"><u><strong>php</strong></u></a>\":<BR> global $parser_file;<BR> // If the parsed document is "trusted", we say it is safe<BR> // to execute PHP code inside it. If not, display the code<BR> // instead.<BR> if (trustedFile($parser_file[$parser])) {<BR> eval($data);<BR> } else {<BR> printf("Untrusted PHP code: <i>%s</i>", <BR> htmlspecialchars($data));<BR> }<BR> break;<BR> }<BR>}</P><P>function defaultHandler($parser, $data) {<BR> if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {<BR> printf('<font color="#aa00aa">%s</font>', <BR> htmlspecialchars($data));<BR> } else {<BR> printf('<font size="-1">%s</font>', <BR> htmlspecialchars($data));<BR> }<BR>}</P><P>function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId,<BR> $publicId) {<BR> if ($systemId) {<BR> if (!list($parser, $fp) = new_xml_parser($systemId)) {<BR> printf("Could not open entity %s at %s\n", $openEntityNames,<BR> $systemId);<BR> return false;<BR> }<BR> while ($data = fread($fp, 4096)) {<BR> if (!xml_parse($parser, $data, feof($fp))) {<BR> printf("XML error: %s at line %d while parsing entity %s\n",<BR> xml_error_string(xml_get_error_code($parser)),<BR> xml_get_current_line_number($parser), $openEntityNames);<BR> xml_parser_free($parser);<BR> return false;<BR> }<BR> }<BR> xml_parser_free($parser);<BR> return true;<BR> }<BR> return false;<BR>}</P><P>function new_xml_parser($file) {<BR> global $parser_file;</P><P> $xml_parser = xml_parser_create();<BR> xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);<BR> xml_set_element_handler($xml_parser, "startElement", "endElement");<BR> xml_set_character_data_handler($xml_parser, "characterData");<BR> xml_set_processing_instruction_handler($xml_parser, "PIHandler");<BR> xml_set_default_handler($xml_parser, "defaultHandler");<BR> xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler");<BR> <BR> if (!($fp = @fopen($file, "r"))) {<BR> return false;<BR> }<BR> if (!is_array($parser_file)) {<BR> settype($parser_file, "array");<BR> }<BR> $parser_file[$xml_parser] = $file;<BR> return array($xml_parser, $fp);<BR>}</P><P>if (!(list($xml_parser, $fp) = new_xml_parser($file))) {<BR> die("could not open XML input");<BR>}</P><P>print "<pre>";<BR>while ($data = fread($fp, 4096)) {<BR> if (!xml_parse($xml_parser, $data, feof($fp))) {<BR> die(sprintf("XML error: %s at line %d\n",<BR> xml_error_string(xml_get_error_code($xml_parser)),<BR> xml_get_current_line_number($xml_parser)));<BR> }<BR>}<BR>print "</pre>";<BR>print "parse complete\n";<BR>xml_parser_free($xml_parser);<BR>?> <BR> <BR></P>