Php 简明教程

PHP - SAX Parser Example

PHP 在 php.ini 设置文件中默认启用了 XML 解析器扩展。该解析器实现了 SAX API,这是一种基于事件的解析算法。

PHP has the XML parser extension enabled by default in the php.ini settings file. This parser implements SAX API, which is an event-based parsing algorithm.

基于事件的解析器不会在内存中加载整个 XML 文档。相反,它一次读取一个节点。解析器允许你实时交互。一旦你移到下一个节点,以前的那个节点就会从内存中移除。

An event-based parser doesn’t load the entire XML document in the memory. instead, it reads in one node at a time. The parser allows you to interact with in real time. Once you move onto the next node, the old one is removed from the memory.

基于 SAX 的解析机制比基于树的解析器更快。PHP 库包含用于处理 XML 事件的函数,如本章所述。

SAX based parsing mechanism is faster than the tree based parsers. PHP library includes functions to handle the XML events, as explained in this chapter.

解析 XML 文档的第一步是让一个解析器对象使用 xml_parse_create() 函数。

The first step in parsing a XML document is to have a parser object, with xml_parse_create() function

xml_parser_create(?string $encoding = null): XMLParser

此函数创建一个新的 XML 解析器,并返回一个 XMLParser 对象供其他 XML 函数使用。

This function creates a new XML parser and returns an object of XMLParser to be used by the other XML functions.

xml_parse() 函数开始解析 XML 文档

The xml_parse() function starts parsing an XML document

xml_parse(XMLParser $parser, string $data, bool $is_final = false): int

xml_parse() 解析 XML 文档。已配置事件的处理程序将按需要多次调用。

xml_parse() parses an XML document. The handlers for the configured events are called as many times as necessary.

XMLParser 扩展提供不同的事件处理程序函数。

The XMLParser extension provides different event handler functions.

xml_set_element_handler()

此函数设置 XML 解析器的元素处理程序函数。每当 XML 解析器遇到开始或结束标记时,就会发出元素事件。有单独的处理程序来处理开始标记和结束标记。

This function sets the element handler functions for the XML parser. Element events are issued whenever the XML parser encounters start or end tags. There are separate handlers for start tags and end tags.

xml_set_element_handler(XMLParser $parser, callable $start_handler,
   callable $end_handler): true

当打开新的 XML 元素时,调用 start_handler() 函数。当关闭 XML 元素时,调用 end_handler() 函数。

The start_handler() function is called when a new XML element is opened. end_handler() function is called when an XML element is closed.

xml_set_character_data_handler()

此函数设置 XML 解析器 parser 的字符数据处理程序函数。字符数据大概包括 XML 文档中所有非标记内容,包括标记之间的空格。

This function sets the character data handler function for the XML parser parser. Character data is roughly all the non-markup contents of XML documents, including whitespace between tags.

xml_set_character_data_handler(XMLParser $parser, callable $handler): true

xml_set_processing_instruction_handler()

此函数设置 XML 解析器 parser 的处理指令 (PI) 处理程序函数。<?php ?> 是一个处理指令,其中 php 称为“PI 目标”。它们的处理是特定于应用程序的。

This function sets the processing instruction (PI) handler function for the XML parser parser. <?php ?> is a processing instruction, where php is called the "PI target". The handling of these are application-specific.

xml_set_processing_instruction_handler(XMLParser $parser, callable $handler): true

processing instruction 具有以下格式 -

A processing instruction has the following format −

<?target
   data
?>

xml_set_default_handler()

此函数设置 XML 解析器 parser 的默认处理程序函数。未转交给其他处理程序的所有内容都转交给默认处理程序。您将在默认处理程序中获得 XML 和文档类型声明等内容。

This function sets the default handler function for the XML parser parser. What goes not to another handler goes to the default handler. You will get things like the XML and document type declarations in the default handler.

xml_set_default_handler(XMLParser $parser, callable $handler): true

Example

以下示例演示了用于解析 XML 文档的 SAX API 的使用。我们将使用 SAX.xml 如下所示 -

The following example demonstrates the use of SAX API for parsing the XML document. We shall use the SAX.xml as below −

<?xml version = "1.0" encoding = "utf-8"?>
<tutors>
   <course>
      <name>Android</name>
      <country>India</country>
      <email>contact@tutorialspoint.com</email>
      <phone>123456789</phone>
   </course>

   <course>
      <name>Java</name>
      <country>India</country>
      <email>contact@tutorialspoint.com</email>
      <phone>123456789</phone>
   </course>

   <course>
      <name>HTML</name>
      <country>India</country>
      <email>contact@tutorialspoint.com</email>
      <phone>123456789</phone>
   </course>
</tutors>

Example

以下是解析上述文档的 PHP 代码。它打开 XML 文件并调用 xml_parse() 函数,直至文件结束。事件处理程序将数据存储在 tutors 数组中。然后,按元素顺序回显该数组。

The PHP code to parse the above document is given below. It opens the XML file and calls xml_parse() function till its end of file is reached. The event handlers store the data in tutors array. Then the array is echoed element wise.

<?php

   // Reading XML using the SAX(Simple API for XML) parser
   $tutors   = array();
   $elements   = null;

   // Called to this function when tags are opened
   function startElements($parser, $name, $attrs) {
      global $tutors, $elements;
      if(!empty($name)) {
         if ($name == 'COURSE') {

            // creating an array to store information
            $tutors []= array();
         }
         $elements = $name;
      }
   }

   // Called to this function when tags are closed
   function endElements($parser, $name) {
      global $elements;

      if(!empty($name)) {
         $elements = null;
      }
   }

   // Called on the text between the start and end of the tags
   function characterData($parser, $data) {
      global $tutors, $elements;
      if(!empty($data)) {
         if ($elements == 'NAME' || $elements == 'COUNTRY' ||  $elements == 'EMAIL' ||  $elements == 'PHONE') {
            $tutors[count($tutors)-1][$elements] = trim($data);
         }
      }
   }

   $parser = xml_parser_create();
   xml_set_element_handler($parser, "startElements", "endElements");
   xml_set_character_data_handler($parser, "characterData");

   // open xml file
   if (!($handle = fopen('sax.xml', "r"))) {
      die("could not open XML input");
   }

   while($data = fread($handle, 4096)) {
      xml_parse($parser, $data);
   }

   xml_parser_free($parser);
   $i = 1;

   foreach($tutors as $course) {
      echo "course No - ".$i. '<br/>';
      echo "course Name - ".$course['NAME'].'<br/>';
      echo "Country - ".$course['COUNTRY'].'<br/>';
      echo "Email - ".$course['EMAIL'].'<br/>';
      echo "Phone - ".$course['PHONE'].'<hr/>';
      $i++;
   }
?>

上述代码给出了以下输出 -

The above code gives the following output −

course No - 1
course Name - Android
Country - India
Email - contact@tutorialspoint.com
Phone - 123456789
________________________________________
course No - 2
course Name - Java
Country - India
Email - contact@tutorialspoint.com
Phone - 123456789
________________________________________
course No - 3
course Name - HTML
Country - India
Email - contact@tutorialspoint.com
Phone - 123456789
________________________________________