Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Web Development

Shelling the Pod


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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.