XML application in PHP (ii)

Source: Internet
Author: User
Tags format array command line functions variables php and variable trim
How does XML parse a document?
Now that the script can finally parse the XML document after all the preparations have been done:

xml_parse_from_file (), a custom function that opens the file specified in the parameter and resolves it in 4kb size
Xml_parse (), like the Xml_parse_from_file (), returns False when an error occurs that the XML document is not in full format.

We can use the Xml_get_error_code () function to get the last error of the numeric code. Pass this digital code to the Xml_error_string () function to get the wrong text message. Outputs the current number of rows in the XML, making debugging easier.

When parsing a document, the question for expat needs to be emphasized: how do you keep a basic description of the document structure?

As mentioned earlier, 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 <book><title> the meaning of the expression is different from <figure><title>. The title and the name of the picture are not related, although they all use the term "title". Therefore, in order to use an event-based parser to process XML more effectively, you must use your own stack (stacks) or list (lists) to maintain the document's structural information.

To produce a mirror image of the document structure, the script needs to know at least the parent element of the current element. The EXAPT API is not implemented, and it reports only the events of the current element, without any information about the relationship. Therefore, you need to build your own stack structure.

The script example uses the advanced back-out (FILO) stack structure. With an array, the stack saves all the start elements. For the start element handler function, the current element will be pushed to the top of the stack by the Array_push () function. Accordingly, the end element handler function removes the topmost element by Array_pop ().

For sequence <book><title></title></book&gt, the stack is populated as follows:

Start element Book: assigns "book" to the first element of the stack ($stack [0]).
Start element title: Assign "title" to the top of the stack ($stack [1]).
The end element title: Removes the topmost element from the stack ($stack [1]).
The end element title: Removes the topmost element from the stack ($stack [0]).

PHP3.0 The example by manually controlling the nesting of elements with a $depth variable, which makes the script look more complex. PHP4.0 makes the script look more concise by Array_pop () and Array_push () two functions.

How do I collect the element information in an XML document?

To gather information about each element, the script needs to remember the events for each element. Save all the different elements in the document by using a global array variable $elements. An array of items is an instance of an element class, with 4 properties (variables of the class)

$count-Number of times the element was found in the document
$chars-Number of bytes of character event in element
$parents-Parent Element
$childs-child elements

Note: One feature of PHP is that you can traverse the entire class structure through the while (list () = each ()) loop as you traverse the entire corresponding array. All class variables (when you use PHP3.0 and method names) are output as strings.

When an element is found, we need to increment its corresponding register to track how many times it appears in the document. Add one to the count element in the corresponding $elements item.

We also want the parent element to know that the current element is its child element. Therefore, the name of the current element will be added to the project of the parent element's $childs array. Finally, the current element should remember who is its parent element. Therefore, the parent element is added to the project of the current element $parents array.

Show statistic Information
The rest of the code loops through the $elements array and its child arrays to show its statistical results. This is the simplest nested loop, although the output is the correct result, but the code is neither concise nor any particular technique, it is just a loop that you may use to complete the work every day.

The scripting example is designed to be invoked through the command line in PHP's CGI mode. Therefore, the format of the statistical results output is text format. If you want to apply the script to the Internet, you need to modify the output function to produce HTML format.

How to use Php&xml to compile a mini search engine example?
Let's first familiarize ourselves with the XML (saved as Xyz.xml) that is used in our program.

<?xml version= "1.0" encoding= "gb2312"?>
<links> using PHP and XML technology to build a search engine
<web memo= "Memo1" url= "" >name1</web>
<sub> Computer Network
<web memo= "Nemo2" >name2</web>
<sub> programming
<web memo= "Memo3" >name3</web>
<sub>php
<web url= "http://www.phpbuilder.com/memo=" [English]php development resources. ">
Www.phpbuilder.com</web>
<web url= "http://www.fokus.gmd.de" memo= "[English]php Development Manual. ">
PHP manual</web>
</sub>
</sub>
</sub>
</links>

Its structure is quite simple, the root element is links,sub represents a category, the Web is a Web site information, which contains attributes, URLs for the connection of the Web site, memo for memo information, <web>?? </web>, <sub>?? The data contained in </sub> is the name of the category and the site, which is consistent with the above rules.

Now let's answer the question raised above: Why use XML to compile search engines?
The first reason is that sometimes we may not be able to use the database (MySQL or other) for a variety of reasons;
Second, for small data search engine, its data volume is very small, if the database to do, the efficiency may not be how high;

The most important point is that the search engine is fairly simple to maintain and does not have to write cumbersome database maintenance programs. For example, we want to add a category or Web page, just edit the text of the file, plus a blessing 紈 eb>??? </web> or <sub>???? </sub>, and if you want to move a category to another place, we just have to copy this part of the sub to the past.

Below is one of the simplest examples of XML that is shown in PHP.

The following procedure is to parse the XML and output it 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) {//parse XML file functions}
function Start_element ($parser, $name, $attrs) {//encountered an open element tag such as <a href= "link" > Execute this section}
function Stop_element ($parser, $name) {//encountered open element tag such as </body> execute this section}
function data ($parser, $data) {...}
function Showcount () {//Show the total number of elements per layer}

Global $level, $levelcount, $maxlevel;
$level =-1;
$parser = Xml_parser_create ();//Generate Parser instance
Xml_set_element_handler ($parser, "start_element", "stop_element"); Setting up a handler function
Xml_set_character_data_handler ($parser, "data");
Xml_parser_set_option ($parser, xml_option_case_folding, 0);
$ret = Xml_parse_from_file ($parser, $file); Parsing files
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 parser
Showcount ();
?>

On the basis of the above program, we can show a section of the subtree, and we will locate it according to the layer of the element and the number of the layer he is on.

For example:

links (0,1)
+----Web (1,1)
+----Sub (1,2)
| +----web (2,1)
| +----SUB (2,2)
| | +----web (3,1)
| | +----SUB (3,2)
:
:
:
The following code is the basis of our search engine. Because, to display a subcategory (such as programming->php->) information will use him.

<?php
......
function Start_element ($parser, $name, $attrs) {
Global $level, $levelcount, $maxlevel, $hide, $lev, $num, $PHP _self;
$level + 1;
if ($level > $maxlevel)
$maxlevel = $level;
$levelcount [$level]+=1;

if ($hide) {//judge whether within the scope of the subtree, $hide ==false for the
if ($level = = $lev && $levelcount [$level]== $num)
$hide =false;
}else{
if ($level <= $lev) $hide =true;
}

if (! $hide) {
...//output
}
}
function data ($parser, $data) {
Global $level, $hide;
if (! $hide) {
if (Trim ($data)!= "") {echo trim ($data);}
}
}
......
Global $hide, $lev, $num, $PHP _self;
$level =-1;
$hide = TRUE;
echo "<p><a href= $PHP _self>root</a></p>";
if ($lev = = "") {
$lev =0; $num = 1;
}
......
?>

mini search engine in the end how to do it?

made a number of bedding, let's take a look at our search engine's main document.

The first paragraph is imitation sina,yahoo query by category
The second paragraph displays the contents of the Search Query section (traversing the entire tree).

xml3.php

Keyword matching using the Eregi function, we assume that the input text will not lead to errors.

< finished full >


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.