<?php
/*
(c) 2000 Hans Anderson Corporation. All Rights Reserved.
You are free to use and modify this class under the same
guidelines found in the PHP License.
-----------
bugs/me:
http://www.hansanderson.com/php/
me@hansanderson.com
-----------
Version 1.0
- 1.0 is the first actual release of the class. It's
finally what I was hoping it would be, though there
are likely to still be some bugs in it. This is
a much changed version, and if you have downloaded
a previous version, this WON'T work with your existing
scripts! You'll need to make some SIMPLE changes.
- .92 fixed bug that didn't include tag attributes
(to use attributes, add _attributes[array_index]
to the end of the tag in question:
$xml_html_head_body_img would become
$xml_html_head_body_img_attributes[0],
for example)
-- Thanks to Nick Winfield <nick@wirestation.co.uk>
for reporting this bug.
- .91 No Longer requires PHP4!
- .91 now all elements are array. Using objects has
been discontinued.
-----------
|
What class.xml.php is:
A very, very easy to use XML parser class. It uses PHP's XML functions for you, returning one array that has all the tag information. The only hard part is figuring out the syntax of the tags!
Sample use:
require('class.xml.php');
$file = "data.xml";
$data = implode("",file($file)) or die("could not open XML input file");
$obj = new xml($data,"xml");
print $xml["hans"][0]->num_results[0];
for($i=0;$i<sizeof($xml["hans"]);$i++) {
print $xml["hans"][$i]->tag[0] . " ";
}
To print url attributes (if they exist):
print $xml["hans"][0]->attributes[0]["size"]; #
where "size" was an attr name
(that's it! slick, huh?)
-----------
Two ways to call xml class:
$xml = new xml($data);
- or -
$xml = new xml($data,"jellyfish");
The second argument (jellyfish) is optional. Default is 'xml'.
All the second argument does is give you a chance to name the array
that is returned something besides "xml" (in case you are already using
that name). Normal PHP variable name rules apply.
----------
Explanation of xml class:
This class takes valid XML data as an argument and
returns all the information in a complex but loopable array.
Here's how it works:
Data:
<html>
<head>
<title>Hans Anderson's XML Class</title>
</head>
<body>
</body>
</html>
Run the data through my class, then access the title like this:
$xml["html_head"][0]->title[0];
Or, loop through them:
for($i=0;$i<sizeof($xml["html_head"]);$i++) {
print $xml["html_head"][$i]->title[0] . " ";
}
Yes, the variable names *are* long and messy, but it's
the best way to create the tree, IMO.
Here is a complex explanation I sent to one class.xml.php user:
---------
> Now I've run into another problem:
>
> <STORY TIMESTAMP="2000-12-15T20:08:00,0">
> <SECTION>Markets</SECTION>
> <BYLINE>By <BYLINE_AUTHOR ID="378">Aaron
L. Task</BYLINE_AUTHOR><BR/>Senior
> Writer</BYLINE>
> </STORY>
>
> How do I get BYLINE_AUTHOR?
print $xml["STORY_BYLINE"][0]->BYLINE_AUTHOR[0];
> And just a little question: Is there an easy way to get TIMESTAMP?
print $xml["STORY"][0]->attributes[0]["TIMESTAMP"];
|
This is confusing, I know, but it's the only way I could really do this. Here's the rundown:
The $xml part is an array -- an array of arrays. The first array is the 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.
如果您对本文有任何疑问或者建议,请到讨论区发表您的意见:
>>
论坛入口 <<
上一篇:
用php生成excel文件
下一篇:
PHP中的类-什么叫类
【文章评论】
【收藏本文】
【推荐好友】
【打印本文】
【我要投稿】 【论坛讨论】