About PHP toolkit expat parsing XML secrets _ PHP Tutorial

Source: Internet
Author: User
Tags xml parser
The PHP toolkit expat resolves XML secrets. With the development of PHP, the function is also powerful. This article mainly introduces the role of the PHP toolkit, hoping to help you. Now everyone advocates that XML is the best friend of Web developers. with PHP developing, the function is also powerful. This article mainly introduces the role of PHP toolkit and hopes to help you. 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 we can illustrate how to use the PHP toolkit to parse the XML document into HTML. Then I will introduce some other PHP XML concepts. Parsing XML with the PHP toolkit is very 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.

Overview

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

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.

 
 
  1. XML Articles
  2. "Remedial XML for programmers: Basic syntax"
  3. In this first installment in a three-part series,
  4. I'll introduce you to XML and its basic syntax.
  5. "Remedial XML: Enforcing document formats with DTDs"
  6. To enforce structure requirements for an XML document, you have to turn to one of XML's attendant technologies, data type definition (DTD).
  7. "Remedial XML: Using XML Schema"
  8. In this article, we'll briefly touch on the shortcomings of DTDs and discuss the basics of a newer, more powerful standard: XML Schemas.
  9. "Remedial XML: Say hello to DOM"
  10. Now it's time to put on your programmer's hat and get acquainted with Document Object Model (DOM),
  11. which provides easy access to XML documents via a tree-like set of objects.
  12. "Remedial XML: Learning to play SAX"
  13. In this fifth installment in our Remedial XML series, I'll introduce you to the SAX API and provide some links to
    SAX implementations in several languages.



Bytes. Now everyone advocates that XML is the best friend of Web developers...

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.