XML pull mode in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags net xml what php
Detailed description of XML pull mode in PHP. Research on the xml Library bundled with php (as the mainstream development language) 5 (standardization is getting closer and closer), which makes php (as the mainstream development language) the Reader library that binds pages with php (as the mainstream development language) 5 for efficient research (standardization is getting closer and closer, it enables php (as the mainstream development language) pages to process xml (standardization is getting closer and closer) documents in an efficient stream mode.

Php (as the mainstream development Language) 5 introduces a new class xml (standardization is getting closer and closer) Reader for reading Extensible Markup Language (Extensible Markup Language, xml (standardization is getting closer and closer )). Unlike Simplexml (standardization is getting closer) or Document Object Model (DOM), xml (standardization is getting closer and closer) Reader operates in stream mode. That is, it reads documents from start to end. Before the content after the document is compiled, you can process the content before the compiled document to achieve very fast, efficient, and very cost-effective memory usage. The larger the document to be processed, the more important this feature is.

  Libxml (standardization is getting closer and closer)

Here, the xml (standardization is getting closer and closer) Reader API is located above the libxml (standardization is getting closer and closer) library used in C and C ++ in the Gnome Project. In fact, xml (standardization is getting closer and closer) Reader is only xml in libxml (standardization is getting closer and closer) the thin php (as the mainstream development language) layer on the TextReader API. Xml (standardization is getting closer and closer) TextReader is an imitation. NET xml (standardization is getting closer and closer) TextReader class and xml (standardization is getting closer and closer) Reader class, although it does not have code similar to these classes.

Unlike Simple API for xml (standardization is getting closer and closer) (SAX), xml (standardization is getting closer and closer) Reader is a push parser rather than a pull parser. This means that the program is controllable. You will tell the parser when to get the next document segment, instead of telling you what you see after the parser sees the document. You will request the content instead of responding to the content. Consider this problem from another perspective: xml (standardization is getting closer and closer) Reader is the implementation of the Iterator design pattern, rather than the implementation of the Observer design pattern.

  Example

Start with a simple example. Assume that php (as the mainstream development language) scripts are being compiled to receive xml (standardization is getting closer and closer)-RPC requests and generate responses. More specifically, let's assume that the request is shown in listing 1. The root element of the document is methodCall, which contains the methodName and params elements. The method name is sqrt. params. the params element contains a param element, and the param element contains a double element. the square root of the double element is the expected value. No namespace is used.

  Listing 1. xml (standardization is getting closer and closer)-RPC request

The following is a reference clip:


Sqrt


36.0


The following is what php (as the mainstream development language) scripts need to do:

1. check the method name. if it is not sqrt (it is the only method that the script knows how to handle), an error response is generated.

2. find the parameter. if the parameter does not exist or the parameter type is incorrect, an error response is generated.

3. In addition, calculate the square root.

4. return results in the form, as shown in listing 2.

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

The following is a reference clip:




6.0


The following is a step-by-step description.

Initialize the parser and load the document

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

The following is a reference clip:
$ Reader = new xml (standardization is getting closer and closer) Reader ();

Next, you need to provide some data for parsing. For xml (standardization is getting closer and closer)-RPC, this is the original body of Hypertext Transfer Protocol (HTTP) requests. Then you can pass the string to the reader's xml (standardization is getting closer and closer) () function:

Fill in original sent data

The following is a reference clip:
$ Request = $ HTTP_RAW_POST_DATA;
$ Reader-> xml (standardization is getting closer and closer) ($ request );

If you find that $ HTTP_RAW_POST_DATA is empty, add the following code lines to the php (as the mainstream development language). ini file:

The following is a reference clip:
Always_populate_raw_post_data = On

It can parse any string, no matter where it is obtained. For example, it 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 is intended to parse one of the Atom feeds:

The following is a reference clip:
$ Reader-> xml (standardization is getting closer and closer) (http://www.cafeaulait.org/today.atom );

No matter where you get the raw data, you have now created a reader and prepared for parsing.

  Read document

The read () function enables the parser to move forward to the next tag. The simplest way is to traverse the entire document in the while loop:

The following is a reference clip:
While ($ reader-> read ()){
// Processing code goes here...
}

After traversing, close the parser to release any resources it holds and reset the parser for use in the next document:

The following is a reference clip:
$ Reader-> close ();

In the Loop, the parser is placed on a special node: The starting point of the element, the ending point of the element, the text node, and the comment. Check the following attributes to find what the parser is viewing:

LocalName is a local node name without a prefix.

Name is a possible node prefix name. Nodes without names such as # comment, # text, and # document are the same as those in the DOM.

NamespaceURI is the Uniform Resource Identifier (URI) of the node namespace ).

NodeType is an integer that represents the node type. for example, 2 represents the attribute node, and 7 represents the processing command.

Prefix is the prefix of the node namespace.

Value is the next text content of the node.

If the node has a text value, the hasValue is true; otherwise, the value is false.

Php (as the mainstream development language) 5 is bundled with xml (standardization is getting closer and closer) Reader library, which makes php (as the mainstream development language) the page can be efficient...

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.