Home >Backend Development >PHP Tutorial >Detailed explanation of XML pull mode in PHP_PHP tutorial

Detailed explanation of XML pull mode in PHP_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 17:34:00814browse

Research and php(as the current mainstream development language) 5 bundled xml(standardization is getting closer)Reader library, which makes php (As the current mainstream development language) pages can process xml (standardization is getting closer) documents in an efficient streaming mode.

 php(as the current mainstream development language) 5 introduced a new class xml(standardization is getting closer)Reader, used to read extensible markup language (Extensible Markup Language, xml(Standardization is getting closer)). Unlike Simplexml(Standardization is getting closer) or the Document Object Model (DOM), xml(Standardization is getting closer)Reader operates in streaming mode . i.e. it reads the document from beginning to end. Before the content behind the document is compiled, the content in front of the compiled document can be processed first, thereby achieving very fast, very efficient, and very economical use of memory. The larger the document that needs to be processed, the more important this feature becomes.

 libxml(Standardization is getting closer)

 The xml mentioned here(Standardization is getting closer)Reader API is located in the libxml(Standardization is getting closer) library for C and C++ in the Gnome Project above. In fact, xml(Standardization is getting closer)Reader is just xml(Standardization is getting closer)(Standardization is getting closer) >A very thin php(as the current mainstream development language) layer on top of the TextReader API. xml(Standardization is getting closer)TextReader itself is imitating .NET’s xml(Standardization is getting closer)TextReader class and xml(Standardization is getting closer) )Reader classes, although there is no code similar to these classes.

Unlike Simple API for xml

(Standardization is getting closer) (SAX), xml(Standardization is getting closer)Reader is a push parser, not Pull the parser. This means that the program can be controlled. You're telling the parser when to get the next document fragment, rather than telling the parser what it saw after it has seen the document. You'll be requesting content, not reacting to content. Think about this problem from another angle: xml(Standardization is getting closer)Reader is an implementation of the Iterator design pattern, not an implementation of the Observer design pattern.

 

Example question

Let’s start with a simple example. Suppose you are writing a php

(as the current mainstream development language) script to receive xml(standardization is getting closer)-RPC requests and generate responses. To be more specific, assume that the request looks like Listing 1. The root element of the document is methodCall, which contains methodName elements and params elements. The name of the method is sqrt.params element contains a param element, the param element contains a double, the square root of the double is the desired value. No namespace is used.

 

List 1. xml(Standardization is getting closer)-RPC request

<b>以下是引用片段:<br></b><?xml<font class=reblank>(标准化越来越近了)</font> version="1.0"?> <br><methodCall> <br>  <methodName>sqrt</methodName> <br>  <params> <br>    <param> <br>      <value><double>36.0</double></value> <br>    </param> <br>  </params> <br></methodCall>
The following is the work that the php

(as the current mainstream development language) script needs to complete:

1. Check the method name, and if it is not sqrt (which is the only method the script knows how to handle), generate an error response.

 2. Find the parameter. If the parameter does not exist or the parameter type is wrong, an error response is generated.

3. In addition, calculate the square root.

4. Return the results in the form, as shown in Listing 2.

 

Listing 2. xml(Standardization is getting closer)-RPC response

<b>以下是引用片段:<br></b><?xml<font class=reblank>(标准化越来越近了)</font> version="1.0"?> <br><methodResponse> <br>  <params> <br>    <param> <br>      <value><double>6.0</double></value> <br>    </param> <br>  </params> <br></methodResponse>
We will explain step by step below.

Initialize the parser and load the document

The first step is to create a new parser object. The creation operation is simple:

<b>以下是引用片段:<br></b>$reader = new xml<font class="reblank">(标准化越来越近了)</font>Reader();
Next, you need to provide it with some data for parsing. For xml

(standardization getting closer)-RPC, this is the raw body of a Hypertext Transfer Protocol (HTTP) request. That string can then be passed to the reader's xml(standardization is getting closer)() function:

Fill the original sending data

  如果发现 $HTTP_RAW_POST_DATA 是空的,则将以下代码行添加到 php(做为现在的主流开发语言).ini 文件:

<b>以下是引用片段:<br></b>  always_populate_raw_post_data = On

  可以解析任何字符串,无论它是从何处获取的。例如,可以是程序中的一串文字或从本地文件读取。还可以使用 open() 函数从外部 URL 载入数据。例如,下面的语句准备解析其中一个 Atom 提要:

<b>以下是引用片段:<br></b>  $reader->xml<font class="reblank">(标准化越来越近了)</font>(http://www.cafeaulait.org/today.atom);

  无论是从何处获取原始数据,现在已建立了阅读器并为解析做好准备。

  读取文档

  read() 函数使解析器前进到下一个标记。最简单的方法是在 while 循环中遍历整个文档:

<b>以下是引用片段:<br></b>  while ($reader->read()) { <br>  // processing code goes here... <br>  }

  完成遍历后,关闭解析器以释放它所持有的任何资源,并且重置解析器以便用于下一个文档:

<b>以下是引用片段:<br></b>  $reader->close();

  在循环内部,将解析器放置在特殊节点上:元素的起点、元素的终点、文本节点、注释等等。通过检查以下属性,可以发现解析器正在查看的内容:

  localName 是本地的、未带前缀的节点名。

  name 是可能的节点前缀名。对于像注释这种没有名称的节点,包括 #comment、#text、#document 等等,与 DOM 中的一样。

  namespaceURI 是节点名称空间的统一资源标识符(Uniform Resource Identifier,URI)。

  nodeType 是代表节点类型的整数 —— 例如,2 代表属性节点,7 代表处理指令。

  prefix 是节点的名称空间前缀。

  value 是节点的下一个文本内容。

  如果节点有文本值,hasValue 值为 true;否则,值为 false.

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/508532.htmlTechArticle研究与 php (做为现在的主流开发语言) 5 捆绑在一起的 xml (标准化越来越近了) Reader 库,它使 php (做为现在的主流开发语言) 页面能够以高效...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn