PhpXML Expat Parser
The built-in EXPAT parser makes it possible to work with XML documents in PHP.
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.
To learn more about XML, please 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 parser: This parser transforms an XML document into a tree structure. It parses the entire document and provides access to the elements in the tree, such as the Document Object Model (DOM).
- Time-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 parsers focus on the content of XML documents, not their structure. 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>Jani</from>
The 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 XML instance above contains well-formed XML. However, this instance is invalid XML because there is no document type declaration (DTD) associated with it.
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.
notes: The XML document must be in good form or Expat will generate an error.
Installation
The XML Expat parser function is part of the PHP core. These functions can be used without installation.
XML file
The following XML file will be applied to our instance:
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<body>don ' t forget me this weekend!</body>
</note>
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 on the start of an element
function 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 in the end of an element
function Stop ($parser, $element _name)
{
echo "<br>";
}
Function to use when finding character data
function char ($parser, $data)
{
Echo $data;
}
Specify element Handler
Xml_set_element_handler ($parser, "Start", "Stop");
Specify data handler
Xml_set_character_data_handler ($parser, "char");
Open XML File
$FP =fopen ("Test.xml", "R");
Read data
while ($data =fread ($fp, 4096))
{
Xml_parse ($parser, $data, feof ($fp)) or
Die (sprintf ("XML Error:%s @ line%d",
Xml_error_string (Xml_get_error_code ($parser)),
Xml_get_current_line_number ($parser)));
}
Free the XML parser
Xml_parser_free ($parser);
?>
The above code will output:
--Note--
To:tove
From:jani
Heading:reminder
Message:don ' t forget me this weekend!
Working principle:
- 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
More information on PHP EXPAT parser
For more information on PHP Expat functions, please visit our PHP XML Parser reference Manual.
PHP XML Expat Parser