PHP中的类-操作XML(2)
2008-05-01 04:18
525 查看
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();XML:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
相关文章推荐
- PHP DOM操作XML文档,查询遍历结点元素
- PHP DOM操作xml文档,修改元素
- php5&Flash as2操作xml
- PHP操作HTML/XML的类库 QueryPath需要启用javascri
- php xml操作
- xml之phpdom操作
- 用php操作xml的方法
- PHP扩展之XML操作(四)——XMLReader
- PHP通过DOM操作XML
- XMl入门介绍及php操作XML
- DOM基础及php读取xml内容操作的方法
- PHPdom操作查找xml标签文本
- php对图书的增删改查操作(xml作为数据存储机制)
- php DOMElement 操作xml 文档的示例代码
- PHP通过DOM操作XML
- PHP对XML文件操作之属性与方法讲解
- php之SimpleXml操作xml
- PHP操作XML作为数据库的类
- php simpleXML操作xml的用法
- php操作xml详解