>  기사  >  php教程  >  PHP操作XML作为数据库的类

PHP操作XML作为数据库的类

WBOY
WBOY원래의
2016-06-13 12:12:20829검색

xml.class.php文件代码

复制代码 代码如下:


* example 读取数据:
*
* $xml = new xml("dbase.xml",'table');
*
* $data=$xml->xml_fetch_array();
*
* echo "

"; <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 $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$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>for($c=0;$c//$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=='$true++;//若条件符合,符合数加1 <br>} elseif(($operator=='$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>$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>$xml.="dbtable>rn"; <br>foreach($array[$i] as $k=>$s){ <br>$xml.="$s$k>rn"; <br>} <br>$xml.="$this->dbtable>rn"; <br>} <br>$xml.="$this->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>fputs($fp,"dbtable>rn"); <br>$xml.="dbtable>rn"; <br>foreach($array[$i] as $k=>$s){ <br>fputs($fp,"$s$k>rn"); <br>} <br>fputs($fp,"$this->dbtable>rn"); <br>} <br>fputs($fp,"$this->dbname>"); <br>fclose($fp); <br>} <br>} <br>?> <br> <br>使用方法: 插入一条记录 <br><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code29303')"><u>复制代码</u></span> 代码如下:</p>
<p class="codebody" id="code29303"> <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></p> <br>修改记录 <br><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code7277')"><u>复制代码</u></span> 代码如下:</p>
<p class="codebody" id="code7277"> <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></p> <br>删除记录 <br><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code46798')"><u>复制代码</u></span> 代码如下:</p>
<p class="codebody" id="code46798"> <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></p> <br>备注   删除时其实是把值变空,我们可以修改一下xml_update(),在生成xml文件之前先判断$array的值,如果值为空就不写入到最终的数组中就是删除的效果了。  写入xml文件时速度粉快(我测试过30000条记录的情况),插入时只插入一条记录,修改速度也相当的快,挺适合中型网站生成XML时使用,所以推荐一下。

</count></count>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.