#native_company# #native_desc#

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.



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;



$open_tag ;

$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>",


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


"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() {




//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) {


                   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":


                //    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_set_processing_instruction_handler($this->xml_parser, "PIHandler");

        if (!($fp fopen($this->xml_file"r"))) {

"could not open XML input");


                while ($data fread($fp4096)) {

                    if (!
xml_parse($this->xml_parser$datafeof($fp))) {

sprintf("XML error: %s at line %d",








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.