Express Gate PHP and XML: Using the Expat function two

Source: Internet
Author: User
Tags opening and closing tags xml parser
PHP and XML: Using the Expat function (ii)
Let's take a look at the PHP code that actually handles this document.
/*newsboy:news system for the web written in PHP by Justin Grant (Web:jusgrant.cjb.net or Justin.host.za.net mail:just In@glendale.net) March V0.0.2 converted Newsboy to a PHP class, allowing the layout to be easily modified. Also added made the HTML that's 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 is the default settings but they is quite easy to modify
"Newsboy" = "nn", "Story" and "" "," DATE "=" "," SLUG "and" "," TEXT "=" "," PIC "=" "," NEWLINE "=&G T "" ); $this->close_tag = Array ("Newsboy" and "="
NNN "," Story "=" "," DATE "and" "," SLUG "and" = "
"," TEXT "=" n "," PIC "=" ""
" ); }
Class destructor (have to is 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->h tml. = $format; } }
function EndElement ($parser, $name) {Global $close _tag, if ($format = $this->close_tag[$name]) {$this->html. = $for Mat } }
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 is sure to find the tag in $map _array
Xml_parser_set_option ($this->xml_parser, xml_option_case_folding, true); Xml_set_element_handler ($this->xml_parser, "startelement", "endElement"); Xml_set_character_data_handler ($this->xml_parser, "Characterdata");//xml_set_processing_instruction_handler ( $this->xml_parser, "Pihandler");
if (! ( $fp = fopen ($this->xml_file, "R")) {die (' could not open XML input '),} while ($data = Fread ($fp, 4096)) {if (!xml_pa RSE ($this->xml_parser, $data, feof ($fp))) {Die (sprintf ("XML error:%s @ line%d", Xml_error_string (xml_get_error_ Code ($this->xml_parser)), Xml_get_current_line_number ($this->xml_parser)); } } }}
?>
--------------------------------------------------------------------------------
In the constructor for this class, I created an array of two tags that are opened and closed. The keyword of the array is the tag that I will parse later
The same, and their corresponding values contain HTML code that formats open and close tags.
I have defined a simple class destructor to release the XML parser when we no longer need it. This function has to be called manually,
Because PHP does not support the automatic invocation of a destructor for a class when an object is disposed.
I then defined the primary callback method used in the XML document to parse the open and close tags. I have also defined a data analysis method that will
For simple formatting of data when there is data in the opening and closing tags, I'll show you how to register these callback methods
into the parser.
Use the name of the tag in Startelement and closeelement (called separately when parsing to an open or closed tag)
The corresponding array is queried as the index key value. If the key value exists, the value is returned and appended to the ' HTML ' property of the class.
The ' HTML ' attribute will be used in the future when we actually display the contents of the document.
The Characterdata method simply adds the value between the tags to the back of the class's HTML property.
The annotated method called Pihandler is a callback function that I have never implemented. If it exists, it will be directly in the XML
The PHP script is processed in the document.
Now, let me explain the invocation of the main parsing method, you guess, parse ()!!!
The first line invokes the function xml_parser_create (), which returns an instance of the expat XML parser, and is saved in the class's
Property in &this->xml_parser.
Next, we need to use the function Xml_set_object () to register a callback function for a class method.
This is how I use it, Xml_set_object ($this->xml_parser, & $this). I specified in the first argument that a
To save the XML parser's class properties, and then in the second argument, I specify the instance address of the PHP object. This allows the analyzer to know the full
The callback function to be registered is the actual method of specifying the class at that address. This is like a ' reference pass ' in C or C + +, and someone
Simply referred to as the ' reference variable '.
On the next line, I called Xml_parser_set_option () to set the properties of an XML parser, using casing folding (case
Folding). Case folding just tells the parser that when I parse my XML document I don't care about case sensitivity, but if you
To use case sensitivity to define two different tokens, such as or, you can not set it.
By using Xml_set_element_handler (), I specified a callback function for the start and end tags, with the name
"Startelement" and "endElement".
Next, I use Xml_set_character_data_handler () to specify that the processing handle of the character data is named
The callback function for Characterdata (). Called by the Annotated function, Xml_set_processing_instruction_handler (),
is a call I used to register the function Pihandler (). Pihandler can be included in XML documents to process PHP code.
The other code simply reads the XML file and parses it. If an error occurs, then the error details are returned, including the error
The line number that occurred.

The above describes express gate PHP and XML: Using the expat function two, including the Express gate content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.