I. Small order
HTML is easy to learn and common. Common PHP programs are embedded in the HTML language. However, as WEB applications become more and more widely used, the weakness of HTML becomes more and more obvious. The emergence of XML makes up for these shortcomings. It provides a common method to process all data on the Internet.
II. Limitations of HTML
1. Poor HTML scalability. As a general application, HTML should be enough, but HTML has obvious disadvantages when dealing with symbols such as mathematics and chemistry, and it cannot be expanded, in this way, its development has been greatly restricted.
2. The link cannot be automatically corrected after it is lost. Because the URL address on the web page is often changed, you must manually modify the information when changing the URL address. Otherwise, the "404URL address not found" message is displayed, this greatly increases the web page maintenance workload.
3. Long data search time. HTML is mainly used to control the display of web pages. As a result, the same data has different storage formats on different web pages. In this way, you cannot quickly find the desired information during data search.
4. HTML does not support double-byte or multi-country text. For example, the Chinese information page cannot be displayed on different platforms.
Due to these shortcomings, people have studied the Web page production language that can replace HTML. Some of them have been put into use: Extensible Markup Language XML, Cascading Style Sheets (CSS), Dynamic HTML (DHTML), and so on.
III. XML composition
Here we will briefly list several major XML technologies:
1. DTD (document type declaration)
The main function of DTD is to define the content mode of XML, limit the data range marked by XML, and define the data type of attributes. However, because it is not written in XML, it has poor scalability and only provides several limited data types, so its role is limited.
2. XML Schema
The role of XML Schema is similar to that of DTD. However, the difference is that the Schema file describes the specific types of elements and attributes in the XML file that references it. In addition, because it is written in XML, Schema has the following advantages over DTD:
Function start_element ($ parser, $ name, $ attrs)
{
// Variable $ depth stores the Mark depth
Global $ depth;
// Variable $ spacer stores the arrow symbol before marking
$ Spacer = "";
For ($ I = 1; $ I <$ depth [$ parser]; $ I)
{
$ Spacer. = "-> ";
}
// Set the color when the tag is displayed.
If ($ depth [$ parser] = 0)
{
$ Font_color = "red ";
}
Else
If ($ depth [$ parser] = 1)
{
$ Font_color = "green ";
}
Else
{
$ Font_color = "blue ";
}
// Set the font when the tag is displayed
$ Font_size = 5-$ depth [$ parser];
If ($ font_size <2)
{
$ Font_size = 2;
}
// Display tags
Echo "";
Echo $ spacer. $ depth [$ parser];
If ($ depth [$ parser] <> 0)
{
Echo ",";
}
Echo $ name ."
";
Echo "";
$ Depth [$ parser];
// A red horizontal line is displayed if it is a primary flag.
If ($ depth [$ parser] = 2)
{
Echo"
--------------------------------------------------------------------------------
";
}
}
/*********************************
* When PHP encounters an XML End mark *
* Call. Its function is to change the current level *
* Count and mark down the display of the horizontal line *
IV. XML support for PHP
PHP provides powerful support for XML. It uses an XML "parser" and provides 20 (PHP4) XML parsing functions to support this parser. Below are some of the most common PHP parsing functions.
1. xml_parse
Boolean xml_parse (int parser, string data, int [isFinal]);
This function is used to parse files in XML format. The parser parameter is the parsing code. The parameter data is the parsed data block (chunk ). The isFinal parameter can be omitted. If it is set to true, the system automatically sends the final data part (piece) to the data parameter. If no error exists, true is returned.
2. xml_parser_create
Int xml_parser_create (string [encoding]);
This function is used to initialize a new XML parser. The encoding parameter can be omitted, is the character set used by XML, the default value is ISO-8859-1, there are other US-ASCII, UTF-8 two. If the call succeeds, the parser code is returned for other functions. If the call fails, false is returned.
3. xml_set_element_handler
Boolean xml_set_element_handler (int parser, string startElementHandler, string endElementHandler );
This function configures the element header for the xml_parse () function. The parser parameter is the parsing code. The startElementHandler and endElementHandler parameters are the element start and end headers respectively. The startElementHandler must include the parsing code, name, and attribute. The endElementHandler parameter includes the parsing code and name parameters. If no error exists, true is returned.
4. xml_set_character_data_handler
Boolean xml_set_character_data_handler (int parser, string handler );
This function configures the character data header. The parser parameter is the parsing code. The handler parameter contains two elements: parsing code and data string. If no error exists, true is returned.
5. xml_get_error_code
Int xml_get_error_code (int parser );
This function can obtain the error code for XML processing. The parser parameter is the parsing code. If a parser error occurs, false is returned. Otherwise, error code (such as XML_ERROR_BINARY_ENTITY_REF...) is returned ).
6. xml_error_string
String xml_error_string (int code );
This function can obtain the error code for XML processing. The parameter code is the parsing error code. If no error is returned, the return value is a text description string of the code.
7. xml_get_current_line_number
Int xml_get_current_line_number (int parser );
This function is used to obtain the row number currently being processed by XML parsing. The parser parameter is the parsing code. If a parser error occurs, false is returned. If no error exists, the row number is returned.
8. xml_parser_free
Boolean xml_parser_free (int parser );
This function is used to release the memory currently used for XML parsing. The parser parameter is the parsing code. If no error exists, true is returned. Otherwise, false is returned.
V. Case analysis
The following is an example of using PHP5 to read an address book address. XML in xml 1.0 format and display its content. For more information, see comments.
<? Php
//
// Part 1: several PHP Helper functions
//
/**********************************
* Read XML from a file *
* Content to the string *
*********************************/
Function read_file ($ filename)
{