EXPAT analysis XML with PHP toolkit
Author: BUILDER.COM XML is now advocating for all Web developers best friend, with the help of XML, which can easily format and display data from virtually any data source. However, in terms of dynamic content, the data excellent in format is far from the ideal state. Most of the web developers will tell you that there is no dynamic content on the Internet today! The problem is: "How do you create dynamic content with XML?" The answer is to resolve XML with dynamic content processing language, such as PHP or Perl, etc., in theory, such programming is the utilization of various purposes. XML. Nothing is to use some toolkits that can analyze XML. James Clark provides a toolkit called Expat. The EXPAT XML Toolkit uses C language to parse XML, which makes PHP to dance with XML. PHP is a great script language designed for web. XML is a standard representing the content of the web. It's more beautiful to join hands! Below I will demonstrate a simple example to read this example, how to use PHP to parse the XML document to HTML. Then I will introduce some other XML concepts of PHP. The use of PHP parsing XML is very simple, it is very intuitive but it needs to be explained in detail. Once you really have the essentials of your application, you will definitely be surprised how you don't think of them together. Overview PHP uses the XML toolkit with EXPAT to parse XML through the C language. The function set of this kit is the same as the function set used by Perl XML, in addition, such a toolkit is an event-driven parser. That is to say, Expat uses each XML tag or a new row code as the start of the event, and the event is the trigger of the function. EXPAT installation is very simple, if you are using the Apache web server, then you can find the installation and download guidelines on the PHP XML reference page. The basic task of using PHP parsing XML is this: First, create an instance of an XML parser. Next, the function of processing the triggering event is defined, such as starting or ending the label. Subsequently, the actual data handler is defined. Finally, open the XML file, read file data, and resolve data. Then close the file release XML parser. You are, just as I said, this set of operation is nothing special. However, I understand some of the following warnings before we discuss specific examples:
EXPAT does not test XML. This means that as long as the XML file format is correct - all elements are nested, the start and closing tags do not have any errors - it will be parsed. EXPAT can comply with the standard or definitions of the XML file header regardless of XML. EXPAT converts all XML tags to uppercase letters. If your script is mixed in the label name and other content, you should be careful. PHP is compiled with Magic Quotes settings, then complex XML files will not get the correct parsing. If Magic Quotes is not the default setting, you just didn't say it. Ok, let's take a look at the example! Basic examples in order to simplify complex things, I omitted the error check and other unnecessary things in the example, of course, you can do whatever you can do with your own code. I assume that you are familiar with PHP and its grammar, and I will explain the XML function. First of all, I will explain the meaning of the script, then define the user-defined function, in fact, these functions are before reference to their code. Related Attachments: Program List A shows the full code of the script, the XML document to be parsed by the script is related attachment: List B. The output results after processing are shown in Table A.
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 shortcomings 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 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 in table a PHP XML parsing output First of all, I created X One example of the ML parser: $ parser = XML_Parser_create (); then, I define the operation when the parser encounters the start and end tags. Note "StartElement" and "endelement" are user-defined functions, of course, you can give them another name in your own preferences, but these names are the standard habits. XML_SET_EEMENT_HANDLER ($ PARSER, "StartElement", "endElement"); then I defined data operations. The "CharacterData" here is also a user-defined function, the name is also habitual. XML_SET_CHARACTER_DATA_HANDAL ($ PARSER, "CharacterData"); now opens the file read data. You can start writing an error handling code here, I omitted these error handles 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 file content, read 4k bytes at a time, put it in the variable "$ data" to the end of the file. I use XML_PARSE to parse the read data segments. While ($ DATA = FREAD ($ FILEHANDAL, 409)) {XML_PARS ($ Parser, $ DATA, FeOf ($ fileHandler));} Last clear, turn off files, and release parsers.
Fclose ($ fileHandler); XML_Parser_Free ($ PARSER); above is all XML functions used in the script, and then explain the three user-defined functions used in the script, they are "startelement", "endelement", " Characterdata. As long as the XML_PARS encounters the "; Break;}} After processing element tags, once XML_PARS will call the" CharacterData "function once you encounter XML data. This function is also automatically called by the parser, which requires two parameters, parser instances, and data String .Function CharacterData ($ PARSER_INSTANCE, $ XML_DATA) {Echo $ XML_DATA;} Last XML_PARSE encounters an end tag, run "endelement" function, the function with two parameters, parser instance and element name. Function endelement ($ Parser_Instance $ ELEMENT_NAME) {Switch ($ Element_Name) {CASE "URL": Echo "/"> "; break; case" title ": echo" td> "; break; case" summary ": echo " Td> tr>"; Break;}} With php parsing XML lightweight, it is over, below we started with some resource. Increasing the function PHP has some other functions related to XML parsing. PHP.NET documents have a complete description of these functions. I mentioned here, you may soon use these functions shortly: