Php xml Parser function application example

Source: Internet
Author: User
Tags constant error code fread processing instruction xml parser

Xml functions are the core component of php. These functions can be used without installation.
Php xml parser function
Php: indicates the earliest php version that supports this function.

Function description php
Utf8_decode () decodes UTF-8 strings into ISO-8859-1. 3
Utf8_encode () encodes the ISO-8859-1 string into UTF-8. 3
Xml_error_string () gets the error description of the xml parser. 3
Xml_get_current_byte_index () gets the current byte index of the xml parser. 3
Xml_get_current_column_number () gets the current column number of the xml parser. 3
Xml_get_current_line_number () gets the current row number of the xml parser. 3
Xml_get_error_code () gets the error code of the xml parser. 3
Xml_parse () parses xml documents. 3
Xml_parse_into_struct () parses xml data into an array. 3
Xml_parser_create_ns () creates an xml parser with namespace support. 4
Xml_parser_create () creates an xml parser. 3
Xml_parser_free () releases the xml parser. 3
Xml_parser_get_option () gets the option setting information from the xml parser. 3
Xml_parser_set_option () is used to set options for xml parsing. 3
Xml_set_character_data_handler () creates a character data processor. 3
Xml_set_default_handler () creates the default data processor. 3
Xml_set_element_handler () creates the start and end element processor. 3
Xml_set_end_namespace_decl_handler () is created to terminate the namespace claim processor. 4
Xml_set_external_entity_ref_handler () creates an external entity processor. 3
Xml_set_notation_decl_handler () creates a Annotation declaration processor. 3
Xml_set_object () uses the xml parser in the object. 4
Xml_set_processing_instruction_handler () creates a processing instruction (pi) processor. 3
Xml_set_start_namespace_decl_handler () creates the starting namespace declaration processor. 4
Xml_set_unparsed_entity_decl_handler () creates an unresolved entity definition declarative processor. 3

*/
$ File = "test. xml"; // defines the xml file
$ Character_data_on = false; // defines a Boolean variable.
$ Tag_complete = true; // defines a Boolean variable.
Function startelement ($ parser, $ name, $ attrs) // defines the start callback function.
{
Global $ character_data_on;
Global $ tag_complete;
Echo "& lt; <font color =" # 0000cc "> $ name </font> ";
If (sizeof ($ attrs ))
  {
While (list ($ k, $ v) = each ($ attrs ))
    {
Echo "<font color =" #009900 "> $ k </font> =" <fontcolor = "#990000"> $ v </font> "";
    }
  }
$ Tag_complete = false; // defines a Boolean variable.
$ Character_data_on = false; // defines a Boolean variable.
}
Function endelement ($ parser, $ name) // defines the termination callback function
{
Global $ fp;
Global $ character_data_on;
Global $ tag_complete;
If (! $ Character_data_on)
  {
$ Temp_fp = ftell ($ fp );
$ End_element_byte_index = xml_get_current_byte_index ($ parser );
Fseek ($ fp, $ end_element_byte_index-2 );
$ Validator = fgets ($ fp, 3 );
Fseek ($ fp, $ temp_fp );
If ($ validator = "/> ")
    {
Echo "/& gt ";
    }
Else echo "& gt & lt/<font color =" # 0000cc "> $ name </font> & gt ";
$ Tag_complete = true;
  }
Else echo "& lt/<font color =" # 0000cc "> $ name </font> & gt ";
$ Character_data_on = false;
}
Function characterdata ($ parser, $ data) // defines the callback function
{
Global $ character_data_on;
Global $ tag_complete;
If ((! $ Character_data_on )&&(! $ Tag_complete ))
  {
Echo "& gt ";
$ Tag_complete = true;
  }
Echo "<B> $ data </B> ";
$ Character_data_on = true;
}
$ Xml_parser = xml_parser_create (); // Create an xml parser
Xml_parser_set_option ($ xml_parser, xml_option_case_folding, false); // Set the parser
Xml_set_element_handler ($ xml_parser, "startelement", "endelement"); // creates an element processor.
Xml_set_character_data_handler ($ xml_parser, "characterdata"); // create a character data processor
If (! ($ Fp = fopen ($ file, "r") // if the file fails to be opened
{
Die ("cocould not open xml input"); // output error message
}
Echo "<pre> ";
While ($ file_content = fread ($ fp, 4096) // read the content cyclically
{
If (! Xml_parse ($ xml_parser, $ file_content, 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 )));
  }
}
Echo "</pre> ";
Xml_parser_free ($ xml_parser); // release the xml parser
?>

Instance 2

<? Php
Class xmlparser // define a class
{
Var $ xmlparser; // class attributes
Function xmlparser () // class method
  {
$ This-> xmlparser = xml_parser_create ();
Xml_set_object ($ this-> xmlparser, $ this );
Xml_set_character_data_handler ($ this-> xmlparser, "char ");
Xml_set_element_handler ($ this-> xmlparser, "start_tag", "end_tag ");
  }
Function parse ($ data) // class method
  {
Xml_parse ($ this-> xmlparser, $ data );
  }
Function parse_file ($ xmlfile) // class method
  {
$ Fp = fopen ($ xmlfile, 'r ');
While ($ xmldata = fread ($ fp, 4096 ))
    {
If (! Xml_parse ($ this-> xmlparser, $ xmldata ))
      {
Die (print "error :".
Xml_error_string (xml_get_error_code ($ this-> xmlparser). "<br/> line :".
Xml_get_current_line_number ($ this-> xmlparser). "<br/> column :".
Xml_get_current_column_number ($ this-> xmlparser). "<br/> ");
      }
    }
  }
Function start_tag ($ xmlparser, $ tag, $ attributes) // class method
  {
Print $ tag. "<br/> ";
  }
Function end_tag () // method of the class
  {
  }
Function char ($ xmlparser, $ data) // class method
  {
Echo $ data. "<br/> ";
  }
Function close_parser () // method of the class
  {
Xml_parser_free ($ this-> xmlparser );
  }
}
$ Myxmlparser = new xmlparser (); // Initialize an object for the class
$ Myxmlparser-> parse_file ("test. xml"); // call the file reading method of the class.
$ Myxmlparser-> close_parser (); // call the close method of the class
/*
Php xml parser constant
Constant
Xml_error_none (integer)
Xml_error_no_memory (integer)
Xml_error_syntax (integer)
Xml_error_no_elements (integer)
Xml_error_invalid_token (integer)
Xml_error_unclosed_token (integer)
Xml_error_partial_char (integer)
Xml_error_tag_mismatch (integer)
Xml_error_duplicate_attribute (integer)
Xml_error_junk_after_doc_element (integer)
Xml_error_param_entity_ref (integer)
Xml_error_undefined_entity (integer)
Xml_error_recursive_entity_ref (integer)
Xml_error_async_entity (integer)
Xml_error_bad_char_ref (integer)
Xml_error_binary_entity_ref (integer)
Xml_error_attribute_external_entity_ref (integer)
Xml_error_misplaced_xml_pi (integer)
Xml_error_unknown_encoding (integer)
Xml_error_incorrect_encoding (integer)
Xml_error_unclosed_cdata_section (integer)
Xml_error_external_entity_handling (integer)
Xml_option_case_folding (integer)
Xml_option_target_encoding (integer)
Xml_option_skip_tagstart (integer)
Xml_option_skip_white (integer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.