XML application in PHP (2)
Source: How to parse the document? After completing all preparations, the script is now able to resolve the XML document: Analysis XML_PARSE (), like XML_PARSE_FROM_FILE (), when an error occurs, the format of the XML document is not complete, will return false. We can use the XML_GET_ERROR_CODE () function to get the last error number code. Pass this numeric code to the XML_ERROR_STRING () function to get the wrong text information. Output XML's current number of rows, making debugging easier. When parsing the document, it is necessary to emphasize the problem for Exppat: How to maintain the basic description of the document structure? As mentioned earlier, an event-based parser itself does not produce any structural information. However, the tag (TAG) structure is an important feature of XML. For example, elemental sequence
We also want to let the parent elements know the current element is its child elements. Therefore, the name of the current element will be added to the $ childs array of the parent element. Finally, the current element should remember who is its parent element. Therefore, the parent element is added to the current element $ PARENTS array project. Display the remaining code remaining in the $ Elements array and its statistical results. This is the simplest nesting cycle, although the correct result is output, but the code is neither concise and no special skills, it is just a loop you might use in him every day. The script example is designed to call the command line of the PHP's CGI mode. Therefore, the format of the statistical result is the text format. If you want to use the script to the internet, you need to modify the output function to generate an HTML format. How to prepare a mini search engine instance with PHP & XML? Let us first familiarize with the XML used in our program (saved as XYZ.xml). XML Version = / "1.0 /" Encoding = / "GB2312 /"?>
For example, we have to add a category or web page, just edit the text file, plus a blessing EB> ??? web> or ???? sub> is OK, and if If you want to move a category to another, we just need to copy this part of the SUB. The following is the simplest use of PHP display XML examples. The following program is to parse XML and output to the browser according to the tree structure and display the total number of elements per layer. php $ file = /"demo.xml/";// xml file function XML_PARSE_FROM_FILE ($ PARSER, $ file) {// parsing XML file function} Function Start_element ($ Parser, $ Name, $ Attrs) {/ / I encountered the open element marker, such as to execute this paragraph} Function Stop_Element ($ PARSER, $ NAME) {// encountered this paragraph, such as body> to execute this paragraph} Function Data ($ PARSER, $ DATA) {...} Function showcount () {// The total number of elements of each layer} Global $ Level, $ LevelCount, $ MAXLEVEL; $ level = -1; $ PARSER = XML_Parser_create (); // generated parser instance xml_set_element_handler ($ parser, / "start_element /", / "stop_element /"); // set handler xml_set_character_data_handler ($ parser, / "data /"); xml_parser_set_option ($ parser, XML_OPTION_CASE_FOLDING, 0 $ RET = XML_PARSE_FROM_FILE ($ PARSER, $ file); // Resolution file if (! $ RET) {Die (sprintf (/ "xml error:% s at line% d /", XML_ERROR_STRING (XML_GET_ERROR_CODE ($ PARSER) ), XML_GET_CURRENT_LINE_NUMBER ($ PARSER)));} XML_Parser_Free ($ PARSER); // Release the parser showcount ();> On the basis of the above program, a piece of sub-tree can be displayed, we can in accordance with the number of elements And his order in this layer, you will be able to position him, for example: LLINKS (0,1) ---- Web (1, 1) - --Sub (1, 2) | ---- Web (2,1) ---- SUB (2, 2) | | ---- Web (3, 1) | ---- SUB (3, 2) : It is the foundation of our search engine. Because you want to display a subcategory (such as program design -> php->) to use him.