Parse XML using PHP toolkit expat

Source: Internet
Author: User
Tags xml parser
Now everyone advocates that xml is the best friend of web developers. with the help of xml, the latter can easily format and display data from almost any data source. However, for dynamic content, well-formatted data is far from ideal. Most web developers will tell you how to do this without dynamic content on the network today! The problem is: Now everyone advocates that xml is the best friend of web developers. with the help of xml, the latter can easily format and display data from almost any data source. However, for dynamic content, well-formatted data is far from ideal. Most web developers will tell you how to do this without dynamic content on the network today! The question is: "How can I create dynamic content using xml ?"

The answer is to use a dynamic content processing language to parse xml, such as php or perl. Theoretically, such programming languages can exploit xml for various purposes. It is nothing more than a toolkit that can parse xml. James clark provides an expat toolkit. The expat xml toolkit parses xml in C language, making php and xml easy to dance.

Php is an excellent scripting language designed for the web. Xml indicates the standard of web content. What a beautiful combination of the two!

Below I will show you a simple example, through which you can describe how to use php to parse xml documents into html. Then I will introduce some other php xml concepts. Parsing xml with php is simple, and the operation is intuitive, but the details need to be explained. Once you have mastered the essentials of the application, you will be amazed at how you don't think of bringing them together.

Php uses the xml toolkit such as expat to parse xml in C language. The function set of this toolkit is the same as the function set used for perl xml parsing. In addition, this toolkit is also an event-driven parser. This means that expat treats every xml tag or a new line of code as the starting point of an event, and the event is the trigger of a function. The installation of expat is very simple. if you are using an apache web server, you can find the installation and download guide on the php xml reference page.

The basic task of parsing xml with php is as follows: First, create an instance of the xml parser. Then, define the function for processing the trigger event, such as the start or end tag. Then, define the actual meaning of the data processing program. Finally, open the xml file, read the file data, and parse the data. Then close the file and release the xml parser.

You see, as I said, this operation process is nothing special. However, before discussing specific examples, let's take a look at the following warnings:

Expat does not validate xml. This means that as long as the xml file is in the correct format-all elements are nested properly, the start and close labels are correct-it will be parsed. Expat can be used regardless of whether xml complies with the standards or definitions referenced in the xml file header.
Expat converts all xml tags to uppercase letters. If your script is mixed with uppercase and lowercase letters in the tag name and other content, be careful.
Php is compiled when magic quotes settings are enabled, so complex xml files won't be correctly parsed. If magic quotes is not set by default, you should leave it blank.
Now let's take a look at the relevant examples!

Basic example

To simplify complicated things, I have omitted error checking and other unnecessary things in the example. of course, you can do whatever you want in your own code. I suppose you are familiar with php and its syntax long ago, and I will explain the xml function. First, I will explain the meaning of the script program, and then define the user-defined functions. In fact, these functions are located before referencing their code. Related attachments: the complete code of the script is shown in program List a. The xml document to be parsed by the script is related to the attachment: Program List B. The output result after processing is shown in Table.

Xml articles

"Remedial xml for programmers: basic syntax" in this first installment in a three-part series, I'll introduce you to xml and its basic syntax.

"Remedial xml: enforcing document formats with dtds" to enforce structure requirements for an xml document, you have to turn to one of xml's attendant technologies, data type definition (dtd ).

"Remedial xml: using xml schema" in this article, we'll briefly touch on the latest comings of dtds and discuss the basics of a newer, more powerful standard: xml schemas.

"Remedial xml: say hello to dom" now it's time to put on your programmer's hat and get acquainted with document object model (dom ), which provides easy access to xml documents via a tree-like set of objects.

"Remedial xml: learning to play sax" in this release th installment in our remedial xml series, I'll introduce you to the sax api and provide some links to sax implementations in several releases ages.

Table a php parses xml output results

First, I created an instance of the xml parser:
$ Parser = xml_parser_create ();

Next, I defined the operations when the parser encountered the start and end labels. Note that "startelement" and "endelement" are user-defined functions. of course, you can give them other names as you like, but these names are a standard requirement.
Xml_set_element_handler ($ parser, "startelement", "endelement ");

Then I defined data operations. The "characterdata" here is also a user-defined function, and the name is also habitual.
Xml_set_character_data_handler ($ parser, "characterdata ");

Now open the file to read data. You can write the error handling code here. I have omitted the error handling code in this example. Do not forget to define $ xml_file at the beginning of the script.
$ Filehandler = fopen ($ xml_file, "r ");

I started to read the file content, read 4 K bytes at a time and put it in the variable "$ data" until the end of the file. I use xml_parse to parse the data segments read.
While ($ data = fread ($ filehandler, 4096 )){
Xml_parse ($ parser, $ data, feof ($ filehandler ));

Finally, clear, close the file, and release the parser.
Fclose ($ filehandler );
Xml_parser_free ($ parser );

The above are all the xml functions used in the script. next I will explain in detail the three user-defined functions used in the script. they are "startelement", "endelement", and "characterdata ".

As long as xml_parse encounters In this way, the startelement function is called by the xml parser. In our example, the parser is $ parser. This function is a required function. It has three parameters automatically passed to it: xml parser instance, Capital element name, such as url and attribute array of this element. In the preceding example, the element in the xml file has no attribute set, so the array parameter is null, but this parameter must exist.

In this example, I decided to display my xml data in an html table. As shown above, I did not write the error handling code for the sake of simplicity. Here I am confused, because I know the order in which labels appear in xml files. Otherwise, I can use the "startelement", "characterdata", and "endelement" functions to define the array, and then use a separate function to display the result.

Function startelement ($ parser_instance, $ element_name, $ attrs ){
Switch ($ element_name ){
Case "url": echo"Break;
Case "summary": echo"";

After processing element tags, xml_parse calls the "characterdata" function once xml data is encountered. This function is also automatically called by the parser. this function requires two parameters: the parser instance and the string containing data.
Function characterdata ($ parser_instance, $ xml_data ){
Echo $ xml_data;

Finally, xml_parse encounters an end tag and runs the "endelement" function. this function has two parameters: the parser instance and element name.
Function endelement ($ parser_instance, $ element_name ){
Switch ($ element_name ){
Case "url": echo ""> ";
Case "title": echo"";
Case "summary": echo"";

The simple training of parsing xml with php is over now. next we will add some new jobs.

Add function

There are other functions related to xml parsing in php. The documentation provides a complete description of these functions. I have mentioned some here, and you may soon use these functions:

Xml_set_default_handler ()-The function works in a similar way as the xml_set_character_data_handler () function, but it captures everything defined. This function is often used to control the processing of data in document type declarations.
Xml_parser_set_option ()-you can use this function to disable uppercase letters or select other replacement character sequence sets.
Xml_parse_into_struct ()-This function is used to skip the call of the "startelement", "characterdata", and "endelement" functions, and directly put the data into the array.
Xml_error_string ()-This function is used to obtain text information from xml_parser () errors.
Xml_get_error_code ()-you can use this function to obtain the preceding error string. The usage of the last two functions is as follows: if (! Xml_parse ($ parser, $ data, feof ($ filehandler) {die (xml_error_string (xml_get_error_code ($ parser ));)
If you think you are ready for use, I suggest you carefully read the xml external entity example on the php manual page. These examples present other concepts not mentioned in this article and some techniques for dealing with file errors.

This article demonstrates the close integration of php and xml. The web-centric nature of the two technologies forces them to unite to become the ideal solution needed for dynamic content.

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