XML pull mode parsing in PHP

Source: Internet
Author: User
Tags object model php script square root

PHP 5 introduces a new class XMLReader for reading Extensible Markup Language (extensible Markup language,xml). Unlike the SimpleXML or the Document Object Model,dom, XMLReader operates in streaming mode. That is, it reads the document from beginning to end. Before the contents of the document are compiled, you can work with the content before the compiled document, enabling very fast, very efficient, and very economical use of memory. The larger the document that needs to be processed, the more important this feature is.

Unlike simple APIs for XML (SAX), XMLReader is a push parser, not a pull parser. This means that the program can be controlled. You will tell the parser when to get the next document fragment, instead of telling you what you see when the parser sees the document. Instead of reacting to content, you will request content. Consider the problem from another perspective: XMLReader is the implementation of the iterator design pattern, not the Observer design pattern.

Sample problem

Let's start with a simple example. Suppose you are writing a PHP script to receive an XML-RPC request and generate a response. More specifically, suppose the request is shown in Listing 1. The document's root element is Methodcall, which contains methodname elements and params elements. The name of the method is sqrt. The params element contains a PARAM element, and the Param element contains the double,double square root as the desired value. The namespace is not used.

Listing 1. XML-RPC Request

<?xml version="1.0"?>
<methodCall>
 <methodName>sqrt</methodName>
 <params>
  <param>
   <value><double>36.0</double></value>
  </param>
 </params>
</methodCall>

Here's what your PHP script needs to do:

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

The parameter is found, and an error response is generated if the parameter does not exist or if the parameter type is incorrect.

In addition, the square root is computed.

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

Listing 2. XML-RPC Response

<?xml version="1.0"?>
<methodResponse>
 <params>
  <param>
   <value><double>6.0</double></value>
  </param>
 </params>
</methodResponse>

Let's start with a step-by-step description.

Initializing the parser and loading the document

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

$reader = new XMLReader();

Next, you need to provide it with some data for resolution. For XML-RPC, this is the original body of the Hypertext Transfer Protocol (Hypertext Transfer protocol,http) request. You can then pass the string to the reader's XML () function:

$request = $HTTP_RAW_POST_DATA;
$reader->XML($request);

You can parse any string, regardless of where it was obtained. For example, you can be a string of text in a program or read from a local file. You can also use the open () function to load data from an external URL. For example, the following statement prepares to parse one of the Atom feeds:

$reader->XML('http://www.cafeaulait.org/today.atom');

No matter where you get the raw data, the reader is now set up and ready for parsing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.