Php xml Expat Parser

Source: Internet
Author: User
Tags object model xml parser

Php xml Expat Parser

The built-in foreign analyzer can process XML documents in PHP.
 

What is XML?
XML is used to describe data and focus on what data is. Structure data described in an XML file.

There are no pre-determined tags in XML. You must define your own tag.

For more information about XML, see our XML Guide.
 

What is a foreigner?
To read and update-create and manipulate-an xm l file, you need an xm l parser.

There are two basic types of XML parser:

Tree-based profiling: This analyzer converts an XML file into a tree structure. Analyzes the entire file and provides the content for entering the tree. Example: Document Object Model (DOM)
Event-based analysis: a series of activities for Viewing XML files. When a specific event occurs, it requires a function to process it.
This foreign analyzer is an event-based profiling.

The XML file of the event-based parser's key content, rather than its structure. Because of this, the event-based parser can access data faster than the tree-based parser.

Take a look at the XML section below:

<From> Jani </from>
  

An event-based profiling report XML contains the preceding three activities:

Start element: from
Document, the beginning of this section, value: Jani
Close content: from
The XML in the preceding example contains well-formatted XML. However, for example, XML cannot be valid because there is no Document Type Definition (DTD) associated with this.

However, there is no difference when using a foreign analyzer. A foreigner is a non-authenticated profiling and ignores any DTDs.

As an event-based, non-validated XML analyzer, it is fast and small for foreign users and is suitable for PHP Web applications.

Note: XML files must be fully formed or foreign users will generate an error.




Install
The XML parser function of foreigners is a core part of PHP. These functions can be used without installation.




An XML file
The XML file below will be used as our example

<? Xml version = "1.0" encoding = "ISO-8859-1"?> <Note> <to> Tove </to> <from> Jani </from> 
 
     

Initialize the XML parser
We need to initialize the XML parser in PHP, identify different XML processing events, and then parse the XML file.

For example

<? Php
// Initialize the XML parser $ parser = xml_parser_create ();
// Function to use at the start of an elementfunction start ($ parser, $ element_name, $ element_attrs) {switch ($ element_name) {case "NOTE ": echo "Note <br/>"; break; case "TO": echo "To:"; break; case "FROM": echo "From:"; break; case "HEADING": echo "Heading:"; break; case "BODY": echo "Message :";}}
// Function to use at the end of an elementfunction stop ($ parser, $ element_name) {echo "<br/> ";}
// Function to use when finding character datafunction char ($ parser, $ data) {echo $ data ;}
// Specify element handlerxml_set_element_handler ($ parser, "start", "stop ");
// Specify data handlerxml_set_character_data_handler ($ parser, "char ");
// Open XML file $ fp = fopen ("test. xml", "r ");
// Read datawhile ($ data = fread ($ fp, 4096) {xml_parse ($ parser, $ data, feof ($ fp) or die (sprintf ("XML Error: % s at line % d ", xml_error_string (xml_get_error_code ($ parser), xml_get_current_line_number ($ parser )));}
// Free the XML parserxml_parser_free ($ parser );
?>
 
The output result is as follows.
 
Note To: ToveFrom: JaniHeading: ReminderMessage: Don't forget me this weekend!
 
        

The principle is:

Initialize the XML analyzer and xml_parser_create () functions
Create functions and use different event handlers
Added the xml_set_element_handler () function to specify which functions will be executed, and the parser will encounter opening and closing labels
The xml_set_character_data_handler () function is added to specify which features will be used to analyze character data during execution.
Parse the xml_parse () function of the file "test. xml"
If an error occurs, add the xml_error_string () function to convert the text description of the XML error.
Call the xml_parser_free () function to release the allocated memory and the xml_parser_create () function.

For more information, see www.111cn.net/phper/php.html.

 

Related Article

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.