XML application _php Tutorial in PHP

Source: Internet
Author: User
Tags cdata mysql functions processing instruction xml example xml parser
Review
XML represents extensible Markup Language (the abbreviation for extensible Markup Language, meaning Extensible Markup Language). XML is a set of rules that define semantic markup that divides a document into parts and identifies the parts. It is also a meta-markup language that defines syntactic languages used to define other, semantic, structured markup languages that are relevant to a particular domain. XML is the hottest technology today. While PHP also has the ability to parse XML documents, let's explore the XML application in PHP.

XML overview
Talking about XML (eXtended Markup Language: Extensible Markup Language), we might as well look at the HTML code first:
<title> XML </title>
<body>
<p> <center> <font color= "Red" >text </font> </center> </p>
<a Href= "www.domain.com" > </a>
</body>
The above code is structurally consistent with XML rules, and XML can be understood to be the structure type of the tree that contains the data:
1, when referring to the same element, use a consistent case, such as <center> </Center> is not in accordance with the provisions of the
2. Any attribute value (e.g. href= "????") To use "" to cause, such as <a href=www.yahoo.com> is not correct
3. All elements must be composed of open and close > Callouts, elements should be shaped like <body> </body> or empty elements , if the end of "/>" less "/" is the wrong code
4. All elements must be nested with each other, just like the loop of a program, and all elements must be nested within the root element, such as the above code, all nested within 5. The element name (i.e., body a p img above) should start with a letter.

How to apply PHP XML parser Expat?
Expat is an XML parser (also known as an XML processor) in the PHP scripting language that enables programs to access the structure and content of an XML document. It is an event-based parser. There are two basic types of XML parsers:
Tree-based parser: Converts an XML document into a tree structure. Such parsers parse the entire article and provide an API to access each element of the resulting tree. Its common standard is DOM (Document object mode).
Event-based parser: treats an XML document as a series of events. When a particular event occurs, the parser invokes the function provided by the developer to process it. The event-based parser has a data-set view of an XML document, which means it concentrates on the data part of the XML document, not its structure. These parsers process the document from start to finish, and will resemble the beginning of the element, the end of the element, the start of the feature data, and so on-the event is reported to the application through the callback (callback) function.
Here is an example of a "Hello-world" XML document:
<greeting>
Hello World
</greeting>
The event-based parser is reported as three events:
Start element: Greeting
CDATA The start of the entry with the value: Hello World
End element: Greeting
The event-based parser does not produce a structure that describes the document, but if you use expat, it can generate a full native tree structure in PHP as necessary. In a CDATA item, the event-based parser does not get information about the parent element greeting. However, it provides a lower level of access, which makes it possible to make better use of resources and faster access. In this way, it is not necessary to put the entire document into memory, and in fact, the entire document can even be larger than the actual memory value.

The example above Hello-world includes the full XML format, but it is not valid because there is no DTD (document type definition) associated with it, and there is no inline DTD. However, expat is a parser that does not check for validity, and therefore ignores any DTD associated with the document. It should be noted that the document still requires a full format, otherwise expat (as with other XML-compliant parsers) will stop with the error message.

  Compiling expat
Expat can be compiled into the PHP3.0.6 version (or above). From Apache1.3.22 onwards, expat has been part of Apache. In UNIX systems, PHP can be configured with the-with-xml option to compile it into PHP.
If PHP is compiled as an Apache module, expat will default as part of Apache. In Windows, you must load the XML dynamic connection library.
XML Example: Xmlstats
The example we are going to discuss is using expat to collect statistical data for XML documents.
For each element in the document, the following information is output:
* The number of times the element was used in the document
* Number of character data in this element
* element's parent element
* Element's child element
Note: To demonstrate, we use PHP to generate a structure to hold the elements ' parent and child elements

What are the functions used to produce an XML parser instance?
The function used to produce an XML parser instance is Xml_parser_create (). The instance will be used for all future functions. This idea is very similar to the connection token for MySQL functions in PHP. Before parsing a document, an event-based parser typically requires that a callback function be registered-called when a particular event occurs. Expat has no exception, it defines the following seven possible events:

Object XML parsing function description
The start and end of the element Xml_set_element_handler () element
Start of character data Xml_set_character_data_handler () character data
External entity Xml_set_external_entity_ref_handler () external entity appears
unresolved external entities xml_set_unparsed_entity_decl_handler () unresolved external entities appear
Processing instruction Xml_set_processing_instruction_handler () the appearance of processing instructions
Notation Statement Xml_set_notation_decl_handler () The appearance of the notation statement
Default Xml_set_default_handler () other events that do not specify a handler function

All callback functions must have an instance of the parser as their first parameter (plus other parameters).
For the example script at the end of this article, it is important to note that it uses both the element handler function and the character data processing function. The callback handler for the element is registered by Xml_set_element_handler ().
This function requires three parameters:
Examples of parsers
The name of the callback function that handles the start element
The name of the callback function that handles the end element
The callback function must be present when parsing the XML document begins. They must be defined as consistent with the prototypes described in the PHP manual.
For example, expat passes three parameters to the handler function for the start element. In the script example, it is defined as follows:
function Start_element ($parser, $name, $attrs)
$parser is the parser flag, $name is the name of the start element, $attrs an array containing all the attributes and values of the element.
Once the XML document is parsed, expat will call the Start_element () function and pass the arguments past when it encounters the start element.

Case folding option for XML
Use the Xml_parser_set_option () function to close the case folding option. This option is turned on by default so that the element name passed to the handler function is automatically converted to uppercase. However, XML is sensitive to capitalization (so the case is very important for statistical XML documents). For our example, the case folding option must be closed.

How do I parse a document?
After all the preparations have been completed, the script can now parse the XML document:
Xml_parse_from_file (), a custom function that opens the file specified in the parameter and resolves it in 4kb size
Xml_parse (), like Xml_parse_from_file (), returns False when an error occurs, that is, if the XML document is not fully formed.
We can use the Xml_get_error_code () function to get the number code of the last error. Pass this numeric 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 to expat needs to be emphasized: how to maintain a basic description of the document structure?
As mentioned earlier, the event-based parser itself does not produce any structural information. The tag structure, however, is an important feature of XML. For example, the element sequence <book> <title> represents a different meaning than <figure> <title>. The title is not related to the name of the map, although they all use the term "title". Therefore, in order to use the event-based parser more effectively for XML processing, you must use your own stack (stacks) or list (lists) to maintain the structure information of the document.
To create a mirror image of the document structure, the script needs to know at least the parent element of the current element. Using the EXAPT API is not possible, it only reports the events of the current element, and does not have any information about the relationship. Therefore, you need to build your own stack structure.
The script paradigm uses the advanced back-out (FILO) stack structure. With an array, the stack will hold 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 removes the topmost element by Array_pop ().
For sequence <book> <title> </title> </book>, the stack is populated as follows:
Start element Book: assigns "book" to the first element of the stack ($stack [0]).
Start element title: assigns "title" to the top of the stack ($stack [1]).
End element Title: Removes the topmost element from the stack ($stack [1]).
End element title: From the stack will be the most

http://www.bkjia.com/PHPjc/486244.html www.bkjia.com true http://www.bkjia.com/PHPjc/486244.html techarticle the summary XML represents extensible Markup Language (extensible Markup Language abbreviation, meaning Extensible Markup Language). XML is a set of rules that define semantic markup that will document ...

  • 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.