Home >Backend Development >PHP Tutorial >About the PHP toolkit expat parsing XML_PHP tutorial

About the PHP toolkit expat parsing XML_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:27:48900browse

As PHP develops, its functions are also becoming more powerful. The introduction of this article mainly introduces the functions of the PHP toolkit. I hope it can be helpful to everyone. Everyone these days is touting XML as a Web developer's best friend, making it easy to format and display data from almost any data source. However, well-formatted data is far from ideal for dynamic content. Most web developers will tell you how the web today can work without dynamic content! The question is: "How to create dynamic content using XML?"

The answer is to use a dynamic content processing language to parse XML, such as PHP or Perl, etc. , Theoretically, this type of programming language can utilize XML for various purposes. It's nothing more than using some toolkits that can parse XML. James Clark provides a toolkit called expat. The expat XML toolkit uses C language to parse XML, making it easy for PHP and XML to dance together. PHP is a great scripting language designed specifically for the web. XML is a standard for representing web content. How beautiful it would be if the two joined forces!

Below I will show readers a simple example. This example can illustrate how to use the PHP toolkit to parse XML documents into HTML. Then I'll introduce some other XML concepts of PHP. Parsing XML with the PHP toolkit is simple and intuitive but requires some explanation of the details. Once you really get the hang of it, you'll be surprised why you didn't think of combining them earlier.

Overview

PHP uses expat, an XML toolkit, to parse XML through C language. The function set of this toolkit is the same as that used by Perl XML parsing. In addition, this toolkit is an event-driven parser. That is to say, expat treats each XML tag or new line of code as the start of an event, and the event is the trigger of the function. Installing Expat is very simple, and if you are using the Apache web server, you can find installation and download instructions 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 functions that handle trigger events, such as start or end tags. Subsequently, define the actual data processing procedures. Finally, the XML file is opened, the file data is read and the data is parsed. Afterwards closing the file releases the XML parser.

Look, like I said, there’s nothing special about this process. However, before we discuss specific examples, here are some caveats: Expat does not perform XML validation. This means that as long as the XML file is well-formed - all elements are nested properly, opening and closing tags have no errors - it will be parsed. Expat does not care whether the XML conforms to the standards or definitions referenced in the XML file header. Expat converts all XML tags to uppercase letters. Be careful if your script mixes uppercase and lowercase letters in tag names and other content. PHP is compiled with the magic quotes setting enabled, so complex XML files will not be parsed correctly. If magic quotes is not the default setting, just pretend I didn't say it.

Basic Example

In order to simplify complex things, I have omitted error checking and other unnecessary things in the example. Of course, you can do it yourself The code is whatever you want. I assume that you are already familiar with PHP and its syntax, and I will explain the XML functions. First I'll explain what a script is, and then I'll define user-defined functions that actually precede the code that references them. Related Attachment: Program Listing A shows the complete code of the script, and the XML document to be parsed by the script is Related Attachment: Program Listing B. The output results after processing are shown in Table A.

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



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446502.htmlTechArticleWith the development of PHP, its functions are also becoming more powerful. The introduction of this article mainly introduces the functions of PHP toolkit. I hope it can be useful to everyone. Bring help. Nowadays, everyone advocates that XML is the best friend of web developers. With...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn