Inaruka ya Expat wa XML cha PHP

Course recommendation:

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

What is XML?

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

If you want to learn more about XML, please visit our Mafunzo ya XML.

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 converts the XML document into a tree structure. It analyzes the entire document and provides an API to access elements in the tree, such as the Document Object Model (DOM).
  • Event-based parser: Treat the XML document as a series of events. When a specific event occurs, the parser will call a function to handle it.

Expat parser is an event-based parser.

An event-based parser focuses on the content of the XML document rather than its results. For this reason, an event-based parser can access data faster than a tree-based parser.

Please see the following XML snippet:

<from>John</from>

An 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 above XML example contains well-formed XML. However, this example is invalid XML because it does not have an associated document type declaration (DTD) or an embedded DTD.

However, there is no difference when using the Expat parser. Expat is not a validating parser and ignores any DTD.

As an event-based, non-validating XML parser, Expat is fast and lightweight, making it very suitable for PHP web applications.

Note:The XML document must be well-formed, otherwise Expat will generate errors.

Installation

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

XML file

In our example, we will use the following XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Do not forget the meeting!</body>
</note>

Kuanzisha mtaalamu wa XML

Tunawafikia kwa PHP kuanzisha mtaalamu wa XML, kufikia vifaa vya matukio ya XML ya kina, na kuagiza faili ya XML hii.

Mfano

<?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);
?>

以上代码的输出:

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

工作原理解释:

  • 通过 xml_parser_create() 函数初始化 XML 解析器
  • 创建配合不同事件处理程序的的函数
  • 添加 xml_set_element_handler() 函数来定义,当解析器遇到开始和结束标签时执行哪个函数
  • 添加 xml_set_character_data_handler() 函数来定义,当解析器遇到字符数据时执行哪个函数
  • 通过 xml_parse() 函数来解析文件 "test.xml"
  • 万一有错误的话,添加 xml_error_string() 函数把 XML 错误转换为文本说明
  • 调用 xml_parser_free() 函数来释放分配给 xml_parser_create() 函数的内存

更多 PHP Expat 解析器的信息

如需更多有关 PHP Expat 函数的信息,请访问我们的 PHP XML Parser 参考手册。