PHP XML Expat Parser

Source: Internet
Author: User
Tags xml example xml parser

What is XML?

XML is used to describe data, with the focus on what the data is. The XML file describes the structure of the data.

In XML, there are no predefined tags. You must define your own labels.

What is Expat?

If you want to read and update-Create and process an XML document, you need an XML parser.

There are two basic types of XML parsers:

Tree-based parser: This parser transforms an XML document into a tree structure. It parses the entire document and provides an API to access the elements of the tree, such as the Document Object Model (DOM).

Event-based parser: treats an XML document as a series of events. When a specific event occurs, the parser invokes the function to process it.

The Expat parser is an event-based parser.

Event-based gold sinks the contents of the XML document, not their results. Because of this, event-based parsers are able to access data faster than tree-based parsers.

Take a look at the following XML fragment:

<from>John</from>

The event-based parser reports the above XML as a series of three events:

Start element: From

Start CDATA section, Value: John

Close element: From

The XML example above contains well-formed XML. However, this example is invalid XML because there is no document type declaration (DTD) associated with it, and there is no inline DTD.

However, this is no different when using the Expat parser. Expat is a parser that does not check for validity, ignoring any DTD.

As an event-based, non-validating XML parser, the Expat is fast and lightweight and works well for PHP Web applications.

Note: The XML document must be in good form or Expat will generate an error.

Installation

The XML Expat parser is part of the PHP core. These functions can be used without installation.

XML file

The following XML file will be used in our example:

<?xml version= "1.0" encoding= "iso-8859-1"? ><note><to>george</to><from>john</ From>
Initializing the XML parser

We will initialize the XML parser in PHP, define the processor for different XML events, and parse the XML file.

Example
<?php//initialize the XML parser$parser=xml_parser_create ();//function to use at the start of a 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.bjrongjinhuiyin.com ($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 @ line%d", Xml_error_str ING (Xml_get_error_code ($pArser)), Xml_get_current_line_number ($parser))); }//free the XML parserxml_parser_free ($parser);? >

The output of the above code:

--Note--to:georgefrom:johnheading:remindermessage:don ' t forget the meeting!
Working principle Explanation:
    • Initializing the XML parser with the Xml_parser_create () function
    • Create a function that mates with different event handlers
    • Add the Xml_set_element_handler () function to define which function to execute when the parser encounters the start and end tags
    • Add the Xml_set_character_data_handler () function to define which function to execute when the parser encounters character data
    • Parse the file "Test.xml" with the Xml_parse () function
    • In the event of an error, add the xml_error_string () function to convert the XML error to a textual description
    • Call the Xml_parser_free () function to release the memory allocated to the Xml_parser_create () function

PHP XML Expat Parser

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.