I. XML element structure routine the first routine indent displays the starting element structure in the document. Example #1 show the XML element structure & lt ;? Php $ file
I. XML element structure routine
The first routine indent shows the start element structure in the document.
Example #1 display the XML element structure
II. XML tag ing routine
Example #1 map XML to HTML
This routine maps XML tags directly to HTML tags. Elements not found in map_array are ignored. Of course, this routine only applies to specific XML document types.
"B","EMPHASIS" => "I","LITERAL" => "TT");function startElement($parser, $name, $attrs){global $map_array;if (isset($map_array[$name])) {echo "<$map_array[$name]>";}}function endElement($parser, $name){global $map_array;if (isset($map_array[$name])) {echo "
";}}function characterData($parser, $data){echo $data;}$xml_parser = xml_parser_create();// use case-folding so we are sure to find the tag in $map_arrayxml_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);?>
III. XML external entity routine
This routine is used to highlight the XML code. An example shows how to use external entity references to include and parse other documents, how the processing commands are processed, and how to determine whether the code contained in the processing commands is "trustable ".
The XML document used for this routine is located at the bottom (and) of this routine ).
Example #1 external entity routine
$ V) {echo "$ k = \" $ v \ "" ;}} echo ">" ;}function endElement ($ parser, $ name) {echo "</$ name>";} function characterData ($ parser, $ data) {echo"$ Data";} Function PIHandler ($ parser, $ target, $ data) {switch (strtolower ($ target) {case" php ": global $ parser_file; // if the document to be parsed is "trustable", it indicates that the internal PHP code can be safely executed. Otherwise, the code content is displayed. If (trustedFile ($ parser_file [$ parser]) {eval ($ data);} else {printf ("Untrusted PHP code:% S", Htmlspecialchars ($ data);} break;} function defaultHandler ($ parser, $ data) {if (substr ($ data, 0, 1) = "&" & substr ($ data,-1, 1) = ";") {printf ('% s', htmlspecialchars ($ data ));} else {printf ('% s', htmlspecialchars ($ data) ;}} function externalEntityRefHandler ($ parser, $ openEntityNames, $ base, $ systemId, $ publicId) {if ($ systemId) {if (! List ($ parser, $ fp) = new_xml_parser ($ systemId) {printf ("cocould 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), evaluate ($ 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, parser, 1); xml_set_element_handler ($ xml_parser, "startElement", "endElement"); aggregate ($ xml_parser, "characterData"); aggregate ($ 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 ("cocould not open XML input");} echo"";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)));}}echo "
"; Echo" parse complete \ n "; xml_parser_free ($ xml_parser);?>
Example #2 xmltest. xml
]>
Title &plainEntity;
a1
b1
c1
a2
c2
a3
b3
c3
&systemEntity;
About this Document
This file is contained in:
Example #3 xmltest2.xml
]>
&testEnt;
IV. XML parser functions
Utf8_decode-converts a UTF-8 string encoded in ISO-8859-1 to a single-byte ISO-8859-1 string.
Utf8_encode-convert a ISO-8859-1-encoded string to a UTF-8-encoded string
Xml_error_string-get the error string of the XML parser
Xml_get_current_byte_index-get the current byte index of the XML parser
Xml_get_current_column_number-get the current column number of the XML parser
Xml_get_current_line_number-get the current row number of the XML parser
Xml_get_error_code-get XML parser error code
Xml_parse_pai_struct-parse XML data into an array
Xml_parse-start parsing an XML document
Xml_parser_create_ns-generate an XML parser that supports namespaces
Xml_parser_create-create an XML parser
Xml_parser_free-release the specified XML parser
Xml_parser_get_option-get the option setting information from the XML parser
Xml_parser_set_option-set options for specified XML parsing
Xml_set_character_data_handler-create a character data processor
Xml_set_default_handler-create a default processor
Xml_set_element_handler-creates the start and end element processor
Xml_set_end_namespace_decl_handler-create an end namespace claim processor
Xml_set_external_entity_ref_handler-create external entity pointing to processor
Xml_set_notation_decl_handler-creates a Annotation declaration processor
Xml_set_object-use the XML parser in the object
Xml_set_processing_instruction_handler-create a processing instruction (PI) processor
Xml_set_start_namespace_decl_handler-create the starting namespace declaration processor
Xml_set_unparsed_entity_decl_handler-creates an unresolved Entity Definition claim processor
The above is the content above. For more information, see The PHP Chinese website (www.php1.cn )! For more information, see The PHP Chinese website (www.php1.cn )!