Parsing XML attributes

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

I'm just learning how to parse XML, and the script from is the first
one that actually worked for me.

However, it is a very simple example with only Elements in the XML
file.  What I need to be able to do is get data from Attributes as
well.  Here is the code I'm using:

// Create an XML parser
$xml_parser = xml_parser_create();

// Set the functions to handle opening and closing tags
xml_set_element_handler($xml_parser, "startElement", "endElement");

// Set the function to handle blocks of character data
xml_set_character_data_handler($xml_parser, "characterData");

// Open the XML file for reading
$fp = fopen("seattle_info.xml","r")
       or die("Error reading RSS data.");

// Read the XML file 4KB at a time
while ($data = fread($fp, 4096))
   // Parse each 4KB chunk with the XML parser created above
   xml_parse($xml_parser, $data, feof($fp))
       // Handle errors in parsing
       or die(sprintf("XML error: %s at line %d",

// Close the XML file

// Free up memory used by the XML parser

$insideitem = false;
$tag = "";
$position = "";
$name = "";
$cityweburl = "";
$imageurl = "";

function startElement($parser, $tagName, $attrs) {
    global $insideitem, $tag;
    if ($insideitem) {
        $tag = $tagName;
    } elseif ($tagName == "COUNCILMEMBER") {
       $insideitem = true;

function characterData($parser, $data) {
   global $insideitem, $tag, $position, $name, $cityweburl, $imageurl;
   if ($insideitem) {
           switch ($tag) {
           case "POSITION":
           $position .= $data;
           case "NAME":
           $name .= $data;
           case "CITYWEBURL":
           $cityweburl .= $data;
           case "IMAGEURL":
           $imageurl .= $data;

function endElement($parser, $tagName) {
   global $insideitem, $tag, $position, $name, $cityweburl, $imageurl;
   if ($tagName == "COUNCILMEMBER") {
        printf("<h2><a href='%s'>%s</a></
        printf("<img src='%s'>",trim($imageurl));

       $position = "";
       $name = "";
       $cityweburl = "";
       $imageurl = "";
       $insideitem = false;

I see the $attrs variable in the startElement function, but it doesn't
seem to be anywhere else in the example that I can find.

So, what can I do to this code to parse and assign the values of the
attributes to variables so I can use them on my pages?

Site Timeline