PHP and XML: Use the expat function (2)

Source: Internet
Author: User
Tags opening and closing tags
PHP and XML: Use the expat function (2) PHP and XML: Use the expat function (2)
Let's take a look at the PHP code that actually processes this document.



/* NewsBoy: News system for the web written in PHP by Justin Grant (Web: jusgrant.cjb.net or justin.host.za.net Mail: justin@glendale.net) 25 March V0.0.2 Converted Newsboy to a PHP class, allowing the layout to be easily modified. also added made the HTML that is genrated a little easier to read.24 March V0.0.1 Just completed the intial version, very rough and basic. */
Class newsboy {var $ xml_parser; var $ xml_file; var $ html; var $ open_tag; var $ close_tag;
// Class Constructor
Function newsboy () {$ this-> xml_parser = ""; $ this-> xml_file = ""; $ this-> html = ""; $ this-> open_tag = array (
// These are the default settings but they are quite easy to modify
"NEWSBOY" => "nn", "STORY" => "", "DATE" => "", "SLUG" => "", "TEXT" => "", "PIC" => "", "NEWLINE" => ""); $ this-> close_tag = array ("NEWSBOY" =>"
Nnn "," STORY "=>" "," DATE "=>" "," SLUG "=>"
"," TEXT "=>" n "," PIC "=> ""
");}
// Class Destructor (has to be invoked manually as PHP does not support destructors)

Function destroy () {xml_parser_free ($ this-> xml_parser );}
// Class Members
Function concat ($ str) {$ this-> html. = $ str ;}
Function startElement ($ parser, $ name, $ attrs) {// global $ open_tag; if ($ format = $ this-> open_tag [$ name]) {$ this-> html. = $ format ;}}
Function endElement ($ parser, $ name) {global $ close_tag; if ($ format = $ this-> close_tag [$ name]) {$ this-> html. = $ format ;}}
Function characterData ($ parser, $ data) {$ this-> html. = $ data ;}
/* Function PIHandler ($ parser, $ target, $ data) {// switch (strtolower ($ target) {// case "php": eval ($ data ); // break ;//}}*/
Function parse () {$ this-> xml_parser = xml_parser_create (); xml_set_object ($ this-> xml_parser, & $ this ); // use case-folding so we are sure to find the tag in $ map_array
Xml_parser_set_option ($ this-> xml_parser, parser, true); xml_set_element_handler ($ this-> xml_parser, "startElement", "endElement"); aggregate ($ this-> xml_parser, "characterData"); // xml_set_processing_instruction_handler ($ this-> xml_parser, "PIHandler ");
If (! ($ Fp = fopen ($ this-> xml_file, "r") {die ("cocould not open XML input ");} while ($ data = fread ($ fp, 4096) {if (! Xml_parse ($ this-> xml_parser, $ data, feof ($ fp) {die (sprintf ("XML error: % s at line % d ", xml_error_string (xml_get_error_code ($ this-> xml_parser), xml_get_current_line_number ($ this-> xml_parser )));}}}}
?>



--------------------------------------------------------------------------------

In the constructor of this class, I created two Mark arrays: open and close. The keyword of the array and the tag I will analyze later are:
And their corresponding values include HTML code for formatting the opening and closing tags.

I have defined a simple class Destructor to release the XML analyzer when we no longer need it. This function has to be called manually,
Because PHP does not support automatic calling of class destructor when an object is released.

Then I defined the main callback method used in the XML document to analyze the enable and disable tags. I have also defined a data analysis method
It is used to format the data in a simple manner when there is data in the open and close tags. I will show you how to register these callback methods.
To the analyzer.

Use the tag name in startElement and closeElement (called separately when an open or closed logo set is analyzed)
Queries the corresponding array as the index key value. If the key value exists, the return value is appended to the 'HTML 'attribute of the class.
The 'HTML 'attribute will be used when the document content will be displayed in the future.

The characterData method simply adds the value between tags to the end of the class's html attribute.

The Annotated PIHandler method is a callback function, which I have not implemented yet. If it exists, it will be directly stored in the XML
Process php scripts in this document.

Now, let me explain how to call the main analysis method. you can guess, parse ()!!!

The first line calls the xml_parser_create () function, which returns an expat xml analyzer instance and is saved in
Attribute & this-> xml_parser.

Next, we need to use the xml_set_object () function to register the callback function of a class method.

I use xml_set_object ($ this-> xml_parser, & $ this) in this way ). I specified
To save the class attributes of the xml Analyzer. in the second parameter, I specified the PHP object instance address. This allows analyzer to know all
The callback function to be registered is the actual method of specifying the class on that address. This is like a 'reference transfer 'in c or c ++, and someone else
It is simply called 'Reference variable '.

In the next line, I call xml_parser_set_option () to set the attributes of an xml analyzer.
Folding ). The case folding only tells the analyzer that when I analyze my XML documents, I do not care about case sensitivity, but if you
If you want to use case sensitivity to define two different tags, for example, or, you can not set it.

By using xml_set_element_handler (), I specify a callback function for the start and end tags. The name is
"StartElement" and "endElement ".

Next, I use xml_set_character_data_handler () to specify the processing handle of character data
CharacterData () callback function. Calls the annotated function, xml_set_processing_instruction_handler (),
Is a call that I use to register the function PIHandler. PIHandler can be included in XML documents to process php code.

Other code simply reads the XML file and analyzes it. If an error occurs, the error details will be returned, including the error
The row number that occurs.

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.