1. you may be familiar with XML. I don't want to explain why XML is a thing here. I just want to introduce some of the concepts used in this article, if you have already used XML, even for beginners. You can skip this chapter. When talking about XML, I 'd like to give you a piece of html code that we are familiar. (1) html (2) titlepage 1. understand XML
You may still be unfamiliar with XML. I don't want to explain why XML is a thing here. I just want to introduce some of the concepts used in this article. if you have already used XML, for beginners. You can skip this chapter.
When talking about XML, I 'd like to give you a piece of html code that we are familiar.
(1)
(2) Page title
(3)
(4)
TEXT
(5)
(6)
(7)
The above code is structured to comply with XML rules.
He meets the following characteristics:
1. use the same case when referencing the same element, as shown in figure Is not compliant with the regulations
2. any attribute value (such as href = "???? ") It is incorrect to use" "to expand it.
3. all elements must be opened <和关闭> Annotation composition, the element should be like, Or empty element
Pay attention to the ending/> missing/is the error code.
4. all elements must be nested with each other, just like a program loop. Moreover, all elements must be nested in the root element. all the content of the code above is nested in.
5. the element name (that is, the above body a p img, etc.) should start with a letter. In fact, it is best to be an English word. pay attention to the case.
XML is not so annoying. you can understand it as a tree structure type that contains data.
Let's get familiar with the XML used in our program.
Network mania-your search engine is built using PHP and XML technologies
Name1
Computer network
Name2
Programming language
Name3
PHP
Www.phpbuilder.com
PHP Manual
In fact, its structure is quite simple, the root element is links, sub represents a category, the web is the information of a website, which contains attributes, the url represents the link of the website, memo is the remarks, ?? ,?? The data contained in the element is the category and website name here. Please note that it complies with my above rules.
Add (No error occurs.) save it as xyz. xml and open it in IE5 or a later browser.
His tree structure is at a glance.
Why should we use the mini search engine. The first reason is that I still cannot use mysql on osuo. Secondly, for search engines with small data volumes, the data volume is small. if I use a database, the efficiency may not be high. The most important thing is that it is quite simple to maintain, reduces manpower, and does not need to write tedious database maintenance programs. for example, we need to add a category or webpage, you only need to edit the text file and add ??? Or ???? And if you want to move a category to another place, you just need to move the sub, ctrl-x, and ctrl-v in this part (tree structure ).
In fact, I have only used the XML feature. I will give you more in-depth articles in the future.
II. how PHP parses XML
Note: the content in this chapter is based on the NetEase virtual community (I am too lazy to think about it) and modified.
Two basic types of XML parser:
Tree-based parser: converts an XML document into a tree structure. This type of parser analyzes the entire article and provides an API to access each element of the generated tree. Its general standard is DOM (document object mode ). If Javascript is used, XMLDOM may be used.
Event-based parser: treats XML documents as a series of events. When a special event occurs, the parser calls the functions provided by the developer for processing.
The event-based parser has a centralized data view of the XML document, that is, it is concentrated in the data part of the XML document, rather than its structure. These parsers process documents from start to end, and report events similar to-element start, Element end, feature data start, and so on through the callback function.
To the application. The following is an XML document example of "Hello-World:
Hello World
The event-based parser reports three events:
Start element: greeting
Start of the CDATA entry; value: Hello World
End element: greeting
Unlike the tree-based parser, the event-based parser does not generate the structure of the description document. In the CDATA item, the event-based parser does not give you information about the parent element greeting.
However, it provides a more underlying access, which enables better resource utilization and faster access. In this way, there is no need to put the entire document into the memory. In fact, the entire document can be larger than the actual memory value.
Preparation
The function used to generate 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 mark of MySQL functions in PHP. Before parsing a document, the event-based parser usually requires you to register a callback function-called when a specific event occurs. Expat has no exception event. it defines the following seven possible events:
Object XML parsing function description
Start and end of element xml_set_element_handler ()
Character data xml_set_character_data_handler () start of character data
External entity xml_set_external_entity_ref_handler () external entity appears
External entity xml_set_unparsed_entity_decl_handler () not resolved
Appears
Processing Command xml_set_processing_instruction_handler () processing command appears
The emergence of the xml_set_notation_decl_handler () method declaration
By default, xml_set_default_handler () is used for events that do not specify a processing function.
All callback functions must take the parser instance as its first parameter (and other parameters ).
For more details, see the description of PHP.
Element Structure)
The following example is taken from the PHP manual example,
He is the basic structure of our search engine, but I will not comment it out, because we will introduce it in the next chapter.
$ File = "data. xml ";
$ Depth = array ();
Function startElement ($ parser, $ name, $ attrs)
{
Global $ depth;
For ($ I = 0; $ I <$ depth [$ parser]; $ I ++ ){
Print "";
}
Print "$ name
";
$ Depth [$ parser] ++;
}
Function endElement ($ parser, $ name, $ attrs)
{
Global $ depth;
$ Depth [$ parser] --;
}
$ Xml_parser = xml_parser_create ();
Xml_set_element_handler ($ xml_parser, "startElement", "endElement ");
If (! ($ Fp = fopen ($ file, "r "))){
Die ("cocould not open XML input ");
}
While ($ data = fread ($ fp, 4096 )){
If (! Xml_parse ($ xml_parser, $ data, feof ($ fp ))){
Die (sprintf ("XML error: % s at line % d ",
Xml_error_string (xml_get_error_code ($ xml_parser )),
Xml_get_current_line_number ($ xml_parser )));
}
}
Xml_parser_free ($ xml_parser );
?>