<HTML ><HEAD ><TITLE >XML parser functions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Function Reference" HREF="funcref.html"><LINK REL="PREVIOUS" TITLE="wddx_serialize_vars" HREF="function.wddx-serialize-vars.html"><LINK REL="NEXT" TITLE="utf8_decode" HREF="function.utf8-decode.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="reference" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.wddx-serialize-vars.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.utf8-decode.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="reference" ><A NAME="ref.xml" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >CVII. XML parser functions</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN95405" ></A ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="xml.intro" ></A >Introduction</H1 ><P > XML (eXtensible Markup Language) is a data format for structured document interchange on the Web. It is a standard defined by The World Wide Web consortium (W3C). Information about XML and related technologies can be found at <A HREF="http://www.w3.org/XML/" TARGET="_top" >http://www.w3.org/XML/</A >. </P ><P > This PHP extension implements support for James Clark's <SPAN CLASS="productname" >expat</SPAN > in PHP. This toolkit lets you parse, but not validate, XML documents. It supports three source <A HREF="ref.xml.html#xml.encoding" >character encodings</A > also provided by PHP: <TT CLASS="literal" >US-ASCII</TT >, <TT CLASS="literal" >ISO-8859-1</TT > and <TT CLASS="literal" >UTF-8</TT >. <TT CLASS="literal" >UTF-16</TT > is not supported. </P ><P > This extension lets you <A HREF="function.xml-parser-create.html" >create XML parsers</A > and then define <SPAN CLASS="emphasis" ><I CLASS="emphasis" >handlers</I ></SPAN > for different XML events. Each XML parser also has a few <A HREF="function.xml-parser-set-option.html" >parameters</A > you can adjust. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="xml.requirements" ></A >Requirements</H1 ><P > This extension uses <SPAN CLASS="productname" >expat</SPAN >, which can be found at <A HREF="http://www.jclark.com/xml/" TARGET="_top" >http://www.jclark.com/xml/</A >. The Makefile that comes with expat does not build a library by default, you can use this make rule for that: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="makefile" >libexpat.a: $(OBJS) ar -rc $@ $(OBJS) ranlib $@</PRE ></TD ></TR ></TABLE > A source RPM package of expat can be found at <A HREF="http://sourceforge.net/projects/expat/" TARGET="_top" >http://sourceforge.net/projects/expat/</A >. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="xml.installation" ></A >Installation</H1 ><P > These functions are enabled by default, using the bundled expat library. You can disable XML support with <TT CLASS="option" >--disable-xml</TT >. If you compile PHP as a module for Apache 1.3.9 or later, PHP will automatically use the bundled <SPAN CLASS="productname" >expat</SPAN > library from Apache. In order you dont't want to use the bundled expat library configure PHP <TT CLASS="option" >--with-expat-dir=DIR</TT >, where DIR should point to the base installation directory of expat. </P ><P > The windows version of <TT CLASS="literal" >PHP</TT > has built in support for this extension. You do not need to load any additional extension in order to use these functions.</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="xml.configuration" ></A >Runtime Configuration</H1 ><P >This extension has no configuration directives defined in <TT CLASS="filename" >php.ini</TT >.</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="xml.resources" ></A >Resource Types</H1 ><P >This extension has no resource types defined.</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="xml.constants" ></A >Predefined Constants</H1 ><P > The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime. </P ><P ></P ><DIV CLASS="variablelist" ><DL ><DT ><TT CLASS="constant" ><B >XML_ERROR_NONE</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_NO_MEMORY</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_SYNTAX</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_NO_ELEMENTS</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_INVALID_TOKEN</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_UNCLOSED_TOKEN</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_PARTIAL_CHAR</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_TAG_MISMATCH</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_DUPLICATE_ATTRIBUTE</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_JUNK_AFTER_DOC_ELEMENT</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_PARAM_ENTITY_REF</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_UNDEFINED_ENTITY</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_RECURSIVE_ENTITY_REF</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_ASYNC_ENTITY</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_BAD_CHAR_REF</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_BINARY_ENTITY_REF</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_MISPLACED_XML_PI</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_UNKNOWN_ENCODING</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_INCORRECT_ENCODING</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_UNCLOSED_CDATA_SECTION</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_ERROR_EXTERNAL_ENTITY_HANDLING</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_OPTION_CASE_FOLDING</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_OPTION_TARGET_ENCODING</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_OPTION_SKIP_TAGSTART</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >XML_OPTION_SKIP_WHITE</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="xml.eventhandlers" ></A >Event Handlers</H1 ><P > The XML event handlers defined are: <DIV CLASS="table" ><A NAME="AEN95606" ></A ><P ><B >Table 1. Supported XML handlers</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><THEAD ><TR ><TH ALIGN="LEFT" VALIGN="MIDDLE" >PHP function to set handler</TH ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Event description</TH ></TR ></THEAD ><TBODY ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" ><A HREF="function.xml-set-element-handler.html" ><B CLASS="function" >xml_set_element_handler()</B ></A ></TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" > Element events are issued whenever the XML parser encounters start or end tags. There are separate handlers for start tags and end tags. </TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" > <A HREF="function.xml-set-character-data-handler.html" ><B CLASS="function" >xml_set_character_data_handler()</B ></A > </TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" > Character data is roughly all the non-markup contents of XML documents, including whitespace between tags. Note that the XML parser does not add or remove any whitespace, it is up to the application (you) to decide whether whitespace is significant. </TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" > <A HREF="function.xml-set-processing-instruction-handler.html" ><B CLASS="function" >xml_set_processing_instruction_handler()</B ></A > </TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" > PHP programmers should be familiar with processing instructions (PIs) already. <?php ?> is a processing instruction, where <TT CLASS="replaceable" ><I >php</I ></TT > is called the "PI target". The handling of these are application-specific, except that all PI targets starting with "XML" are reserved. </TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" ><A HREF="function.xml-set-default-handler.html" ><B CLASS="function" >xml_set_default_handler()</B ></A ></TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" > What goes not to another handler goes to the default handler. You will get things like the XML and document type declarations in the default handler. </TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" > <A HREF="function.xml-set-unparsed-entity-decl-handler.html" ><B CLASS="function" >xml_set_unparsed_entity_decl_handler()</B ></A > </TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" > This handler will be called for declaration of an unparsed (NDATA) entity. </TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" > <A HREF="function.xml-set-notation-decl-handler.html" ><B CLASS="function" >xml_set_notation_decl_handler()</B ></A > </TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" > This handler is called for declaration of a notation. </TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" > <A HREF="function.xml-set-external-entity-ref-handler.html" ><B CLASS="function" >xml_set_external_entity_ref_handler()</B ></A > </TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" > This handler is called when the XML parser finds a reference to an external parsed general entity. This can be a reference to a file or URL, for example. See <A HREF="ref.xml.html#example.xml-external-entity" >the external entity example</A > for a demonstration. </TD ></TR ></TBODY ></TABLE ></DIV > </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="xml.case-folding" ></A >Case Folding</H1 ><P > The element handler functions may get their element names <I CLASS="glossterm" >case-folded</I >. Case-folding is defined by the XML standard as "a process applied to a sequence of characters, in which those identified as non-uppercase are replaced by their uppercase equivalents". In other words, when it comes to XML, case-folding simply means uppercasing. </P ><P > By default, all the element names that are passed to the handler functions are case-folded. This behaviour can be queried and controlled per XML parser with the <A HREF="function.xml-parser-get-option.html" ><B CLASS="function" >xml_parser_get_option()</B ></A > and <A HREF="function.xml-parser-set-option.html" ><B CLASS="function" >xml_parser_set_option()</B ></A > functions, respectively. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="xml.error-codes" ></A >Error Codes</H1 ><P > The following constants are defined for XML error codes (as returned by <A HREF="function.xml-parse.html" ><B CLASS="function" >xml_parse()</B ></A >): <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD >XML_ERROR_NONE</TD ></TR ><TR ><TD >XML_ERROR_NO_MEMORY</TD ></TR ><TR ><TD >XML_ERROR_SYNTAX</TD ></TR ><TR ><TD >XML_ERROR_NO_ELEMENTS</TD ></TR ><TR ><TD >XML_ERROR_INVALID_TOKEN</TD ></TR ><TR ><TD >XML_ERROR_UNCLOSED_TOKEN</TD ></TR ><TR ><TD >XML_ERROR_PARTIAL_CHAR</TD ></TR ><TR ><TD >XML_ERROR_TAG_MISMATCH</TD ></TR ><TR ><TD >XML_ERROR_DUPLICATE_ATTRIBUTE</TD ></TR ><TR ><TD >XML_ERROR_JUNK_AFTER_DOC_ELEMENT</TD ></TR ><TR ><TD >XML_ERROR_PARAM_ENTITY_REF</TD ></TR ><TR ><TD >XML_ERROR_UNDEFINED_ENTITY</TD ></TR ><TR ><TD >XML_ERROR_RECURSIVE_ENTITY_REF</TD ></TR ><TR ><TD >XML_ERROR_ASYNC_ENTITY</TD ></TR ><TR ><TD >XML_ERROR_BAD_CHAR_REF</TD ></TR ><TR ><TD >XML_ERROR_BINARY_ENTITY_REF</TD ></TR ><TR ><TD >XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF</TD ></TR ><TR ><TD >XML_ERROR_MISPLACED_XML_PI</TD ></TR ><TR ><TD >XML_ERROR_UNKNOWN_ENCODING</TD ></TR ><TR ><TD >XML_ERROR_INCORRECT_ENCODING</TD ></TR ><TR ><TD >XML_ERROR_UNCLOSED_CDATA_SECTION</TD ></TR ><TR ><TD >XML_ERROR_EXTERNAL_ENTITY_HANDLING</TD ></TR ></TBODY ></TABLE ><P ></P > </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="xml.encoding" ></A >Character Encoding</H1 ><P > PHP's XML extension supports the <A HREF="http://www.unicode.org/" TARGET="_top" >Unicode</A > character set through different <I CLASS="glossterm" >character encoding</I >s. There are two types of character encodings, <I CLASS="glossterm" >source encoding</I > and <I CLASS="glossterm" >target encoding</I >. PHP's internal representation of the document is always encoded with <TT CLASS="literal" >UTF-8</TT >. </P ><P > Source encoding is done when an XML document is <A HREF="function.xml-parse.html" >parsed</A >. Upon <A HREF="function.xml-parser-create.html" >creating an XML parser</A >, a source encoding can be specified (this encoding can not be changed later in the XML parser's lifetime). The supported source encodings are <TT CLASS="literal" >ISO-8859-1</TT >, <TT CLASS="literal" >US-ASCII</TT > and <TT CLASS="literal" >UTF-8</TT >. The former two are single-byte encodings, which means that each character is represented by a single byte. <TT CLASS="literal" >UTF-8</TT > can encode characters composed by a variable number of bits (up to 21) in one to four bytes. The default source encoding used by PHP is <TT CLASS="literal" >ISO-8859-1</TT >. </P ><P > Target encoding is done when PHP passes data to XML handler functions. When an XML parser is created, the target encoding is set to the same as the source encoding, but this may be changed at any point. The target encoding will affect character data as well as tag names and processing instruction targets. </P ><P > If the XML parser encounters characters outside the range that its source encoding is capable of representing, it will return an error. </P ><P > If PHP encounters characters in the parsed XML document that can not be represented in the chosen target encoding, the problem characters will be "demoted". Currently, this means that such characters are replaced by a question mark. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="xml.examples" ></A >Examples</H1 ><P > Here are some example PHP scripts parsing XML documents. </P ><DIV CLASS="section" ><H2 CLASS="section" ><A NAME="example.xml-structure" ></A >XML Element Structure Example</H2 ><P > This first example displays the stucture of the start elements in a document with indentation. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN95703" ></A ><P ><B >Example 1. Show XML Element Structure</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$file = "data.xml"; $depth = array(); function startElement($parser, $name, $attrs) { global $depth; for ($i = 0; $i < $depth[$parser]; $i++) { print " "; } print "$name\n"; $depth[$parser]++; } function endElement($parser, $name) { global $depth; $depth[$parser]--; } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); if (!($fp = fopen($file, "r"))) { die("could not open XML input"); } while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } xml_parser_free($xml_parser);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="section" ><H2 CLASS="section" ><A NAME="example.xml-map-tags" ></A >XML Tag Mapping Example</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN95709" ></A ><P ><B >Example 2. Map XML to HTML</B ></P ><P > This example maps tags in an XML document directly to HTML tags. Elements not found in the "map array" are ignored. Of course, this example will only work with a specific XML document type. <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$file = "data.xml"; $map_array = array( "BOLD" => "B", "EMPHASIS" => "I", "LITERAL" => "TT" ); function startElement($parser, $name, $attrs) { global $map_array; if ($htmltag = $map_array[$name]) { print "<$htmltag>"; } } function endElement($parser, $name) { global $map_array; if ($htmltag = $map_array[$name]) { print "</$htmltag>"; } } function characterData($parser, $data) { print $data; } $xml_parser = xml_parser_create(); // use case-folding so we are sure to find the tag in $map_array xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); if (!($fp = fopen($file, "r"))) { die("could not open XML input"); } while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } xml_parser_free($xml_parser);</PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="section" ><H2 CLASS="section" ><A NAME="example.xml-external-entity" ></A >XML External Entity Example</H2 ><P > This example highlights XML code. It illustrates how to use an external entity reference handler to include and parse other documents, as well as how PIs can be processed, and a way of determining "trust" for PIs containing code. </P ><P > XML documents that can be used for this example are found below the example (<TT CLASS="filename" >xmltest.xml</TT > and <TT CLASS="filename" >xmltest2.xml</TT >.) </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN95720" ></A ><P ><B >Example 3. External Entity Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $file = "xmltest.xml"; function trustedFile($file) { // only trust local files owned by ourselves if (!eregi("^([a-z]+)://", $file) && fileowner($file) == getmyuid()) { return true; } return false; } function startElement($parser, $name, $attribs) { print "&lt;<font color=\"#0000cc\">$name</font>"; if (sizeof($attribs)) { while (list($k, $v) = each($attribs)) { print " <font color=\"#009900\">$k</font>=\"<font color=\"#990000\">$v</font>\""; } } print "&gt;"; } function endElement($parser, $name) { print "&lt;/<font color=\"#0000cc\">$name</font>&gt;"; } function characterData($parser, $data) { print "<b>$data</b>"; } function PIHandler($parser, $target, $data) { switch (strtolower($target)) { case "php": global $parser_file; // If the parsed document is "trusted", we say it is safe // to execute PHP code inside it. If not, display the code // instead. if (trustedFile($parser_file[$parser])) { eval($data); } else { printf("Untrusted PHP code: <i>%s</i>", htmlspecialchars($data)); } break; } } function defaultHandler($parser, $data) { if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") { printf('<font color="#aa00aa">%s</font>', htmlspecialchars($data)); } else { printf('<font size="-1">%s</font>', htmlspecialchars($data)); } } function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId, $publicId) { if ($systemId) { if (!list($parser, $fp) = new_xml_parser($systemId)) { printf("Could not open entity %s at %s\n", $openEntityNames, $systemId); return false; } while ($data = fread($fp, 4096)) { if (!xml_parse($parser, $data, feof($fp))) { printf("XML error: %s at line %d while parsing entity %s\n", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser), $openEntityNames); xml_parser_free($parser); return false; } } xml_parser_free($parser); return true; } return false; } function new_xml_parser($file) { global $parser_file; $xml_parser = xml_parser_create(); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); xml_set_processing_instruction_handler($xml_parser, "PIHandler"); xml_set_default_handler($xml_parser, "defaultHandler"); xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler"); if (!($fp = @fopen($file, "r"))) { return false; } if (!is_array($parser_file)) { settype($parser_file, "array"); } $parser_file[$xml_parser] = $file; return array($xml_parser, $fp); } if (!(list($xml_parser, $fp) = new_xml_parser($file))) { die("could not open XML input"); } print "<pre>"; while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d\n", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } print "</pre>"; print "parse complete\n"; xml_parser_free($xml_parser); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN95724" ></A ><P ><B >Example 4. xmltest.xml</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="xml" ><?xml version='1.0'?> <!DOCTYPE chapter SYSTEM "/just/a/test.dtd" [ <!ENTITY plainEntity "FOO entity"> <!ENTITY systemEntity SYSTEM "xmltest2.xml"> ]> <chapter> <TITLE>Title &plainEntity;</TITLE> <para> <informaltable> <tgroup cols="3"> <tbody> <row><entry>a1</entry><entry morerows="1">b1</entry><entry>c1</entry></row> <row><entry>a2</entry><entry>c2</entry></row> <row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row> </tbody> </tgroup> </informaltable> </para> &systemEntity; <section id="about"> <title>About this Document</title> <para> <!-- this is a comment --> <?php print 'Hi! This is PHP version '.phpversion(); ?> </para> </section> </chapter></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > This file is included from <TT CLASS="filename" >xmltest.xml</TT >: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN95729" ></A ><P ><B >Example 5. xmltest2.xml</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="xml" ><?xml version="1.0"?> <!DOCTYPE foo [ <!ENTITY testEnt "test entity"> ]> <foo> <element attrib="value"/> &testEnt; <?php print "This is some more PHP code being executed."; ?> </foo></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="function.utf8-decode.html" >utf8_decode</A > -- Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1. </DT ><DT ><A HREF="function.utf8-encode.html" >utf8_encode</A > -- encodes an ISO-8859-1 string to UTF-8</DT ><DT ><A HREF="function.xml-error-string.html" >xml_error_string</A > -- get XML parser error string</DT ><DT ><A HREF="function.xml-get-current-byte-index.html" >xml_get_current_byte_index</A > -- get current byte index for an XML parser</DT ><DT ><A HREF="function.xml-get-current-column-number.html" >xml_get_current_column_number</A > -- Get current column number for an XML parser </DT ><DT ><A HREF="function.xml-get-current-line-number.html" >xml_get_current_line_number</A > -- get current line number for an XML parser</DT ><DT ><A HREF="function.xml-get-error-code.html" >xml_get_error_code</A > -- get XML parser error code</DT ><DT ><A HREF="function.xml-parse-into-struct.html" >xml_parse_into_struct</A > -- Parse XML data into an array structure</DT ><DT ><A HREF="function.xml-parse.html" >xml_parse</A > -- start parsing an XML document</DT ><DT ><A HREF="function.xml-parser-create-ns.html" >xml_parser_create_ns</A > -- Create an XML parser </DT ><DT ><A HREF="function.xml-parser-create.html" >xml_parser_create</A > -- create an XML parser</DT ><DT ><A HREF="function.xml-parser-free.html" >xml_parser_free</A > -- Free an XML parser</DT ><DT ><A HREF="function.xml-parser-get-option.html" >xml_parser_get_option</A > -- get options from an XML parser</DT ><DT ><A HREF="function.xml-parser-set-option.html" >xml_parser_set_option</A > -- set options in an XML parser</DT ><DT ><A HREF="function.xml-set-character-data-handler.html" >xml_set_character_data_handler</A > -- set up character data handler</DT ><DT ><A HREF="function.xml-set-default-handler.html" >xml_set_default_handler</A > -- set up default handler</DT ><DT ><A HREF="function.xml-set-element-handler.html" >xml_set_element_handler</A > -- set up start and end element handlers</DT ><DT ><A HREF="function.xml-set-end-namespace-decl-handler.html" >xml_set_end_namespace_decl_handler</A > -- Set up character data handler </DT ><DT ><A HREF="function.xml-set-external-entity-ref-handler.html" >xml_set_external_entity_ref_handler</A > -- set up external entity reference handler</DT ><DT ><A HREF="function.xml-set-notation-decl-handler.html" >xml_set_notation_decl_handler</A > -- set up notation declaration handler</DT ><DT ><A HREF="function.xml-set-object.html" >xml_set_object</A > -- Use XML Parser within an object</DT ><DT ><A HREF="function.xml-set-processing-instruction-handler.html" >xml_set_processing_instruction_handler</A > -- Set up processing instruction (PI) handler </DT ><DT ><A HREF="function.xml-set-start-namespace-decl-handler.html" >xml_set_start_namespace_decl_handler</A > -- Set up character data handler </DT ><DT ><A HREF="function.xml-set-unparsed-entity-decl-handler.html" >xml_set_unparsed_entity_decl_handler</A > -- Set up unparsed entity declaration handler </DT ></DL ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.wddx-serialize-vars.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.utf8-decode.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >wddx_serialize_vars</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="funcref.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >utf8_decode</TD ></TR ></TABLE ></DIV ></BODY ></HTML >