国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP中的類-操作XML(2)

2024-05-04 23:02:32
字體:
來源:轉載
供稿:網友

 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 漳浦县| 印江| 津南区| 长海县| 于田县| 宜良县| 新和县| 合江县| 巨野县| 界首市| 阿尔山市| 景宁| 扎赉特旗| 永胜县| 邵武市| 镇宁| 青岛市| 平利县| 胶州市| 宁河县| 河曲县| 宣恩县| 盐池县| 阿鲁科尔沁旗| 文安县| 天台县| 怀远县| 奈曼旗| 靖宇县| 沧源| 宽城| 高台县| 望谟县| 奉节县| 遂溪县| 三亚市| 邓州市| 棋牌| 方城县| 富川| 正蓝旗|