Home > php教程 > php手册 > PHP中的类-操作XML(2)

PHP中的类-操作XML(2)

WBOY
Release: 2016-06-21 09:02:01
Original
1083 people have browsed it

 

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:


       
       
               
               
               
       

       
               
               
               
       



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
       for($j=0;$jCITY);$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();



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template