当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > XML Expat Parser

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 XML Expat Parser


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 926 ::
收藏到网摘: n/a

The built-in Expat parser makes it possible to process XML documents in PHP.
内置的Expat解析器可以使XML文档在PHP中运行。


What is XML?
什么是XML?

XML is used to describe data and to focus on what data is. An XML file describes the structure of the data.
XML是用来描述数据及其本身的属性的。一个XML文件描述了数据结构。

In XML, no tags are predefined. You must define your own tags.
在XML中,无需预先定义标签[tag],你可以自定义标签。

If you want to learn more about XML, please visit our XML tutorial.
如果你先学习关于XML的更多知识,请访问我们的XML教程。


What is Expat?
什么是Expat?

To read and update - create and manipulate - an XML document, you will need an XML parser.
为了对一个XML文档进行读取/修改、创建/操作,你需要一个XML解析器。

There are two basic types of XML parsers:
下面介绍一下XML解析器的两种基本类型:

  • Tree-based parser: This parser transforms an XML document into a tree structure. It analyzes the whole document, and provides access to the tree elements. e.g. the Document Object Model (DOM)
    “树型”解析器[Tree-based parser]:这个解析器将一个XML文件转换成一个树结构。他对整个文档进行分析,并提供访问“树元素”的路径。比如:文档对象模型[Document Object Model (DOM)]
  • Event-based parser: Views an XML document as a series of events. When a specific event occurs, it calls a function to handle it
    “事件”解析器[Event-based parser]:该解析器将XML文档看作是一系列的事件。当一个具体的事件发生时,它会请求一个函数去处理它

The Expat parser is an event-based parser.
Expat解析器是一个“事件”解析器[Event-based parser]。

Event-based parsers focus on the content of the XML documents, not its structure. Because of this, event based parsers can access data faster than Tree-based parsers.
“事件”解析器主要针对XML文档的内容进行解析,而不是它的结构。由于这个原因“事件”解析器对数据的访问速度要比“树型”解析器更快。

Look at the following XML fraction:
看下面这个XML片段:

<from>Jani</from>

An event-based parser reports the XML above as a series of three events:
“事件解析器”通过三个方面的事件对上述XML进行陈述:

  • Start element: from
    开始元素:<from>
  • Start CDATA section, value: Jani
    开始的CDATA部分、值:jani
  • Close element: from
    结束部分:</from>

The XML example above contains well-formed XML. However, the example is not valid XML, because there is no Document Type Definition (DTD) associated with it.
上述的XML案例包含了格式完整的XML。然而,这个案例并不是一个有效的XML,因为它并未包含文档类型定义[Document Type Definition (DTD)]。

However, this makes no difference when using the Expat parser. Expat is a non-validating parser, and ignores any DTDs.
然而,这和使用Expat解析器并不存在什么不同。Expat解析器本身就不对有效性进行验证,它将忽略所有的DTD。

As an event-based, non-validating XML parser, Expat is fast and small, and a perfect match for PHP web applications.
作为一个“事件”解析器,Expat并不对XML进行有效性的验证;但它的速度更快,且所占的空间更小,并且它与PHP的网络应用程序匹配的天衣无缝。

Note: XML documents must be well-formed or Expat will generate an error.
注意:XML文档必须具有完整的格式,否则,Expat将产生一个错误。


Installation
安装

The XML Expat parser functions are part of the PHP core. There is no installation needed to use these functions.
XML Expat解析器函数是PHP核心的一部分,所以无需安装。


An XML File
XML文件

The XML file below will be used in our example:
我们用下面的例子展示XML文件:

<?xml version="1.0" encoding="ISO-8859-1"?>

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>

</note>


Initializing the XML Parser
安装XML解析器

We want to initialize the XML parser in PHP, define some handlers for different XML events, and then parse the XML file.
我们打算在PHP中安装XML解析器,为不同的XML事件定义不同的处理方法,然后对XML文件进行解析。

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

The output of the code above will be:
上述代码将输出下面的结果:

-- Note --
To: Tove
From: Jani
Heading: Reminder
Message: Don't forget me this weekend!

How it works:
它的工作原理:

  1. Initialize the XML parser with the xml_parser_create() function
    通过xml_parser_create()函数安装XML解析器;
  2. Create functions to use with the different event handlers
    使用不同的事件处理其建立函数;
  3. Add the xml_set_element_handler() function to specify which function will be executed when the parser encounters the opening and closing tags
     通过添加xml_set_element_handler()函数来指定需要执行的函数(当解析器遇到开始或结束标签时)
  4. Add the xml_set_character_data_handler() function to specify which function will execute when the parser encounters character data
    通过添加xml_set_element_handler()函数来指定需要执行的函数(当解析器遇到字符时)
  5. Parse the file "test.xml" with the xml_parse() function
    通过xml_parse()函数来解析“test.xml”文件
  6. In case of an error, add  xml_error_string() function to convert an XML error to a textual description
    当出现错误情况时,通过添加xml_error_string()函数将一个XML的错误转换成一个文本描述
  7. Call the xml_parser_free() function to release the memory allocated with the xml_parser_create() function
    通过请求xml_parser_free()函数来释放xml_parser_create()函数分配的内存

More PHP Expat Parser
更关于PHP Expat解析器的知识

For more information about the PHP Expat functions, visit our PHP XML Parser Reference.
如果你希望了解更多关于PHP Expat函数的信息,请访问我们的PHP XML Parser 参数。

评论 (0) All

登陆 | 还没注册?