Home >
Backend Development >
PHP Tutorial >
PHP XML parsing function transfer There are not many articles introducing this XML parsing function in PHP. . It should be clear after reading this article..._PHP Tutorial
PHP XML parsing function transfer There are not many articles introducing this XML parsing function in PHP. . It should be clear after reading this article..._PHP Tutorial
WBOY
Release: 2016-07-13 17:28:45
Original
1076 people have browsed it
PHP's XML parsing functions First of all, I have to admit that I like computer standards. If everyone followed the industry's standards, the Internet would be a better medium. The use of standardized data exchange formats makes open and platform-independent computing models feasible. That's why I'm an XML enthusiast. Fortunately, my favorite scripting language not only supports XML but is increasingly supporting it. PHP allows me to quickly publish XML documents to the Internet, collect statistical information about XML documents, and convert XML documents into other formats. For example, I often use PHP's XML processing capabilities to manage articles and books I write in XML. In this article, I will discuss any use of PHP's built-in Expat parser to process XML documents. Through examples, I will demonstrate the processing method of Expat. At the same time, the examples can show you how to: Create your own processing functions Convert XML documents into your own PHP data structures Introduce the Expat XML parser, also called an XML processor, which allows programs to access the structure and content of XML documents. Expat is an XML parser for the PHP scripting language. It is also used in other projects, such as Mozilla, Apache and Perl. What is an event-based parser? There are two basic types of XML parsers: Tree-based parsers: convert XML documents into tree structures. This type of parser parses the entire article while providing an API to access each element of the resulting tree. Its common standard is DOM (Document Object Model). Event-based parser: Treat XML documents as a series of events. When a special event occurs, the parser will call the function provided by the developer to handle it. The event-based parser has a data-focused view of the XML document, which means that it focuses on the data portion of the XML document rather than its structure. These parsers process the document from beginning to end and report events like - start of element, end of element, start of feature data, etc. - to the application through callback functions. The following is an example XML document of "Hello-World": Hello World The event-based parser will report as three events: Start element: The beginning of the greeting CDATA item, with the value: Hello World End element: greeting Unlike tree-based parsers, event-based parsers do not produce a structure that describes the document. In CDATA items, the event-based parser will not let you get the greeting information of the parent element. However, it provides a lower level access, which allows for better utilization of resources and faster access. This way, there is no need to fit the entire document into memory; in fact, the entire document can even be larger than the actual memory value. Expat is such an event-based parser. Of course, if you use Expat, it can also generate a complete native tree structure in PHP if necessary. The Hello-World example above includes the complete XML format. But it is invalid because there is neither a DTD (Document Type Definition) associated with it nor an embedded DTD. For Expat, this makes no difference: Expat is a parser that does not check validity and therefore ignores any DTD associated with the document. It should be noted, however, that the document still needs to be fully formatted, otherwise Expat (like other XML-compliant parsers) will stop with an error message. As a parser that does not check validity, Exapt's speed and lightweight make it well suited for Internet applications. Compiling Expat Expat can be compiled into PHP3.0.6 version (or above). Starting from Apache 1.3.9, Expat has been included as part of Apache. On Unix systems, you can compile it into PHP by configuring PHP with the -with-xml option. If you compile PHP as an Apache module, Expat will be included as part of Apache by default. In Windows, you must load the XML dynamic link library. XML Examples: XMLstats One way to learn about Expat's functions is through examples. The example we are going to discuss is using Expat to collect statistics on XML documents. For each element in the document, the following information will be output: The number of times the element is used in the document The amount of character data in the element The parent element of the element The child elements of the element Note: For demonstration purposes, we use PHP to generate a structure to The function that saves the parent element and child elements of the element and prepares it for generating an XML parser instance is xml_parser_create(). This instance will be used for all future functions. This idea is very similar to the connection tag of the MySQL function in PHP. Before parsing the document, event-based parsers usually require you to register a callback function - to be called when a specific event occurs. Expat has no exception events. It defines the following seven possible events: Object XML parsing function description element xml_set_element_handler() Start and end character data of the element () The occurrence of an unresolved external entity processing instruction xml_set_processing_instruction_handler() The occurrence of a processing instruction notation declaration xml_set_notation_decl_handler() The occurrence of a notation declaration default xml_set_default_handler() Other events without a specified handler function All callback functions must use an instance of the parser as Its first parameter (there are others).For the sample script at the end of this article. What you need to note is that it uses both element processing functions and character data processing functions. The element's callback handler function is registered through xml_set_element_handler(). This function takes three parameters: The instance of the parser The name of the callback function that handles the starting element The name of the callback function that handles the ending element The callback function must exist when starting to parse the XML document. They must be defined consistent with the prototypes described in the PHP manual. For example, Expat passes three arguments to the handler function for the start element. In the script example, it is defined as follows: function start_element($parser, $name, $attrs) The first parameter is the parser identifier, the second parameter is the name of the starting element, and the third parameter contains all attributes and values of the element array. Once you start parsing the XML document, Expat will call your start_element() function and pass the parameters whenever it encounters the start element. XML Case Folding option Use the xml_parser_set_option() function to turn off the Case folding option. This option is on by default, causing element names passed to handler functions to be automatically converted to uppercase. But XML is case sensitive (so case is very important for statistical XML documents). For our example, the case folding option must be turned off. Parsing the document After completing all the preparations, now the script can finally parse the XML document: Xml_parse_from_file(), a custom function that opens the file specified in the parameter and parses it in 4kb size , when an error occurs, that is, the format of the XML document is incomplete, false will be returned. You can use the xml_get_error_code() function to get the numeric code of the last error. Pass this numeric code to the xml_error_string() function to get the error text information. Outputs the current line number of XML, making debugging easier. During the parsing process, the callback function is called. Describing the document structure When parsing a document, the question that needs to be addressed with Expat is: how to maintain a basic description of the document structure? As mentioned before, the event-based parser itself does not produce any structural information. However, the tag structure is an important feature of XML. For example, the element sequence means something different than
http://www.bkjia.com/PHPjc/531750.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531750.htmlTechArticlePHP’s XML parsing function First of all, I have to admit that I like computer standards. If everyone followed the industry's standards, the Internet would be a better medium. Use standardized data exchange...
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