xml_fetch_array(); * * echo "
"; * * print_r($data); *"/> xml_fetch_array(); * * echo ""; * * print_r($data); *">xinputemulator PHP操作XML作為資料庫的類
- WBOY原創
- 2016-07-29 08:44:02835瀏覽
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_par🎜 >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>$c//條件數組<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>$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 class="brush:php;toolbar:false">"; <br>//print_r($true); <br>$true=0;/ /符合條件數歸零,進入下一輪迴圈<br>} <br>} else { <br>$result=$this->array; <br>} <br>//echo "<pre class="brush:php;toolbar:false">"; <br>//print_r($this->result); <br>return $result; <br>} <br>//篩選或統計<br>function xml_select($method,$condition,$if) <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 class="brush:php;toolbar:false">"; <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 class="brush:php;toolbar:false">"; <br>//print_r($datas); <br>$this->array=$datas; <br> $this->WriteXml($datas); }<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,"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><span><u>複製代碼</u></span> 程式碼如下:</p> <p><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><span><u>複製代碼</u></span> 程式碼如下:</p> <p>$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></p>刪除記錄<br> <br><p><span>複製程式碼<u></u> 程式碼如下:</span></p> <p>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><br></p>備註刪除時其實是把值變空,我們可以修改一下xml_update(),在產生xml檔之前先判斷$array的值,如果值為空就不寫入到最終的數組中就是刪除的效果了。也相當的快,挺適合中型網站生成XML時使用,所以推薦一下。 <br> 以上就介紹了xinputemulator PHP操作XML作為資料庫的類,包括了xinputemulator方面的內容,希望對PHP教程有興趣的朋友有所幫助。 <p> </p> <p></p></count></count>陳述:本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn