一个XML操作类(正则版)[转]

上一篇 / 下一篇  2007-12-20 15:05:28 / 个人分类:PHP开发

i R5a8Z2ZJqw I HE!u0<?phpPHPChina 开源社区门户V$p#cz'Tr{
/*PHPChina 开源社区门户`5nM.`W*O8F
*日期:2007-11-14
$j,y;Xj"MDLN0*功能:操作XML文件(增、删、改、查)
bm\M]cc:`0*版本:正则版
s_ai kY n?0*/
jCpCF'V0class OpXML
6? [P9T]Q9}:PXW0{PHPChina 开源社区门户~He+u/@0i
 private $fpath;PHPChina 开源社区门户;pA{g2zp+q1U
 private $enter;PHPChina 开源社区门户EC'f&F/mqf
 private $root;PHPChina 开源社区门户yiNjq@O/O
 function __construct($root,$fpath)PHPChina 开源社区门户%m L{2IR?r \Z
 {
$^],F^+Z!S'A`"[Z0  $this->fpath=$fpath;PHPChina 开源社区门户 Ds o^(]jl
  $this->root=$root;
.H-mvnN;z\k0  $this->enter=chr(13).chr(10);PHPChina 开源社区门户+c~$b1| Nd'W^
  $this->checkFile();
#H/u3{~ oUD0 }
r'gpJPOf0 /*PHPChina 开源社区门户TBF9V+~ ?)A:a$c
 *函数名:insertPHPChina 开源社区门户)T+t4gI9A
 *说明:插入一条记录PHPChina 开源社区门户DR"F:X1vb$N%D
 */PHPChina 开源社区门户w3E|R`P
 public function insert($fields)PHPChina 开源社区门户T%NmN&e/j8dlJ
 {
;^)b%J,@ ^;w0  $content=$this->getFileContent();
?z Dx/Q+G0  preg_match_all('|<key>(\d+?)<\/key>|',$content,$matches);PHPChina 开源社区门户R#`c [*R:J{ OL
  rsort($matches[1]);PHPChina 开源社区门户*}.W!O7X6f\Q
  $newkey=$matches[1][0]+1;PHPChina 开源社区门户.S BB BJb
 
Iah0zmxD2T0  $record='<record>'.$this->enter;PHPChina 开源社区门户oa0`*Z2n
  $record.='<key>'.$newkey.'</key>'.$this->enter;PHPChina 开源社区门户#p@7s$G I |r!R8c `
  foreach($fields as $k=>$v)PHPChina 开源社区门户%D(n p4r3R1\
  {PHPChina 开源社区门户([2v!p.wvT0L
   $record.="<$k>$v</$k>".$this->enter;PHPChina 开源社区门户;O[a3B;FC
  }PHPChina 开源社区门户TSrV`e/i
  $record.='</record>'.$this->enter.$this->enter;
8Y};U7U%]"m8TqY0  $this->save(preg_replace('/(?=<\/'.$this->root.'>)/',$record,$content));PHPChina 开源社区门户!G9S1B1@;hX
  return true;PHPChina 开源社区门户"a\GA8c ^-tJ
 }
,y6ohCI5U*z0O0 /*
!B5x;h]8_0 *函数名:checkFile
Deo5`C3}h/L0 *说明:如果文件不存在,则创建之,并初始化PHPChina 开源社区门户 Y$m~p-b5y
 *     否则检查文件规则是否被破坏PHPChina 开源社区门户~Z&j"Q%UlK.t$N]
 */
+n5V:h'?QV\4W-gu0 private function checkFile()
c4nC7V!]zO!YU0 {
|P[gN$y:L v2I0  if(!file_exists($this->fpath))PHPChina 开源社区门户 [S3EhX%t2AM$t
  {PHPChina 开源社区门户4j*Zi4Ke8d
   $xmlstr='';
_YFODi_0   $xmlstr='<?xml version="1.0" encoding="UTF-8"?>'.$this->enter;
Q%hg~@+\0   $xmlstr.='<'.$this->root.'>'.$this->enter.$this->enter;PHPChina 开源社区门户-F$v%i:Fu5?|
   $xmlstr.='</'.$this->root.'>';
h4K vMQ B,l(S)uj0   $this->save($xmlstr);PHPChina 开源社区门户'xL$aq%K9m
  }
#XVD U_2|TX0  else
/S ?#OS&}+`9y(\0  {PHPChina 开源社区门户&_*y r ~L-Zy Okzlq
   $content=$this->getFileContent();
R4p])v(Zr0   $bool_statement=preg_match('/<\?xml version="1\.0".*?\?>/',$content)==0 ? false : true;
y7_!w n!gI ?0   $bool_root=preg_match('/<'.$this->root.'>.*<\/'.$this->root.'>/s',$content)==0 ? false : true;
p+R&u;Y,S/] lq0   preg_match_all('|(<record>(?:.+?)<\/record>)|s',$content,$matches);
m:BGM#OH_0   for($i=0;$i<count($matches[1]);$i++)PHPChina 开源社区门户i;sa4xTQ @Gf8J
   {PHPChina 开源社区门户P2E0Gc(s I
    $re='/^<record>\s*<key>(\d+)<\/key>\s*[\s\S]*\s*<\/record>$/';PHPChina 开源社区门户,zJ'_Y[6y6B
    $bool_record=preg_match($re,$matches[1][$i],$arr)==0 ? false : true;PHPChina 开源社区门户{{+nS[#J z T
    $keys[]=$arr[1];PHPChina 开源社区门户1J5z@+T1_8sk9S
    if(!$bool_record) break;
Pr8d3Gbk0   }PHPChina 开源社区门户BK~+A|I`X)@#V
   rsort($keys);PHPChina 开源社区门户1c,lp6We |oi1~8l|
   $bool_repeat=preg_match('/(\d),\1/',join(',',$keys),$wb)==0 ? true : false;
f eVT9A]M-aO9T0   if(!($bool_statement && $bool_root && $bool_record && $bool_repeat))PHPChina 开源社区门户!E/vpn Mp5S0D
   {
k9E,gNjK M0    echo '文件规则已被破坏';
rj\T8gF[C0    exit;PHPChina 开源社区门户q8R6@b|1@'j;o3bJ
   }
OG~ XJ&C0  }PHPChina 开源社区门户y @ ?%dD/Tz
 }PHPChina 开源社区门户-i.td \t!Y9nR
 /*PHPChina 开源社区门户(k&T(K}WQ2JAo
 *函数名:getRecordById
nLj@ T ~0 *说明:根据主键ID,获取字段信息PHPChina 开源社区门户2Z/S'Pt;dL,t5zu;X&a
 */
/b+Uo eD#F,H,d0 public function getRecordById($id)PHPChina 开源社区门户Mm5[lf
 {
[p4nDE0  $content=$this->getFileContent();PHPChina 开源社区门户/E HhU ]NY
  preg_match('/<record>(\s*<key>'.$id.'<\/key>.+?)<\/record>/s',$content,$matches);PHPChina 开源社区门户q}:_#\#f)a.T
  preg_match_all('|(<.+</.+>)|',$matches[1],$arr);
8g_8a7\|0  $arrstr='';
{,d v!O/i5j0  for($i=0;$i<count($arr[1]);$i++)PHPChina 开源社区门户 K`C5? a1S\
  {
pn#oZ6}j0   preg_match('/^.+>(.+)<\/(.+)>/',$arr[1][$i],$farr);
g tS:X._NSjO _-Y B0   $arrstr.="'$farr[2]'=>'$farr[1]'";PHPChina 开源社区门户 XD*K\G-gJ-ej"["g`
   if($i<count($arr[1])-1) $arrstr.=',';
8n*Y g~:HI0  }PHPChina 开源社区门户"tP3t4T/rF
  eval("\$row=array($arrstr);");PHPChina 开源社区门户0v T:i2e E lI4{#@kL
  return $row;PHPChina 开源社区门户-N*~8g"qc
 }
/M$D^#@(^0 /*
e#hq@ IUI!iI0 *函数名:checkFilePHPChina 开源社区门户,H7Q sr9a,R9J8g,r%W
 *说明:根据主键ID,更新字段PHPChina 开源社区门户7E9T;]/bmb.CR
 */
|.b&z;_$f8[7I _0 public function updateRecordById($id,$form_arr=array())PHPChina 开源社区门户&Qc|'UT1z7w
 {PHPChina 开源社区门户2|Qvd*X-Y3L
  $content=$this->getFileContent();PHPChina 开源社区门户-A+Iq!B8ix1bu
  foreach($form_arr as $k=>$v)PHPChina 开源社区门户oO4njYU
  {PHPChina 开源社区门户p?$s R i$rH_kP l8|
   $re='/(<key>'.$id.'<\/key>[\s\S]*?<'.$k.'>).+?(<\/'.$k.'>)/s';
'S^$EjI"\ox9~9Y0   $content=preg_replace($re,'${1}'.$v.'$2',$content);
N4H:P/a z:O$^M)[0  }
4|z:O2M,ay9NrU ?0  $this->save($content);PHPChina 开源社区门户d.`/tt8o XX&r
  return true;PHPChina 开源社区门户1fth!].t(v._o
 }
i7q!kO;Qw%D M0 public function deleteRecordById($id)PHPChina 开源社区门户 IHr }e_J.|
 {
:V1BC3y Y/bl0  $content=$this->getFileContent();
3r a0HJj*OG0  $content=preg_replace('/<record>\s*<key>'.$id.'+.+?<\/record>(\s{4})?/s','',$content);PHPChina 开源社区门户u;tG3d:@.l3n5l?+wu{
  $this->save($content);PHPChina 开源社区门户/ly |^-ek
 }
.B&p'G2A;@0 /*PHPChina 开源社区门户 ~h:H:f8cSA
 *函数名:getListPHPChina 开源社区门户K ~*U!w.Je UG
 *说明:获取所有记录,返回一个二维数组
U(yp pj;G0 *     先获取所有record记录,再获取字段名和值
Z Fa\.HkP0 */PHPChina 开源社区门户q?kY/ox6i1p
 public function getList()PHPChina 开源社区门户O%^O,bX^2x \f:?$Z U\
 {
3lx NA"`u;bC:I*o0  $content=$this->getFileContent();PHPChina 开源社区门户(w/C%J1WYW
  $rs=array();PHPChina 开源社区门户%aaoy+F:bU
  preg_match_all('|<record>(.+?)<\/record>|s',$content,$matches);
S:Z8n8f)ok0  for($i=0;$i<count($matches[1]);$i++)PHPChina 开源社区门户 n-DhDg+Ui)C
  {
q x(r,|/t`f0   preg_match_all('|(<.+</.+>)|',$matches[1][$i],$tmparr);
^q,M]OS0   $arrstr='';PHPChina 开源社区门户XqW-|$sA/I{
   for($j=0;$j<count($tmparr[1]);$j++)PHPChina 开源社区门户/?5wZ6O:xUw
   {
A0GFt1Cs0    preg_match('/^.+>(.+)<\/(.+)>/',$tmparr[1][$j],$farr);
"M5@(Pt0VgX^O0    $arrstr.="'$farr[2]'=>'$farr[1]'";PHPChina 开源社区门户ta`p Rn
    if($j<count($tmparr[1])-1) $arrstr.=',';PHPChina 开源社区门户IJy/g-c~V8c'U{
   }PHPChina 开源社区门户 C$T*k+z;[+_QO!b|
   eval("\$rs[]=array($arrstr);");PHPChina 开源社区门户Vh3?BszF
  }PHPChina 开源社区门户ccQ(k"x
  return $rs;PHPChina 开源社区门户5?u{d"q&E0o4aM5N8y
 }PHPChina 开源社区门户6X$sR?VO2]/r
 private function getFileContent()PHPChina 开源社区门户1V ``z3XWv
 {PHPChina 开源社区门户D:N1r,CE j o
  $hd=fopen($this->fpath,'r');
+b Aq l2L3j#J0  $content=fread($hd,filesize($this->fpath));
&r/xl`i9qZ.j1B0  fclose($hd);
mZF+y6y0  return $content;
-BIWpF*}0 }PHPChina 开源社区门户gy-C9kK
 private function save($content)PHPChina 开源社区门户tSd?Q9E;mZf
 {
q*U{8@$Fo n0  $hd=fopen($this->fpath,'w');PHPChina 开源社区门户+{ |$Q2p}T
  fwrite($hd,$content);
W~%pS3cWOw yuS3Q0  fclose($hd);PHPChina 开源社区门户 E\.oU3|1`6Lm0N5v _
 }
DD)Mx$I@I0}
] No d)H.@0?>
+|}/v"bz(^0PHPChina 开源社区门户zo4DT"l

PHPChina 开源社区门户McN-V6R0v8e,?

 PHPChina 开源社区门户F%U!Vmh7xToYc0M

{9]f't:{0应用示例:
{8Z#Q!l^!f*\0header('content-type: text/html; charset=utf-8');PHPChina 开源社区门户4T0q4b0x2U
include('./libs/x.class.php');
"O ] A)Qs}0$rootname='ROOT';//根标签名
)oA` \G7@[q,K0$fpath='./libs/ooooooooooooooo.xml';//文件的路径,不用手动创建文件
`M"q'V_y~0$x=new OpXML('ROOT',$fpath);
W$Qp P/s7Z,rc o0//增
v6Up [)K8];w3o0$arr=array('username'=>'pangmen','sex'=>'男','tel'=>'139999999','address'=>'北京');
r"cZGP \M F6t0$x->insert($arr);PHPChina 开源社区门户Q/D#i}U x?
//删PHPChina 开源社区门户IzpQ mD2E
//$x->deleteRecordById(1);PHPChina 开源社区门户I*w2N9|6qY
//改
Kpw#dA@mx0//$arr=array('username'=>'pangmen','sex'=>'男','tel'=>'139999999','address'=>'北京');PHPChina 开源社区门户%@8{%MQ8J8\
//$x->updateRecordById(1,$arr);
6Wk3?q:m0//查(一条记录)PHPChina 开源社区门户Um5Jd q,VmD
//print_r($x->getRecordById(1));PHPChina 开源社区门户S1d.f2q-}6{ }7ZTd
//查(所有记录)
)^/r:QC6w:J9gB0//print_r($x->getList());PHPChina 开源社区门户*M2[_)wyS?


TAG: XML 正则 PHP开发

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

我的存档

数据统计

  • 访问量: 204
  • 日志数: 3
  • 建立时间: 2007-12-20
  • 更新时间: 2007-12-20

RSS订阅

Open Toolbar