PHP XML Expat Parser
The built-in Expat parser makes it possible to process XML documents in PHP.
What is XML?
XML is used to describe data, with a focus on what the data is. An XML file describes the structure of the data.
In XML, there are no predefined tags. You must define your own tags.
To learn more about XML, visit our XML Tutorial.
What is Expat?
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 parsers: These parsers transform an XML document into a tree structure. They analyze the entire document and provide access to elements in the tree, such as the Document Object Model (DOM).
Event-based parsers: These parsers view an XML document as a series of events. When a specific event occurs, the parser calls a function to handle it.
The Expat parser is an event-based parser.
Event-based parsers focus on the content of XML documents rather than their structure. Because of this, event-based parsers can access data faster than tree-based parsers.
Consider the following XML snippet:
An event-based parser reports the above XML as a series of three events:
- Start element: from
- Start CDATA section, value: Jani
- Close element: from
The above XML example is well-formed. However, it is invalid XML because it does not have a Document Type Definition (DTD) associated with it.
However, this makes no difference when using the Expat parser. Expat is a non-validating parser and ignores any DTD.
As an event-based, non-validating XML parser, Expat is fast and lightweight, making it well-suited for PHP web applications.
Note: The XML document must be well-formed, otherwise Expat will generate errors.
Installation
The XML Expat parser functions are part of the PHP core. There is no need to install them to use these functions.
XML File
The following XML file will be used in our examples:
Initializing the XML Parser
We need to initialize the XML parser in PHP, define handlers for different XML events, and then parse the XML file.
Example
The above code will output:
How it works:
- Initialize the XML parser with the xml_parser_create() function
- Create functions to handle different events
- Add the xml_set_element_handler() function to define which functions to execute when the parser encounters 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
- If there are any errors, add the xml_error_string() function to convert XML errors to text descriptions
- Call the xml_parser_free() function to free the memory allocated to the xml_parser_create() function
More Information on PHP Expat Parser
For more information about PHP Expat functions, visit our PHP XML Parser Reference Manual.