### PHP 中的类－操作 XML(2)

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 (COUNTYNAME is on the same 'level'), for your first array:
$xml["STATE_COUNTY"] -- ALL tags pushed together are separated with
"_". Otherwise tags are as they were -- spaces, dashes, CaSe, etc.
Now, you want the first COUNTY, though there are two, so we are do this:
$xml["STATE_COUNTY"][0] -- to get the second, we'd use [1] instead of
[0]. You could also do a for() loop through it, using sizeof() to figure
out how big it is.
So, we have the STATE,COUNTY we want -- the first one. It's an
object, and we know we want the CITY. So, we dereference the object. The
name of the array we want is, of course, CITY:
$xml["STATE_COUNTY"][0]->CITY[0] (the first one, the second one would be
[1]).
And that's it. Basically, find what you want, and go up a level.
You could do some complex for loops to go through them all, too:
for($i=0;$i<sizeof($xml["STATE_COUNTY"]);$i++) {
for($j=0;$j<sizeof($xml["STATE_COUNTY"][0]->CITY);$j++) {
print $xml["STATE_COUNTY"][$i]->CITY[$j];
}
}
-----------
Whew. I hope that helps, not hurts.
*/
/* used to store the parsed information */
class xml_container {
function store($k,$v) {
$this->{$k}[] = $v;
}
}
/* parses the information */
class xml {
// initialize some variables
var $current_tag=array();
var $xml_parser;
var $Version = 1.0;
var $tagtracker = array();