#native_company# #native_desc#
#native_cta#

XML How-To Page 2

By Joe Stump
on September 7, 2000

<?php

// handles the attributes for opening tags

// $attrs is a multidimensional array keyed by attribute

// name and having the value of that attribute

function startElement($parser$name$attrs=''){

    global 
$open_tags$temp$current_tag;

    
$current_tag $name;

    if (
$format $open_tags[$name]){

    switch(
$name){

        case 
'STORY':

        echo 
'New Story: ';

        break;

        default:

        break;

    }

    }

}

// $current_tag lets us know what tag we are currently

// dealing with - we use that later in the characterData

// function.

//

// when we see a </STORY> we know that it is time to

// flush our temp variables and prepare to move onto

// the next one

function endElement($parser$name$attrs=''){

    global 
$close_tags$temp$current_tag;

    if (
$format $close_tags[$name]){

    switch(
$name){

        case 
'STORY':

        
return_page($temp);

        
$temp '';

        break;

        default:

        break;

    }

    }

}

// this function is passed data between elements

// theu $data would equal 'Title Here'

// in the line <TITLE>Title Here</TITLE>

function characterData($parser$data){

    global 
$current_tag$temp$catID;

    switch(
$current_tag){

    case 
'TITLE':

        
$temp['title'] = $data;

        
$current_tag '';

        break;

    case 
'URL':

        
$temp['url'] = $data;

        
$current_tag '';

        break;

    default:

        break;

    }

}

?>



As you can see so far XML parsing in PHP isn’t all that bad. Now for the
fun part – parsing the file! For that you will need the rest of the code, which is fairly simple.