#native_company# #native_desc#
#native_cta#

PHP and XML: Using the expat functions Page 3

By Justin Grant
on July 30, 2000

Let’s look at the code for the PHP class that actually parses this document.


<?php

/*

NewsBoy:     a News system for the web written in PHP by Justin Grant

    (Web: jusgrant.cjb.net or justin.host.za.net Mail: [email protected])

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" => "n<!-- XML Starts here -->n<TABLE COLS=1 CELLPADDING=5>",

                            
"STORY"     => "<TR><TD BGCOLOR=#222222>",

                            
"DATE" => "<FONT COLOR=#BBBB00>",

                            
"SLUG"  => "<FONT COLOR=#00AACC><B>&nbsp;&nbsp;",

                            
"TEXT"  => "<FONT COLOR=#AAAAAA>",

                            
"PIC" => "",

                            
"NEWLINE" => ""

                            
);

        
$this->close_tag = array(

                            
"NEWSBOY" => "</TABLE>n<!-- XML Ends here -->nn",

                            
"STORY"     => "</TD></TR>",

                            
"DATE" => "</FONT>",

                            
"SLUG"  => "</B></FONT><BR>",

                            
"TEXT"  => "</FONT>n",

                            
"PIC" => "<IMAGE SRC=",

                            
"NEWLINE" => "<BR>"

                            
);

        

        }

//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_parserXML_OPTION_CASE_FOLDINGtrue);

        
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($fp4096)) {

                    if (!
xml_parse($this->xml_parser$datafeof($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 class constructor I have defined two indexed arrays for opening and closing tags.
The keynames have the same name as the tags that I am going to parse later and their corresponding values contain HTML
for formatting on an open or close tag.
I have defined a simple class destructor to destroy the xml parser when we no longer require it. This destructor has to be called
manually as PHP does not support class destructors when an object is destroyed.
I then define the main callback methods for parsing the open and close tags in the XML document. I also define a data parsing method
that will simply format the data between the open and close tags, if there is any data between them.
Later on I will show you how to register these callback methods with the parser.