Research with PHP (now the mainstream development language) 5 bundled XML(standardization is getting closer)Reader library, which makes PHP(as the current mainstream development language) page can be efficient flow mode to process XML Documentation (standardization is getting closer) .
PHP(as the current mainstream development language) 5 introduced a new class XML(standardization is getting closer)reader, for reading Extensible Markup Language (extensible Markup language,xml( Standardization is getting closer)). Unlike the Simplexml (which isbecoming more standardized) or the Document Object model (documents objects Model,dom), XML(standardization 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, 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.
Libxml(standardization is getting closer)
XML(standardization is getting closer)here the Reader API is located above the library for C and C + + Libxml(standardization is getting closer) in Gnome Project. In fact, XML(standardization is getting closer)Reader is just a thin php on the libxml (standardization is getting closer) to the TextReader API ( As the current mainstream development language) layer. XML(standardization is getting closer)TextReader itself is imitating. NET XML(standardization is getting closer)TextReader class and XML(standardization is getting closer)Reader class, Although there is no code similar to these classes.
Unlike simple APIs for XML(where standardization is getting closer) (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 when the parser sees the document. Instead of reacting to content, you will request content. Consider the problem from another perspective: XML(standardization is getting closer)Reader is the implementation of iterator design patterns, not Observer design patterns.
Sample problem
Let's start with a simple example. Suppose you are writing PHP(as the current mainstream development language) script to receive XML(standardization is getting closer)-RPC requests and generates responses. 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 that the Sqrt.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(standardization is getting closer)-RPC request
The following is a reference fragment: <?xml(standardization is getting closer)
</methodCall> |
Here's what PHP(as the current mainstream development language) script needs to do:
1, check the method name, if not sqrt (it is the script knows how to deal with the only way), then generate an error response.
2. Find the parameters, and generate an error response if the parameter does not exist or if the parameter type is wrong.
3, in addition, calculate the square root.
4, return the result in the form, as shown in Listing 2.
Listing 2. XML(standardization is getting closer)-RPC response
The following is a reference fragment: <?xml(standardization is getting closer)
</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:
The following is a reference fragment: $reader = new XML(standardization is getting closer)reader (); |
Next, you need to provide it with some data for resolution. FOR XML(standardization is getting closer)-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(the standardization is getting closer)() function:
Populating raw Send data
The following is a reference fragment:
$reader->xml(standardization is getting closer)($request); |
If you find that the $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 |
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:
The following is a reference fragment: $reader->xml(standardization is getting closer)(' Http://www.cafeaulait.org/today.atom '); |
No matter where you get the raw data, the reader is now set up and ready for parsing.
Reading documents
The read () function causes the parser to advance to the next tag. The easiest way to do this 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 the next document:
The following is a reference fragment: $reader->close (); |
Inside the loop, place the parser on a special node: the start of the element, the end 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, not prefixed node name.
Name is the possible node prefix name. For nodes such as annotations that have no name, including #comment, #text, #document, and so on, are the same 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 represents the attribute node and 7 represents the processing instruction.
Prefix is the namespace prefix of the node.
Value is the next text content of the node.
True if the node has a literal value, or False if the HasValue value is zero.