Where Am I? (or Call You Right Back)
Much of the code I have in the callback functions keeps track of where the parser is in the XML file when the function is called. This is so the RSS data can be stored in the correct place. By default, "case folding" is enabled in the XML parser functions. Therefore, any string compares are made using uppercase strings.
StartTag() is called when the parser reaches any start tag in the RSS file. For this project, I'm only concerned with whether the parser is in an ITEM. If it's not, it's parsing CHANNEL data. If, as you're parsing, you want to keep closer track of where you are in the XML hierarchy, you might use a stack. Push the current element in the start function and pop it in the end function.
I also want to know if I'm entering the CHANNEL's image tag, because I want to scope the data inside that tag as coming from within the image tag. Also, if the parser is in the ITEM's enclosure tag, I want to store the URL attribute of that tag as part of my item data.
EndTag() is called when the parser reaches any end, or closing, tag in XML file. Here is where I'm going to store the data retrieved from between the start and end tags in the correct global variables. I choose to trim any whitespace that may have been in the feed file. Then, if the parser has come to the end of an item, I note that I'm not currently in an item and bump up my item count. The item count is kept for indexing the 2D array of items. I also want to know if I've come to the end of an image tag.
Now, if any data from between the start/end tags has been retrieved, I store it in the proper place. It's either ITEM data or CHANNEL data. CHANNEL data is further scoped if it came from within the image tag. When storing the data, I take advantage of PHP's associative array feature. I use the tag name as the array key. Users of the class can then easily access any data just by knowing its tag name and whether it's CHANNEL or ITEM data they're looking for. The function ends after resetting the global variables to accept new input.
Last, the TagData() function simply appends to the global member that stores tag data. For some reason, that's not made clear in the PHP documentation; the callback specified in xml_set_character_data_handler() (in this case, TagData) is sometimes called more than once with parts of the character data. This seems to happen when there are linefeeds and special XML characters (like quotes) in the data. That's why I concatenate the data to the global.