If you have an RSS feed that looks something like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<description>Advocacy, Evangelism, and Propaganda for Standards-Compliant Design.</description>
<description>Joe Lieberman spammed me today! Well, kind of...</description>
Assuming that I've created a variable to represent the <channel> tag, I could use the following code to parse out the value of the first <description> tag:
For the sake of naming purposes, let's assume that <item> has
a child of <description>. <item> is
a child of <channel>, and so is its "brother", <description>. Therefore we have two <description> tags with an Uncle-Nephew relationship. This is how I will explain them.
If the Uncle <description> exists (which it should, since it's a required element, but I digress), then the above code will parse out the value of the Uncle <description>. So far, so good.
Keeping the same assumptions, the following code will parse out the value of the Nephew <description> tag:
Excellent! The problem is this: If the Uncle <description> tag is omitted for whatever reason by the creator of the RSS feed, then the code from Figure 1 will default to the Nephew <description> tag instead of returning false (which is what I've programmed the script to do if the sought-out element does not exist). This simply won't do.
Is there any way (or ideas!) to keep the parsing of elements contained within its own "generation" without trying to parse a nested element of the same name?