Php 简明教程

PHP - XML Introduction

借助 PHP 的内置函数和库,我们可以处理 XML 数据的处理。XML(eXtensible Markup Language 的缩写)是一种用于结构化文档交换的数据格式,特别是在网络上。

With the help of PHP’s built-in functions and libraries, we can handle manipulation of XML data. XML, which stands for eXtensible Markup Language, is a data format for structured document interchange, especially on the Web.

XML 是一种流行的文件格式,用于存储数据的序列化,将其传输到另一个位置并在目的地重建它。

XML is a popular file format used for serialization of data storing the data, transmitting it to another location, and reconstructing it at the destination.

在本章中,我们将学习使用 PHP 进行 XML 处理的基础知识。

In this chapter, we shall learn about the basics of XML processing with PHP.

Features of XML

XML 的一个特点是它既可读,又可机器读。XML 的规范由万维网联盟定义和标准化。PHP 解析器可以在 XML 数据上执行读/写操作。

One of the features of XML is that it is both human readable and machine readable. The specifications of XML are defined and standardized by The World Wide Web Consortium. PHP parser can perform read/write operations on XML data.

XML Tags

与 HTML 类似,XML 文档也是借助 tags 组成的。然而,你可以定义自己的标签,这不像 HTML,在其中你需要使用预定义的标签才能编写 HTML 文档。

Like HTML, XML document is also composed with the help of tags. However, you can define your own tags, which is unlike HTML where you need to use predefined tags to compose a HTML document.

HTML 标签本质上应用于文本、图像、多媒体资源等的格式化属性。XML 标签为数据元素定义用户指定的属性。

The HTML tags essentially apply formatting attributes over text, image, multimedia resources etc. The XML tags define user specified attributes to the data elements.

XML Document

XML 文档具有标签的层次结构,这些标签定义文档中数据的元素和属性。每个 XML 文档都包含一个根元素,它封装了其他元素。元素可以具有属性,这些属性提供有关元素的其他信息或特性。元素中的数据通过开始和结束标签来封装。

An XML document has a hierarchical structure of tags that define the elements and attributes of data within a document. Each XML document consists of a root element that encloses other elements. Elements can have attributes, which provide additional information or properties about the element. The data within elements are enclosed by opening and closing tags.

Example

以下是典型 XML 文档的示例 −

An example of a typical XML document is given below −

<?xml version = '1.0' encoding = 'UTF-8'?>
<note>
   <Course>Android</Course>
   <Subject>Android</Subject>
   <Company>TutorialsPoint</Company>
   <Price>$10</Price>
</note>

Types of XML Parsers

在 PHP 中,有两种类型的 XML 解析器 −

In PHP, there are two types of XML parsers available −

  1. Tree based parsers

  2. Event based parsers

Tree-based Parsers

使用这种类型的解析器,PHP 将在内存中加载整个 XML 文档,并将 XML 文档转换为树结构。它分析整个文档,并为树元素提供访问权限。

With this type of a parser, PHP loads the entire XML document in the memory and transforms the XML document into a Tree structure. It analyzes the whole document, and provides access to the Tree elements.

For smaller documents, tree-based parser works well ,但对于大型 XML 文档,它会导致重大的性能问题。 SimpleXML parserDOM XML parser 是基于树的解析器的示例

For smaller documents, tree-based parser works well, but for large XML document, it causes major performance issues. SimpleXML parser and DOM XML parser are the examples of tree-based parsers

Simple XML Parser

简单 XML 解析器也称为基于树的 XML 解析器,它将解析简单的 XML 文件。简单 XML 解析器将调用 simplexml_load_file() 方法以从特定路径获取 xml 的访问权限。

The Simple XML parser also called as tree-based XML parser and it will parse the simple XML file. Simple XML parse will call simplexml_load_file() method to get access to the xml from specific path.

DOM Parser

DOM 解析器也称为复杂节点解析器,用于解析高度复杂的 XML 文件。它用作修改 XML 文件的界面。DOM 解析器已使用 UTF-8 字符编码进行编码。

DOM Parser also called as a complex node parser, Which is used to parse highly complex XML file. It is used as interface to modify the XML file. DOM parser has encoded with UTF-8 character encoding.

Event-based Parsers

基于事件的解析器不会在内存中加载整个 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.

由于没有涉及内存过载,因此这种类型的解析器适用于大型 XML 文档,并且解析文档的速度比任何基于树的解析器都要快。XMLReader 和 XML Expat Parser 是基于事件的解析器的示例。

As there is no memory overload involved, this type of parser is suitable for large XML documents, and the document is parsed faster than any tree-based parser. XMLReader and XML Expat Parser are the examples of event-based parsers.

XML Parser

XML 解析基于 SAX 解析。它比所有上述解析器都要快。它将创建 XML 文件并解析 XML。XML 解析器已由 ISO-8859-1、US-ASCII 和 UTF-8 字符编码编码。

XML parsing is based on SAX parse. It is more faster the all above parsers. It will create the XML file and parse the XML. XML parser has encoded by ISO-8859-1, US-ASCII and UTF-8 character encoding.

XML Reader

XML 阅读器解析也称为 Pull XML 解析。它用于以更快的速度读取 XML 文件。它可以与具有 XML 验证的高复杂 XML 文档配合使用。

XML Reader parse also called as Pull XML parse. It is used to read the XML file in a faster way. It works with high complex XML document with XML Validation.