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

PHP中的类-操作XML(2)

WBOY
풀어 주다: 2016-06-21 09:02:01
원래의
1078명이 탐색했습니다.

 

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();



관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿