天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

PHP操作XML作為數(shù)據(jù)庫的類

xml.class.php文件代碼
復(fù)制代碼 代碼如下:
<?php
* example 讀取數(shù)據(jù):
*
* $xml = new xml("dbase.xml",'table');
*
* $data=$xml->xml_fetch_array();
*
* echo "<pre style="font-size:12px;">";
*
* print_r($data);
*
class xml
{
var $dbase; //數(shù)據(jù)庫,要讀取的XML文件
var $dbname; //數(shù)據(jù)庫名稱,頂層元素,與數(shù)據(jù)庫文件名稱一致
var $dbtable; //數(shù)據(jù)表,要取得的節(jié)點(diǎn)
var $parser; //剖析器
var $vals; //屬性
var $index; //索引
var $dbtable_array;//節(jié)點(diǎn)數(shù)組
var $array; //下級(jí)節(jié)點(diǎn)的數(shù)組
var $result; //返回的結(jié)果
var $querys;
function xml($dbase,$dbtable)
{
$this->dbase=$dbase;
$this->dbname=substr($dbase,strrpos($dbase,"/")+1,-4);
$this->dbtable=$dbtable;
$data=$this->ReadXml($this->dbase);
if(!$data){
die("無法讀取 $this->dbname.xml");
}
$this->parser = xml_parser_create();
xml_parser_set_option($this->parser,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($this->parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct($this->parser,$data,$this->vals,$this->index);
xml_parser_free($this->parser);
//遍歷索引,篩選出要取值的節(jié)點(diǎn) 節(jié)點(diǎn)名:$dbtable
foreach ($this->index as $key=>$val) {
if ($key == $this->dbtable) {
//取得節(jié)點(diǎn)數(shù)組
$this->dbtable_array = $val;
} else {
continue;
}
}
for ($i=0; $i < count($this->dbtable_array); $i+=2) {
$offset = $this->dbtable_array[$i] + 1;
$len = $this->dbtable_array[$i + 1] - $offset;
//array_slice() 返回根據(jù) offset 和 length 參數(shù)所指定的 array 數(shù)組中的一段序列。
//所取節(jié)點(diǎn)下級(jí)數(shù)組
$value=array_slice($this->vals,$offset,$len);
//取得有效數(shù)組,合并為結(jié)果數(shù)組
$this->array[]=$this->parseEFF($value);
}
return true;
}
//將XML文件讀入并返回字符串
function ReadXml($file)
{
return file_get_contents($file);
}
//取得有效數(shù)組
function parseEFF($effective) {
for ($i=0; $i < count($effective); $i++){
$effect[$effective[$i]["tag"]] = $effective[$i]["value"];
}
return $effect;
}
//xml_query(方法,條件,多條件時(shí)邏輯運(yùn)算符and or or,插入或更新的數(shù)組)
function xml_query($method,$condition,$if='and',$array=array())
{
if(($method=='select')||($method=='count')){
return $this->xml_select($method,$condition,$if);
} elseif($method=='insert') {
return $this->xml_insert($condition,$if,$array);
} elseif($method=='update') {
return $this->xml_update($condition,$if,$array);
}
}
//取得xml數(shù)組
function xml_fetch_array($condition,$if)
{
//$this->querys++;
$row = $this->array; //初始化數(shù)據(jù)數(shù)組
if($condition) {
//是否有條件,如有條件則生成符合條件的數(shù)組
//生成條件數(shù)組,條件格式 field,operator,match
$condition=explode(",",$condition);//條件數(shù)組
$cs=count($condition)/3; //條件數(shù)
for($i=0;$i<$cs;$i++){
$conditions[]=array("field"=>$condition[$i*3],"operator"=>$condition[$i*3+1],"match"=>$condition[$i*3+2]);
}
//echo count($row);
for($r=0;$r<count($row);$r++){
for($c=0;$c<$cs;$c++){
//$i++;
$condition=$conditions[$c]; //當(dāng)前條件
$field=$condition['field']; //字段
$operator=$condition["operator"];//運(yùn)算符
$match=$condition['match']; //匹配
if(($operator=='=')&&($row[$r][$field]==$match)){
$true++;//若條件符合,符合數(shù)加1
} elseif(($operator=='!=')&&($row[$r][$field]!=$match)){
$true++;//若條件符合,符合數(shù)加1
} elseif(($operator=='<')&&($row[$r][$field]<$match)){
$true++;//若條件符合,符合數(shù)加1
} elseif(($operator=='<=')&&($row[$r][$field]<=$match)){
$true++;//若條件符合,符合數(shù)加1
} elseif(($operator=='>')&&($row[$r][$field]>$match)){
$true++;//若條件符合,符合數(shù)加1
} elseif(($operator=='>')&&($row[$r][$field]>=$match)){
$true++;//若條件符合,符合數(shù)加1
}
}
//根據(jù)條件取值
if($if=='and'){
//如果多條件為and,當(dāng)符合數(shù)等于條件數(shù)時(shí),生成數(shù)組
if($true==$cs){
$result[]=$row[$r];
}
} else {
//如果多條件為or,當(dāng)有符合紀(jì)錄時(shí),生成數(shù)組
if($true!=0){
$result[]=$row[$r];
}
}
//echo $true;
//echo "<pre style="font-size:12px;text-align:left">";
//print_r($true);
$true=0;//符合條件數(shù)歸零,進(jìn)入下一輪循環(huán)
}
} else {
$result=$this->array;
}
//echo "<pre style="font-size:12px;text-align:left">";
//print_r($this->result);
return $result;
}
//篩選或統(tǒng)計(jì)
function xml_select($method,$condition,$if)
{
$result=$this->xml_fetch_array($condition,$if);
if($method=='select'){
return $result;
} else {
return count($result);
}
}
//插入數(shù)據(jù)
function xml_insert($condition,$if,$array)
{
$data=$this->xml_fetch_array($condition,$if);//總數(shù)據(jù)數(shù)組
$data[]=$array; //插入后的總數(shù)據(jù)數(shù)組
$this->array=$data; //更新總數(shù)組
$this->WriteXml($data);
}
//得到更新的XML并改寫
function xml_update($condition,$if,$array)
{
$datas=$this->array; //總數(shù)據(jù)數(shù)組
$subtract=$this->xml_fetch_array($condition,$if);//要更新的數(shù)組
//echo "<pre style="font-size:12px;text-align:left">";
//print_r($data);
//print_r($datas);
//echo "每條記錄中有".count($datas[0])."個(gè)值<br>";
for($i=0;$i<count($datas);$i++){
$data=$datas[$i];
//echo "原始記錄中的第".$i."條<br>";
foreach($data as $k=>$v){
//echo "-第".$i."條的".$k."值為".$v."<br>";
//echo "--要查找的數(shù)組".$k."值為".$subtract[0][$k]."<br>";
if($v==$subtract[0][$k]){
$is++;
}
}
if($is==count($data)){
//echo "----與第".$i."條符合<br>";
$datas[$i]=$array;
//array_splice($datas,$i,$i+1);
}
//echo "原始記錄中的第".$i."條與要查找的有".$is."匹配<br>";
//echo "原始記錄中的第".$i."條結(jié)束<br>";
$is=0;
}
//array_splice($datas,2,2+1,$array);
//echo "<pre style="font-size:12px;text-align:left">";
//print_r($datas);
$this->array=$datas;
$this->WriteXml($datas);
}
//寫入XML文件(全部寫入)
function WriteXml($array)
{
if(!is_writeable($this->dbase)){
die("無法寫入".$this->dbname.".xml");
}
$xml.="<?xml version="1.0" encoding="utf-8"?>rn";
$xml.="<$this->dbname>rn";
for($i=0;$i<count($array);$i++){
$xml.="<$this->dbtable>rn";
foreach($array[$i] as $k=>$s){
$xml.="<$k>$s</$k>rn";
}
$xml.="</$this->dbtable>rn";
}
$xml.="</$this->dbname>";
$fp=@fopen($this->dbase,"w");
flock($fp, LOCK_EX);
rewind($fp);
fputs($fp,$xml);
fclose($fp);
}
//逐行寫入xml(我試著寫入10000行,感覺沒一次寫入快,所以沒用這種寫入方式)
function WriteLine($array)
{
if(!is_writeable($this->dbase)){
die("無法寫入".$this->dbname.".xml");
}
$fp=@fopen($this->dbase,"w");
rewind($fp);
flock($fp, LOCK_EX);
fputs($fp,"<?xml version="1.0" encoding="utf-8"?>rn");
fputs($fp,"<$this->dbname>rn");
for($i=0;$i<count($array);$i++){
fputs($fp,"<$this->dbtable>rn");
$xml.="<$this->dbtable>rn";
foreach($array[$i] as $k=>$s){
fputs($fp,"<$k>$s</$k>rn");
}
fputs($fp,"</$this->dbtable>rn");
}
fputs($fp,"</$this->dbname>");
fclose($fp);
}
}
?>

使用方法: 插入一條記錄
復(fù)制代碼 代碼如下:
require_once('xml.class.php');
$xml = new xml("exemple.xml","item");
$newarray = array(
"title"=>"XML標(biāo)題",
"text"=>"php的XML類測試!"
);
$insert=$xml->xml_query('insert','','',$newarray);//第二及第三個(gè)變量位置是條件,留空表示在最后插入

修改記錄
復(fù)制代碼 代碼如下:
require_once('xml.class.php');
$xml = new xml("exemple.xml","item");
$array = array(
"title"=>"XML標(biāo)題",
"text"=>"php的XML類測試!"
);
$insert=$xml->xml_query('update','title,=,20年后世界將會(huì)怎樣?','and',$array);//title標(biāo)簽等于xxx的用$array替換(可以建唯一屬性的標(biāo)簽,比如id,這樣就可以修改某一條記錄)

刪除記錄
復(fù)制代碼 代碼如下:
require_once('xml.class.php');
$xml = new xml("exemple.xml","item");
$array = array();
$insert=$xml->xml_query('update','title,=,20年后世界將會(huì)怎樣?','and',$array);//數(shù)組留空

備注   刪除時(shí)其實(shí)是把值變空,我們可以修改一下xml_update(),在生成xml文件之前先判斷$array的值,如果值為空就不寫入到最終的數(shù)組中就是刪除的效果了。  寫入xml文件時(shí)速度粉快(我測試過30000條記錄的情況),插入時(shí)只插入一條記錄,修改速度也相當(dāng)?shù)目欤m合中型網(wǎng)站生成XML時(shí)使用,所以推薦一下。

php技術(shù)PHP操作XML作為數(shù)據(jù)庫的類,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国语对白老女人8av 国语对白刺激真实精品 | 青青草原网址 | 动漫美女无衣 | 色屁屁影院 | 美女的隐私蜜桃传媒免费看 | 被滋润的艳妇疯狂呻吟白洁老七 | 亚洲精品永久免费 | 男人日女人的b | 国产亚洲精品久久久无码狼牙套 | 大地影院在线播放 | 亚洲成人一区 | 成人性生交大片免费看中文 | 纯肉宠文高h一对一 | 国产午夜人做人免费视频中文 | 99E久热只有精品8在线直播 | 强壮的公次次弄得我高潮韩国电影 | 国产人成高清在线视频99 | 乱码中字在线观看一二区 | a级毛片高清免费视频 | 久久99re2在线视频精品 | 再深点灬舒服灬太大了在线视频 | 7756短视频 | 国产毛片AV久久久久精品 | 亚洲欧美一级久久精品 | 美女叉腿掰阴大胆艺术照 | 午夜福利体检 | 青青久在线| 亚洲乱亚洲乱妇在线观看 | 亚洲日本乱码中文论理在线电影 | qvod在线电影 | 琪琪色原网站ying | 亲胸揉胸膜下刺激视频在线观看 | 冈本视频黄页正版 | 久久学生精品国产自在拍 | 日本aa大片 | 国产爱豆果冻传媒在线观看视频 | 出租屋自拍贵在真实15P | 国产九九熟女在线视频 | 天天爽夜夜爽 | MMM日本兽交| 黄色jjzz |