动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   PHP → 阅读文章

 一个操作xml的类

作者来源: 
阅读 1196 人次 , 2006-3-29 4:08:00 


<?
/*
(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
showstv@163.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.
*/

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

/* here are the xml functions needed by expat */


/* when expat hits an opening tag, it fires up this function */
function startelement($parser, $name, $attrs){

array_push($this->current_tag, $name); // add tag to the cur. tag array
$curtag = implode("_",$this->current_tag); // piece together tag

/* this tracks what array index we are on for this tag */

if(isset($this->tagtracker["$curtag"])) {
$this->tagtracker["$curtag"]++;
}
else{
$this->tagtracker["$curtag"]=0;
}

/* if there are attributes for this tag, we set them here. */

if(count($attrs)>0) {
$j = $this->tagtracker["$curtag"];
if(!$j) $j = 0;

if(!is_object($globals[$this->identifier]["$curtag"][$j])) {
$globals[$this->identifier]["$curtag"][$j] = new xml_container;
}

$globals[$this->identifier]["$curtag"][$j]->store("attributes",$attrs);
}

}// end function startelement



/* when expat hits a closing tag, it fires up this function */
function endelement($parser, $name) {
$curtag = implode("_",$this->current_tag); // piece together tag

// before we pop it off,
// so we can get the correct
// cdata

if(!$this->tagdata["$curtag"]) {
$popped = array_pop($this->current_tag); // or else we screw up where we are
return; // if we have no data for the tag
}
else{
$td = $this->tagdata["$curtag"];
unset($this->tagdata["$curtag"]);
}

$popped = array_pop($this->current_tag);
// we want the tag name for
// the tag above this, it
// allows us to group the
// tags together in a more
// intuitive way.

if(sizeof($this->current_tag) == 0) return; // if we aren't in a tag

$curtag = implode("_",$this->current_tag); // piece together tag
// this time for the arrays

$j = $this->tagtracker["$curtag"];

if(!$j) $j = 0;

if(!is_object($globals[$this->identifier]["$curtag"][$j])) {
$globals[$this->identifier]["$curtag"][$j] = new xml_container;
}

$globals[$this->identifier]["$curtag"][$j]->store($name,$td);
#$this->tagdata["$curtag"]);
unset($td);
return true;
} // end function endelement


/* when expat finds some internal tag character data,
 it fires up this function */

function characterdata($parser, $cdata) {
$curtag = implode("_",$this->current_tag); // piece together tag
$this->tagdata["$curtag"] .= $cdata;
}


function xml($data,$identifier='xml') {  

$this->identifier = $identifier;

// create parser object
$this->xml_parser = xml_parser_create();

// set up some options and handlers
xml_set_object($this->xml_parser,$this);
xml_parser_set_option($this->xml_parser,xml_option_case_folding,0);
xml_set_element_handler($this->xml_parser, "startelement", "endelement");
xml_set_character_data_handler($this->xml_parser, "characterdata");

if (!xml_parse($this->xml_parser, $data, true)) {
sprintf("xml error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml_parser)),
xml_get_current_line_number($this->xml_parser));
}

// we are done with the parser, so let's free it
xml_parser_free($this->xml_parser);

}//end constructor: function xml()


}//thus, we end our class xml

?>




操作方法:

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"];

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:PHP实现的功能是显示8条基色色带
· 下一篇:PHP画图的程序
· PHP 4.0 RC2 - Win32安装指南(中文版)
· 写的一个比较烂的目录文件列表程序,支持多系统,可按时间排序,可进入多层目录
· 在PHP3中实现SESSION的功能
· 多php服务器实现多session并发运行
· 几点提高php序运行效率的方法


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077