Home  >  Article  >  Backend Development  >  PHP operates XML as a database class_PHP tutorial

PHP operates XML as a database class_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:32:46712browse

xml.class.php file code

Copy code The code is as follows:

* example read data:
*
* $xml = new xml("dbase.xml",'table');
*
* $data=$xml->xml_fetch_array();
*
* echo "
"; 
*
* print_r ($data);
*
class xml
{
var $dbase; //Database, XML file to be read
var $dbname; //Database name, top-level element, Consistent with the name of the database file
var $dbtable; //Data table, node to be obtained
var $parser; //Parser
var $vals; //Attributes
var $index; / /Index
var $dbtable_array;//Node array
var $array; //Array of subordinate nodes
var $result; //Returned results
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("Cannot read $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);
//Traverse the index and filter out the node name of the node to be valued: $dbtable
foreach ($this->index as $key=> ;$val) {
if ($key == $this->dbtable) {
//Get the node array
$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() returns the slice specified by the offset and length parameters array A sequence in an array.
//The lower-level array of the obtained node
$value=array_slice($this->vals,$offset,$len);
//Get the valid array and merge it into the result array
$this ->array[]=$this->parseEFF($value);
}
return true;
}
//Read the XML file and return the string
function ReadXml($file)
{
return file_get_contents($file);
}
//Get effective array
function parseEFF($effective) {
for ($i=0 ; $i < count($effective); $i++){
$effect[$effective[$i]["tag"]] = $effective[$i]["value"];
}
return $effect;
}
//xml_query(method, condition, multiple-condition logical operator and or or, inserted or updated array)
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);
}
}
/ /Get xml array
function xml_fetch_array($condition,$if)
{
//$this->querys++;
$row = $this->array; //Initialize data array
if($condition) {
//Whether there is a condition, if so, generate an array that meets the conditions
//Generate a condition array, conditional format field, operator, match
$condition=explode (",",$condition);//Condition array
$cs=count($condition)/3; //Condition number
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;$rfor($c=0;$c<$cs;$c++){
//$i++;
$condition=$conditions[$c]; //Current conditions
$field =$condition['field']; //Field
$operator=$condition["operator"]; //Operator
$match=$condition['match']; //Match
if(($operator=='=')&&($row[$r][$field]==$match)){
$true++;//If the conditions are met, the number of matches is increased by 1
} elseif(($operator=='!=')&&($row[$r][$field]!=$match)){
$true++;//If the conditions are met, the number of matches is increased by 1
} elseif(($operator=='<')&&($row[$r][$field]<$match)){
$true++;//If the conditions are met, the number of matches is increased by 1
} elseif(($operator=='<=')&&($row[$r][$field]<=$match)){
$true++;//If the conditions are met, the matching number is added 1
} elseif(($operator=='>')&&($row[$r][$field]>$match)){
$true++;//If the conditions are met, the number is met Add 1
} elseif(($operator=='>')&&($row[$r][$field]>=$match)){
$true++;//If the conditions are met, Add 1 to the matching number
}
}
//Get the value according to the condition
if($if=='and'){
//If the multiple conditions are and, when the matching number is equal to When the condition number is used, generate an array
if($true==$cs){
$result[]=$row[$r];
}
} else {
//if Multiple conditions are or. When there is a matching record, an array is generated
if($true!=0){
$result[]=$row[$r];
}
}
//echo $true;
//echo "
"; 
//print_r($true);
$true=0;//The number that meets the conditions is reset to zero and enters the next cycle
}
} else {
$result=$this->array;
}
// echo "
"; 
//print_r($this->result);
return $result;
}
//Filtering or statistics
function xml_select($method,$condition,$if)
{
$result=$this->xml_fetch_array($condition,$if);
if($method=='select'){
return $result;
} else {
return count($result);
}
}
//Insert data
function xml_insert($condition,$if,$array)
{
$data=$this->xml_fetch_array($condition,$if);//Total data array
$data[] =$array; //Total data array after insertion
$this->array=$data; //Update total array
$this->WriteXml($data);
}
//Get the updated XML and rewrite
function xml_update($condition,$if,$array)
{
$datas=$this->array; //Total data array
$ subtract=$this->xml_fetch_array($condition,$if);//Array to be updated
//echo "
" ; 
//print_r($data);
//print_r($datas);
//echo "Each record has ".count($datas[0])." values< ;br>";
for($i=0;$i$data=$datas[$i];
//echo "Original record Item ".$i."
";
foreach($data as $k=>$v){
//echo "-Item ".$i." The value of ".$k." is ".$v."
";
//echo "--The array to be found." The value of ".$k." is ".$subtract[0][$ k]."
";
if($v==$subtract[0][$k]){
$is++;
}
}
if($ is==count($data)){
//echo "----Consistent with item ".$i."
";
$datas[$i]=$array;
//array_splice($datas,$i,$i+1);
}
//echo "The ".$i." item in the original record is the same as what you are looking for. ".$ is."match
";
//echo "end of ".$i." in the original record
";
$is=0;
}
//array_splice($datas,2,2+1,$array);
//echo "
"; 
//print_r($datas);
$this->array=$datas;
$this->WriteXml($datas);
}
//Write XML file (Write all)
function WriteXml($array)
{
if(!is_writeable($this->dbase)){
die("Cannot write".$this-> ;dbname.".xml");
}
$xml.="rn";
$xml.= "<$this->dbname>rn";
for($i=0;$i$xml.="<$this-> ;dbtable>rn";
foreach($array[$i] as $k=>$s){
$xml.="<$k>$srn";
}
$xml.="dbtable>rn";
}
$xml.="dbname>";
$fp=@fopen($this->dbase,"w");
flock($fp, LOCK_EX);
rewind($fp);
fputs($fp,$xml ;
function WriteLine($array)
{
if(!is_writeable($this->dbase)){
die("Unable to write ".$this->dbname.". xml");
}
$fp=@fopen($this->dbase,"w");
rewind($fp);
flock($fp, LOCK_EX);
fputs($fp,"rn");
fputs($fp,"<$this->dbname>rn ");
for($i=0;$ifputs($fp,"<$this->dbtable>rn");
$xml.="<$this->dbtable>rn";
foreach($array[$i] as $k=>$s){
fputs($fp,"< $k>$srn");
}
fputs($fp,"dbtable>rn");
}
fputs( $fp,"dbname>");
fclose($fp);
}
}
?>


Use Method: Insert a record


Copy code
The code is as follows: require_once('xml.class.php');
$xml = new xml("exemple.xml","item");
$newarray = array(
"title"=>"XML title",
"text"=>" PHP XML class test! "
);
$insert=$xml->xml_query('insert','','',$newarray);//The second and third variable positions are conditions, leaving them blank means that they are in Finally insert


Modify record


Copy code
The code is as follows: require_once('xml. class.php');
$xml = new xml("exemple.xml","item");
$array = array(
"title"=>"XML title",
"text"=>"PHP's XML class test!"
);
$insert=$xml->xml_query('update','title,=,What will the world be like in 20 years? ','and',$array);//Replace the title tag equal to xxx with $array (you can create a tag with unique attributes, such as id, so that you can modify a certain record)


Delete Record


Copy code
The code is as follows: require_once('xml.class.php');
$xml = new xml("exemple.xml","item");
$array = array();
$insert=$xml->xml_query('update','title,=,The world in 20 years What will happen? ','and',$array);//Leave the array empty


Note that when deleting, the value is actually made empty. We can modify xml_update() and generate the xml file. Before, we judge the value of $array. If the value is empty, it will not be written to the final array, which is the effect of deletion. When writing the xml file, the speed is very fast (I have tested 30,000 records), and only inserting is done. One record can be modified very quickly. It is very suitable for medium-sized websites to use when generating XML, so it is recommended.

http://www.bkjia.com/PHPjc/322775.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322775.htmlTechArticlexml.class.php file code copy code is as follows: ?php * example read data: * * $xml = new xml("dbase.xml",'table'); * * $data=$xml-xml_fetch_array(); * * echo "pre style="f...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn