xml.class.php文件代码
<BR><?p<strong style="color:transparent">本文来源gaodai#ma#com搞@@代~&码*网/</strong><strong>搞gaodaima代码</strong>hp <BR>* example 读取数据: <BR>* <BR>* $xml = new xml("dbase.xml",'table'); <BR>* <BR>* $data=$xml->xml_fetch_array(); <BR>* <BR>* echo "<pre style="font-size:12px">"; <BR>* <BR>* print_r($data); <BR>* <BR>class xml <BR>{ <BR>var $dbase; //数据库,要读取的XML文件 <BR>var $dbname; //数据库名称,顶层元素,与数据库文件名称一致 <BR>var $dbtable; //数据表,要取得的节点 <BR>var $parser; //剖析器 <BR>var $vals; //属性 <BR>var $index; //索引 <BR>var $dbtable_array;//节点数组 <BR>var $array; //下级节点的数组 <BR>var $result; //返回的结果 <BR>var $querys; <BR>function xml($dbase,$dbtable) <BR>{ <BR>$this->dbase=$dbase; <BR>$this->dbname=substr($dbase,strrpos($dbase,"/")+1,-4); <BR>$this->dbtable=$dbtable; <BR>$data=$this->ReadXml($this->dbase); <BR>if(!$data){ <BR>die("无法读取 $this->dbname.xml"); <BR>} <BR>$this->parser = xml_parser_create(); <BR>xml_parser_set_option($this->parser,XML_OPTION_CASE_FOLDING,0); <BR>xml_parser_set_option($this->parser,XML_OPTION_SKIP_WHITE,1); <BR>xml_parse_into_struct($this->parser,$data,$this->vals,$this->index); <BR>xml_parser_free($this->parser); <BR>//遍历索引,筛选出要取值的节点 节点名:$dbtable <BR>foreach ($this->index as $key=>$val) { <BR>if ($key == $this->dbtable) { <BR>//取得节点数组 <BR>$this->dbtable_array = $val; <BR>} else { <BR>continue; <BR>} <BR>} <BR>for ($i=0; $i dbtable_array); $i+=2) { <BR>$offset = $this->dbtable_array[$i] + 1; <BR>$len = $this->dbtable_array[$i + 1] - $offset; <BR>//array_slice() 返回根据 offset 和 length 参数所指定的 array 数组中的一段序列。 <BR>//所取节点下级数组 <BR>$value=array_slice($this->vals,$offset,$len); <BR>//取得有效数组,合并为结果数组 <BR>$this->array[]=$this->parseEFF($value); <BR>} <BR>return true; <BR>} <BR>//将XML文件读入并返回字符串 <BR>function ReadXml($file) <BR>{ <BR>return file_get_contents($file); <BR>} <BR>//取得有效数组 <BR>function parseEFF($effective) { <BR>for ($i=0; $i < count($effective); $i++){ <BR>$effect[$effective[$i]["tag"]] = $effective[$i]["value"]; <BR>} <BR>return $effect; <BR>} <BR>//xml_query(方法,条件,多条件时逻辑运算符and or or,插入或更新的数组) <BR>function xml_query($method,$condition,$if='and',$array=array()) <BR>{ <BR>if(($method=='select')||($method=='count')){ <BR>return $this->xml_select($method,$condition,$if); <BR>} elseif($method=='insert') { <BR>return $this->xml_insert($condition,$if,$array); <BR>} elseif($method=='update') { <BR>return $this->xml_update($condition,$if,$array); <BR>} <BR>} <BR>//取得xml数组 <BR>function xml_fetch_array($condition,$if) <BR>{ <BR>//$this->querys++; <BR>$row = $this->array; //初始化数据数组 <BR>if($condition) { <BR>//是否有条件,如有条件则生成符合条件的数组 <BR>//生成条件数组,条件格式 field,operator,match <BR>$condition=explode(",",$condition);//条件数组 <BR>$cs=count($condition)/3; //条件数 <BR>for($i=0;$i<$cs;$i++){ <BR>$conditions[]=array("field"=>$condition[$i*3],"operator"=>$condition[$i*3+1],"match"=>$condition[$i*3+2]); <BR>} <BR>//echo count($row); <BR>for($r=0;$r<count($row);$r++){ <BR>for($c=0;$c<$cs;$c++){ <BR>//$i++; <BR>$condition=$conditions[$c]; //当前条件 <BR>$field=$condition['field']; //字段 <BR>$operator=$condition["operator"];//运算符 <BR>$match=$condition['match']; //匹配 <BR>if(($operator=='=')&&($row[$r][$field]==$match)){ <BR>$true++;//若条件符合,符合数加1 <BR>} elseif(($operator=='!=')&&($row[$r][$field]!=$match)){ <BR>$true++;//若条件符合,符合数加1 <BR>} elseif(($operator=='<')&&($row[$r][$field]<$match)){ <BR>$true++;//若条件符合,符合数加1 <BR>} elseif(($operator=='<=')&&($row[$r][$field]<=$match)){ <BR>$true++;//若条件符合,符合数加1 <BR>} elseif(($operator=='>')&&($row[$r][$field]>$match)){ <BR>$true++;//若条件符合,符合数加1 <BR>} elseif(($operator=='>')&&($row[$r][$field]>=$match)){ <BR>$true++;//若条件符合,符合数加1 <BR>} <BR>} <BR>//根据条件取值 <BR>if($if=='and'){ <BR>//如果多条件为and,当符合数等于条件数时,生成数组 <BR>if($true==$cs){ <BR>$result[]=$row[$r]; <BR>} <BR>} else { <BR>//如果多条件为or,当有符合纪录时,生成数组 <BR>if($true!=0){ <BR>$result[]=$row[$r]; <BR>} <BR>} <BR>//echo $true; <BR>//echo "<pre style="font-size:12px;text-align:left">"; <BR>//print_r($true); <BR>$true=0;//符合条件数归零,进入下一轮循环 <BR>} <BR>} else { <BR>$result=$this->array; <BR>} <BR>//echo "<pre style="font-size:12px;text-align:left">"; <BR>//print_r($this->result); <BR>return $result; <BR>} <BR>//筛选或统计 <BR>function xml_select($method,$condition,$if) <BR>{ <BR>$result=$this->xml_fetch_array($condition,$if); <BR>if($method=='select'){ <BR>return $result; <BR>} else { <BR>return count($result); <BR>} <BR>} <BR>//插入数据 <BR>function xml_insert($condition,$if,$array) <BR>{ <BR>$data=$this->xml_fetch_array($condition,$if);//总数据数组 <BR>$data[]=$array; //插入后的总数据数组 <BR>$this->array=$data; //更新总数组 <BR>$this->WriteXml($data); <BR>} <BR>//得到更新的XML并改写 <BR>function xml_update($condition,$if,$array) <BR>{ <BR>$datas=$this->array; //总数据数组 <BR>$subtract=$this->xml_fetch_array($condition,$if);//要更新的数组 <BR>//echo "<pre style="font-size:12px;text-align:left">"; <BR>//print_r($data); <BR>//print_r($datas); <BR>//echo "每条记录中有".count($datas[0])."个值<br>"; <BR>for($i=0;$i<count($datas);$i++){ <BR>$data=$datas[$i]; <BR>//echo "原始记录中的第".$i."条<br>"; <BR>foreach($data as $k=>$v){ <BR>//echo "-第".$i."条的".$k."值为".$v."<br>"; <BR>//echo "--要查找的数组".$k."值为".$subtract[0][$k]."<br>"; <BR>if($v==$subtract[0][$k]){ <BR>$is++; <BR>} <BR>} <BR>if($is==count($data)){ <BR>//echo "----与第".$i."条符合<br>"; <BR>$datas[$i]=$array; <BR>//array_splice($datas,$i,$i+1); <BR>} <BR>//echo "原始记录中的第".$i."条与要查找的有".$is."匹配<br>"; <BR>//echo "原始记录中的第".$i."条结束<br>"; <BR>$is=0; <BR>} <BR>//array_splice($datas,2,2+1,$array); <BR>//echo "<pre style="font-size:12px;text-align:left">"; <BR>//print_r($datas); <BR>$this->array=$datas; <BR>$this->WriteXml($datas); <BR>} <BR>//写入XML文件(全部写入) <BR>function WriteXml($array) <BR>{ <BR>if(!is_writeable($this->dbase)){ <BR>die("无法写入".$this->dbname.".xml"); <BR>} <BR>$xml.="<?xml version="1.0" encoding="utf-8"?>rn"; <BR>$xml.="dbname>rn"; <BR>for($i=0;$i<count($array);$i++){ <BR>$xml.="dbtable>rn"; <BR>foreach($array[$i] as $k=>$s){ <BR>$xml.="$srn"; <BR>} <BR>$xml.="dbtable>rn"; <BR>} <BR>$xml.="dbname>"; <BR>$fp=@fopen($this->dbase,"w"); <BR>flock($fp, LOCK_EX); <BR>rewind($fp); <BR>fputs($fp,$xml); <BR>fclose($fp); <BR>} <BR>//逐行写入xml(我试着写入10000行,感觉没一次写入快,所以没用这种写入方式) <BR>function WriteLine($array) <BR>{ <BR>if(!is_writeable($this->dbase)){ <BR>die("无法写入".$this->dbname.".xml"); <BR>} <BR>$fp=@fopen($this->dbase,"w"); <BR>rewind($fp); <BR>flock($fp, LOCK_EX); <BR>fputs($fp,"<?xml version="1.0" encoding="utf-8"?>rn"); <BR>fputs($fp,"dbname>rn"); <BR>for($i=0;$i<count($array);$i++){ <BR>fputs($fp,"dbtable>rn"); <BR>$xml.="dbtable>rn"; <BR>foreach($array[$i] as $k=>$s){ <BR>fputs($fp,"$srn"); <BR>} <BR>fputs($fp,"dbtable>rn"); <BR>} <BR>fputs($fp,"dbname>"); <BR>fclose($fp); <BR>} <BR>} <BR>?> <BR>
使用方法: 插入一条记录
<BR>require_once('xml.class.php'); <BR>$xml = new xml("exemple.xml","item"); <BR>$newarray = array( <BR>"title"=>"XML标题", <BR>"text"=>"PHP的XML类测试!" <BR>); <BR>$insert=$xml->xml_query('insert','','',$newarray);//第二及第三个变量位置是条件,留空表示在最后插入 <BR>
修改记录
<BR>require_once('xml.class.php'); <BR>$xml = new xml("exemple.xml","item"); <BR>$array = array( <BR>"title"=>"XML标题", <BR>"text"=>"PHP的XML类测试!" <BR>); <BR>$insert=$xml->xml_query('update','title,=,20年后世界将会怎样?','and',$array);//title标签等于xxx的用$array替换(可以建唯一属性的标签,比如id,这样就可以修改某一条记录) <BR>
删除记录
<BR>require_once('xml.class.php'); <BR>$xml = new xml("exemple.xml","item"); <BR>$array = array(); <BR>$insert=$xml->xml_query('update','title,=,20年后世界将会怎样?','and',$array);//数组留空 <BR>
备注 删除时其实是把值变空,我们可以修改一下xml_update(),在生成xml文件之前先判断$array的值,如果值为空就不写入到最终的数组中就是删除的效果了。 写入xml文件时速度粉快(我测试过30000条记录的情况),插入时只插入一条记录,修改速度也相当的快,挺适合中型网站生成XML时使用,所以推荐一下。