Join hands with PHP and XML for website programming _ PHP Tutorial

Source: Internet
Author: User
Tags xml parser
Use PHP and XML for website programming. 1. 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. 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 changes frequently, you must manually modify the information when changing the URL address. Otherwise, the information "404URL address Not Found" 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:

· The XML Schema content model is open and can be expanded at will, while the DTD cannot parse the expanded content.

· DTD can only define the content type as a string, while XML Schema can define the content type as integer, floating point, Boolean, or many other simple data types.

· XML Schema uses Namespaces to associate special nodes in the document with the Schema. an XML file can have multiple corresponding schemas, and an XML file can have only one DTD.

3. XLink

As a Web language, the link capability of XML is very important. XML links and addressing mechanisms include XLink, XPath, and XPointer. XLink provides powerful link methods to establish one-way or multi-direction complex links between documents, as well as annotation links, summary links, extended link sets, and other link functions. XPath is used in XSLT and XPointer. it supports locating relative nodes and node sets in XML documents. Based on XPath, XPointer provides positioning for the internal structure (such as a string or a selected paragraph) of the content of the XML document. The link capability of XML is greatly enhanced than that of HTML.

4. CSS and XSL

A major feature of XML is the separation of content and format. that is to say, XML documents do not contain information about how to display/represent documents. CSS and XSL (XML Style Language) solve the XML document display problem.

CSS (stacked style sheet) can also be used in HTML and XML. XSL fully uses the XML syntax, which is much more powerful than CSS.

5. DOM
The Document Object Model (DOM) is a platform-and language-independent program interface that provides a means to dynamically access and update the content, structure, and style of a document. The text can be further processed and the processing result is updated to the presentation page.


The purpose of DOM is to define a standard programming interface for XML and HTML, which includes three parts: core, HTML, and XML. The core part of DOM establishes a set of underlying object sets, which can represent any structured documents. HTML and XML provide high-level interfaces for convenient document views. DOM norms consist of objects and methods. Programmers can use them to access and operate specific types of documents more easily.

6. Namespaces

Namespaces is a set of all the names in the elements and attributes of the XML file that are differentiated by URLs. In XML, you can define tags and elements by yourself. Therefore, if you merge multiple XML files into one, conflicts may occur. Namespaces solves this problem.

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.

<?
//
// Part 1: several PHP Helper functions
//

/*********************************

* Read XML from a file *
* Content to the string *

*********************************/

Function read_file ($ filename)
{
// Read the file
$ Lines = file ($ filename );
// Variable $ contents is the variable that stores the file content
$ Contents = "";
While (list ($ key, $ value) = each ($ lines ))
{
$ Contents. = $ value;
}
Return $ contents;
}

/*********************************
* When PHP encounters an XML start mark *
* The call function is based on a certain level *
* Display the XML mark *
*********************************/

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 "<font size =". $ font_size. "color =". $ font_color. "> ";
Echo $ spacer. $ depth [$ parser];
If ($ depth [$ parser] <> 0)
{
Echo ",";
}
Echo $ name. "<BR> ";
Echo "</font> ";
$ 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 *
*********************************/

Function stop_element ($ parser, $ name)
{
// Variable $ depth stores the Mark depth
Global $ depth;
$ Depth [$ parser] --;
// A red horizontal line is displayed if it is a primary flag.
If ($ depth [$ parser] = 2)
{
Echo "}
}

/*********************************
* When PHP encounters an XML tag *
* The call function is based on a certain level *
* Display the marked content *
*********************************

Function char_data ($ parser, $ data)
{
// Variable $ depth stores the Mark depth
Global $ depth;
// Display the marked content
$ Data = trim ($ data );
If (strlen ($ data ))
{
For ($ I = 1; $ I <$ depth [$ parser] + 6; $ I ++)
Echo "";
Echo "<B> $ data </B> <br> ";
}
}

//
// Part 2: PHP file execution start
//
// Name of the XML file to be parsed

$ File = "address. xml ";
// Read the file
$ Data = read_file ($ file );

// The instance that generates the parser
$ Parser = xml_parser_create ();
// Set the processing function
Xml_set_element_handler ($ parser, "start_element", "stop_element ");
Xml_set_character_data_handler ($ parser, "char_data ");
// Parse the file
If (! Xml_parse ($ parser, $ data, 1 ))
{
// Error
Die (sPRintf ("XML error: % s at line % d ",
Xml_error_string (xml_get_error_code ($ parser )),
Xml_get_current_line_number ($ parser )));
}

// Release the parser
Xml_parser_free ($ parser );
?>

HTML is easy to learn and common. generally, php programs are embedded in the HTML language. However, as the WEB becomes more and more widely used, the weakness of HTML becomes more and more obvious ....

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.