Easy Tutorial
❮ Php Filter Func Filter List ❯

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:

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:

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:


More Information on PHP Expat Parser

For more information about PHP Expat functions, visit our PHP XML Parser Reference Manual.

❮ Php Filter Func Filter List ❯