Summary of knowledge points of PHP XML Expat parser

  • 2021-11-24 01:06:02
  • OfStack

The built-in Expat parser makes it possible to process XML documents in PHP.

What is XML?

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

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

What is Expat?

To read and update-create and process-1 XML document, you need an XML parser.

There are two basic XML parser types:

Tree-based parser: This parser converts XML documents into tree structure. It analyzes the entire document and provides API to access elements of the tree species, such as the Document Object Model (DOM). Event-based parser: treat XML documents as 1 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 contents of XML documents, not their results. Because of this, event-based parsers can access data faster than tree-based parsers.

See the following XML fragment:


<from>John</from>

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

Start element: from Start the CDATA section, value: John Close element: from

The above example of XML contains XML in good form. However, this example is an invalid XML because there is no document type declaration (DTD) associated with it, and there is no embedded DTD.

However, when using the Expat parser, this makes no difference. Expat is a parser that does not check validity and ignores any DTD.

As an event-based, non-authenticated XML parser, Expat is fast and lightweight, making it 10 points suitable for web applications of PHP.

Note: The XML document must be in good form, otherwise Expat will generate errors.

Installation

The XML Expat parser is part of the PHP core. You can use these functions without installing them.

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>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

Initialize the XML parser

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

Example


<?php

//Initialize the XML parser
$parser=xml_parser_create();

//Function to use at 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 at 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 at line %d", 
 xml_error_string(xml_get_error_code($parser)),
 xml_get_current_line_number($parser)));
 }

//Free the XML parser
xml_parser_free($parser);

?>

Output of the above code:

-- Note --
To: George
From: John
Heading: Reminder
Message: Don't forget the meeting!

Explanation of working principle:

Initialize the XML parser with the xml_parser_create () function Create functions that match 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" through the xml_parse () function If there is an error, add the xml_error_string () function to convert the XML error into a text description Call the xml_parser_free () function to free the memory allocated to the xml_parser_create () function

Related articles: