PHP操作XML作为数据库的类
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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
