How to parse xml_php tutorials with 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, which can easily format and display data from almost any data source. However, for dynamic content, well-formed data is far from ideal. Most Web developers will tell you that there is no dynamic content on today's network! The question is: "How exactly should you create dynamic content with XML?" ”
The answer is to parse the XML with a dynamic content processing language, such as PHP or Perl, which, in theory, can use XML for a variety of purposes. Nothing more than a toolkit that can parse XML. James Clark offers a toolkit called expat. Expat XML tool pack C language parsing XML, make PHP and XML easy to dance together.
PHP is a great scripting language designed for the web. XML is the standard for representing Web content. How beautiful it is to join hands!
I'll show you a simple example of how to parse an XML document into HTML using PHP in this example. Then I'll introduce some other XML concepts for PHP. Parsing XML in PHP is simple and intuitive to use, but requires some explanation of the details. Once you've really mastered the essentials of your application, you'll be amazed at how you never thought you'd get them together.
Overview
PHP uses the XML Toolkit expat, which parses XML through the C language. The set of functions for this toolkit is the same as the set of functions used by Perl XML parsing, and this toolkit is also an event-driven parser. That is, expat treats each XML tag or a new line of code as the beginning of the event, and the event is the function's trigger. Expat installation is very simple, if you are using the Apache Web server, then you can find the installation and download guide on the PHP XML reference page.
The basic task of parsing XML with PHP is this: first, create an instance of the XML parser. Next, define the function that handles the triggering event, such as starting or ending the tag. Subsequently, the data handlers that define the actual meaning are defined. Finally, open the XML file, read the file data, and parse the data. After closing the file, release the XML parser.
You see, as I said, the procedure is nothing special. However, before we discuss specific examples, let's look at some of the following caveats:
The expat does not validate XML. This means that as long as the XML file is well-formed-all elements are nested properly, and the start and close tags are free of errors-it will be parsed. Expat regardless of whether the XML adheres to the standard or definition referenced in the XML file header.
Expat converts the XML tags all to uppercase. Be careful if your script mixes uppercase and lowercase letters on the label signature and other content.
PHP is compiled with the Magic quotes setting enabled, so complex XML files are not parsed correctly. If Magic quotes is not the default setting, you should not say it.
Well, let's take a look at the example now!
Basic example
In order to simplify the complicated things, I omitted the error check and some other unnecessary things in the example, of course, you can do whatever you want in your code. I assume that you are already familiar with PHP and its syntax, and I will explain the XML functions. I'll start by explaining what the script means, and then defining the user-defined function, which is actually before the code that references them. Related accessories: Program listing A is shown as the complete code of the script, the XML document to parse the script is the relevant attachment: program listing B. The output after processing is shown in table A.
XML Articles
"Remedial XML for Programmers:basic syntax" in the first installment in a Three-part series, I'll introduce you to XML a nd its basic syntax.
"Remedial xml:enforcing document formats with DTDs" to enforce structure requirements for a XML document, you had to Tu RN to one of the XML ' s attendant technologies, data type definition (DTD).
"Remedial xml:using XML Schema" In this article, we'll briefly touch on the shortcomings of DTDs and discuss the basics O f A newer, more powerful standard:xml Schemas.
"Remedial Xml:say hello to DOM" Now it's time to put in your programmer ' s hat and get acquainted with Document Object Mod El (DOM), which provides easy access to XML documents via a tree-like set of objects.
"Remedial xml:learning to play SAX" in this fifth installment with our remedial XML series, I'll introduce you to the sax A PI and provide some links to SAX implementations in several languages.
Table A PHP parsing XML output results
First I created an instance of the XML parser:
$parser = Xml_parser_create ();
Next, I define how the parser encounters the start and end tags. Note that "startelement" and "endElement" are user-defined functions, and of course you can give them a different name according to your preferences, but these are the standard custom requirements.
Xml_set_element_handler ($parser, "startelement", "endElement");
Then I defined the data operation. Here the "Characterdata" is also a user-defined function, the name is also habitual.
Xml_set_character_data_handler ($parser, "characterdata");
Now open the file to read the data. You can start writing error handling code here, and I omit these error handling in the example. Don't forget to define $xml_file at the beginning of the script.
$filehandler = fopen ($xml _file, "R");
I started reading the contents of the file, reading 4K bytes at a time and placing it in the variable "$data" until the end of the file. I use Xml_parse parsing these data segments to read.
while ($data = Fread ($filehandler, 4096)) {
Xml_parse ($parser, $data, feof ($filehandler));
}
    • Total 2 Pages:
    • Previous page
    • 1
    • 2
    • Next page

http://www.bkjia.com/PHPjc/364094.html www.bkjia.com true http://www.bkjia.com/PHPjc/364094.html techarticle Now everyone advocates that XML is the best friend of Web developers, with the help of XML, which can easily format and display data from almost any data source. 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: 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.