How to use the PHP toolkit expat to parse XML_PHP tutorial

Source: Internet
Author: User
Use the PHP toolkit expat to parse XML. 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, 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 ));
  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next page

Bytes. However, for dynamic content...

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.