_php Tutorials for web site development with PHP and XML

Source: Internet
Author: User
Tags parse error xml parser xpath xsl
first, small order

HTML is easy to learn and generic, the general PHP program is embedded in the HTML language implemented. But with the web becoming more and more widely used, the weakness of HTML is becoming more and more obvious. The advent of XML makes up for these shortcomings, and it provides a common way to handle all the data on the Internet.

   ii. Limitations of HTML

1. The extensibility of HTML is poor. Although as a general application, HTML should be sufficient, but in the processing of mathematical and chemical symbols, HTML has obvious shortcomings, and it can not be extended, so that its development has received a great deal of limitations.

2, link loss can not be automatically corrected. Because the URL address of the Web page often changes, and in changing the URL address must manually modify the information, otherwise you will encounter "404URL address not Found" information, which greatly increased the maintenance of the Web page.

3, data search for a long time. Since HTML is primarily used to control the display of a Web page, the same data can be stored in different formats in different Web pages, so that data searches cannot be quickly found.

4, HTML support for double-byte or multi-country text is not enough. For example, the Chinese information page will appear under different platforms, such as the problem can not be displayed.

Because of these shortcomings, people have studied the Web page making language that can replace HTML. It has been put into use: Extensible Markup Language XML, cascading style sheets (CSS), and Dynamic HTML (DHTML).

   third, the composition of XML

Here is a brief list of some of the main XML technologies:

1. DTD (document type Declaration)

The main function of a DTD is to define the content schema of the XML, limit the data range of the XML tag, and define the data type of the attribute. However, because it is not written in XML, extensibility is poor, and only a limited number of data types are provided, so it has a limited role.

2. XML Schema

The XML schema works like a DTD. But 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 and DTD compare the following advantages:

· The XML schema content model is open and can be arbitrarily expanded, and the DTD cannot parse the expanded content.

· A DTD can only define a content type as a string, whereas an XML schema allows a content type to be defined as Integer, float, Boolean, or many other simple data types.

· XML Schema uses namespaces to associate a particular node in a document with a schema, an XML file can have multiple schemas, and an XML file can have only one DTD.

3, XLink

As a Web language, the ability to link XML is very important. The links and addressing mechanisms of XML include XLink, XPath, and XPointer. XLink provides powerful linking methods to create a one-way or multi-directional complex join relationship between documents, as well as a number of links such as comment links, summary links, extended link sets, and more. XPath is used in XSLT and xpointer to support positioning in XML documents relative to nodes and node sets. XPointer provides the positioning of the internal structure of the content of an XML document, such as a string or a selected paragraph, on an XPath basis. The ability to link XML is greatly enhanced than HTML.

4. CSS and XSL

One of the major features of XML is the separation of content from the format, meaning that the XML document does not contain information about how to display/represent the document. CSS and XSL (XML Style Language) solve the problem of displaying XML documents.

CSS (cascading style sheets) can also be used in HTML and XML. XSL uses the syntax of XML completely, which is much more powerful than CSS.

5. DOM

The Document Object Model (DOM) is a platform-language-agnostic program interface that provides the means to dynamically access and update the content, structure, and style of a document.   The text can be treated as further processing, and the results of the processing are updated to the presentation page. The goal of DOM is to define a standard programming interface for XML and HTML, which consists of the core, HTML, and XML three parts. The core part of the DOM establishes a set of underlying objects that can represent any structured document. HTML and XML provide a high-level interface that can be used as a more convenient document view. The DOM specification consists of objects and methods. Programmers use them to make it easier to access and manipulate specific types of documents.

6, namespaces

Namespaces is a collection of all the names that are distinguished by URLs and that appear in the elements and attributes of an XML file. In XML, users can define their own tags and elements. Therefore, if you merge multiple XML files into one, there is a good chance of a conflict. namespaces solved the problem.

   iv. PHP support for XML

PHP provides powerful support for XML. It uses an XML "parser", and in order to support this parser, it provides a PHP4 XML parsing function. Here are a few of the most commonly used PHP parsing functions.

1. Xml_parse

Boolean xml_parse (int parser, string data, int [isfinal]);

This function is used to parse file data in XML format. The argument parser to parse the code. Parameter data is the parsed data block (chunk). Parameter isfinal can be omitted, if set to true then the system will automatically send the final data section (piece) to the data parameter. Returns a true value if there is no error.

2. Xml_parser_create

int Xml_parser_create (string [encoding]);

This function is used to initialize a new XML parser. Parameter encoding can be omitted, the character set used for XML, the default value is Iso-8859-1, other us-ascii, UTF-8 two kinds. Success returns the parser code for use by other functions, and the failure returns a value of false.

3. Xml_set_element_handler

Boolean xml_set_element_handler (int parser, string startelementhandler, string endelementhandler);

The header of this function configuration element is intended for use by the Xml_parse () function. The argument parser to parse the code. The parameters Startelementhandler and Endelementhandler are the header where the element begins and ends, where Startelementhandler must include parsing code, name, and attributes, and Endelementhandler Parameters include parsing code and name two parameters. Returns a true value if there is no error.

4. Xml_set_character_data_handler

Boolean xml_set_character_data_handler (int parser, string handler);

This function configures the header of the character data. The argument parser to parse the code. The parameter handler consists of two elements, such as parsing code and a data string. Returns a true value if there is no error.

5. Xml_get_error_code

int Xml_get_error_code (int parser);

This function can get the error code when the XML is processed. The argument parser to parse the code. Returns False if parser is wrong, or returns an error code such as Xml_error_binary_entity_ref .... And so on).

6. xml_error_string

string xml_error_string (int code);

This function can get the error code when the XML is processed. The parameter code is a parse error. If no error return value is the literal 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 line number currently being processed by the XML parsing. The argument parser to parse the code. Returns a false value if parser is wrong, and returns the line number if there is no error.

8. Xml_parser_free

Boolean xml_parser_free (int parser);

This function is used to release the memory used by the current XML parsing. The argument parser to parse the code. Returns a true value if there is no error, otherwise returns a value of false.

   v. Analysis of cases

The following is an example of using the PHP5 implementation to read an address book written in XML 1.0 format and display its contents address.xml. See the comments.


//Part one: several PHP helper functions


* Read the XML's *
* content to a string from a file Medium *


http:// www.bkjia.com/PHPjc/508472.html www.bkjia.com true http:// www.bkjia.com/PHPjc/508472.html techarticle A, small order HTML easy to learn and universal, the general PHP program is embedded in the HTML language implemented. But with the web becoming more and more widely used, the weakness of HTML is becoming more and more obvious. ...

  • Related Article

    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.