Research with 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) page can be in efficient streaming mode to process the XML (standardization is getting closer) document.
PHP (as the current mainstream development language) 5 introduced new class XML (standardized getting closer) reader, for reading Extensible Markup Language (extensible Markup language,xml (standardization is getting closer)). Unlike the Simplexml (standardization is getting closer) or the Document Object model (Doc object model,dom), XML (normalization is getting closer) Reader operates in streaming mode. That is, it reads the document from beginning to end. Before the contents of the document are compiled, it is possible to process the contents of the pre-compiled document before it is ready for 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 getting closer)
The XML (standardization is getting closer) here, the Reader API is located above the Libxml (standardized getting closer) library for C and C + + in Gnome Project. In fact, XML (standardization is getting closer) Reader is just a layer of thin PHP (as the current mainstream development language) on the Libxml (standardization getting closer) XML (standardization getting closer) TextReader API. XML (standardization is getting closer) TextReader itself is emulating. NET XML (standardization is getting closer) the TextReader class and XML (standardization is getting closer) Reader class, although there is no code similar to these classes.
Unlike simple APIs for XML (which is getting closer to standardization) (SAX), XML (standardization is getting closer) reader 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 after the parser sees the document. Instead of reacting to content, you will request content. Consider this issue from another perspective: XML (Standardization is getting closer) Reader is the implementation of the Iterator design pattern rather than the Observer design pattern.
Example issues
Let's start with a simple example. Suppose you are writing PHP (now a mainstream development language) script to receive XML (normalization getting closer)-RPC requests and generate responses. More specifically, assume that the request is shown in Listing 1. The root element of the document is Methodcall, which contains the methodName element and the params element. The name of the method is the Sqrt.params element that contains a PARAM element, and the Param element that contains the square root of the double,double is the value that you want to get. No namespaces are used.
Listing 1. XML (standardization getting closer)-rpc request
The following is a reference fragment:
sqrt
36.0
|
Here's what PHP (as the current mainstream development language) script needs to do:
1. Check the method name and generate an error response if it is not a sqrt (it is the only method that the script knows how to handle).
2. The parameter is found and an error response is generated if the parameter does not exist or the parameter type is incorrect.
3, in addition, calculates the square root.
4. Return the results in the form, as shown in Listing 2.
Listing 2. XML (standardization getting closer)-RPC response
The following is a reference fragment:
6.0
|
Let's step through the instructions below.
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 fragment: $reader = new XML (standardization getting closer) reader (); |
Next, you need to provide some data for it to parse. FOR XML (normalization is getting closer)-rpc, this is the original principal of the Hypertext Transfer Protocol (Hypertext Transfer protocol,http) request. The string can then be passed to the reader's XML (normalized getting closer) () function:
Populate Raw Send data
The following is a reference fragment:
$reader->xml (standardization getting closer) ($request); |
If you find that $HTTP _raw_post_data is empty, add the following line of code to PHP (as the current mainstream development language). ini file:
The following is a reference fragment: Always_populate_raw_post_data = On |
Any string can be parsed, regardless of where it was obtained from. 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 prepares to parse one of the Atom feeds:
The following is a reference fragment: $reader->xml (standardization getting closer) (Http://www.cafeaulait.org/today.atom); |
No matter where the raw data is obtained, the reader is now set up and ready for parsing.
Reading documents
The read () function advances the parser to the next tag. The simplest approach is to traverse the entire document in the while loop:
The following is a reference fragment:
} |
When the traversal is complete, close the parser to free any resources it holds, and reset the parser for use in the next document:
The following is a reference fragment: $reader->close (); |
Inside the loop, the parser is placed on a special node: The starting point of the element, the end point of the element, the text node, the comment, and so on. You can discover what the parser is viewing by examining the following properties:
The LocalName is a local, non-prefixed node name.
Name is the possible node prefix name. For nodes like annotations that have no name, including #comment, #text, #document, and so on, as in the DOM.
NamespaceURI is the Uniform Resource Identifier (Uniform Resource Identifier,uri) of the node namespace.
NodeType is an integer representing the node type--for example, 2 for the attribute node and 7 for the processing instruction.
Prefix is the namespace prefix for the node.
Value is the next text content of the node.
If the node has a literal value, the HasValue value is true; otherwise, the value is false.
http://www.bkjia.com/PHPjc/508532.html www.bkjia.com true http://www.bkjia.com/PHPjc/508532.html techarticle Research and PHP (as the current mainstream development language) 5 bundled XML (standardization is getting closer) Reader library, it makes PHP (as the mainstream development language now) page can be efficient ...