日期:2012-11-25  浏览次数:20416 次

 

name of the tag -- in the first case above, this is the tag STORY, and
below that BYLINE.  You want BYLINE_AUTHOR.  You want the first BA.  The
first one is index [0] in the second part of the two-dimensional array.

Even if there is only *one* byline author, it's still an array, and you
still have to use the [0].  Now, the two-dimensional array is storing
dynamic structures -- objects in this case.  So, we need to dereference
the object, hence the ->.  The BYLINE_AUTHOR is the tag you want, and it
is an array in that object.  The reason for the array is that if there are
more than one BYLINE_AUTHOR for the tags STORY, BYLINE, we would have a
[0] and [1] in the array.  In your case there is just the one.

*** This is very confusing, I know, but once you understand it, the power
of this method will be more apparent.  You have access to *every* bit of
information in the XML file, without having to do anything but understand
how to refer to the variables. ***

EVERY variable will look like this:
       print $xml["STORY_BYLINE"][0]->BYLINE_AUTHOR[0];

The trick is understanding how to get the variable to give you the
information.  This is an array of arrays of objects holding arrays!

Any tag that has attributes will have them stored in a special object
array named "attributes" and will be called this way:

       print $xml["STORY"][0]->attributes[0]["TIMESTAMP"];

If you aren't sure if there are attributes, you could do isset() or
is_array() for that above example.  If isset(), you could for loop and
while(list($k,$v) = each($xml...)) over it to get the values.


         array of
                 objects
                    |
                    |
$xml["STORY_BYLINE"][0]->BYLINE_AUTHOR[0];
          ^                    ^
       array of                ^
        arrays                 ^
                               ^
                            array in
                             object

In general, to get the value of this:

<STATE>
       <STATENAME></STATENAME>
       <COUNTY>
               <COUNTYNAME></COUNTYNAME>
               <CITY></CITY>
               <CITY></CITY>
       </COUNTY>
       <COUNTY>
               <COUNTYNAME></COUNTYNAME>
               <CITY></CITY>
               <CITY></CITY>
       </COUNTY>
</STATE>

You would look for what you want, say "CITY", then go UP one level, to
COUNTY (COUNTYNAM