PHP operates on XML as a class for the database

  • 2020-03-31 21:24:03
  • OfStack

XML. Class. PHP file code
 
<?php 
* example  Read data:  
* 
* $xml = new xml("dbase.xml",'table'); 
* 
* $data=$xml->xml_fetch_array(); 
* 
* echo "<pre style="font-size:12px;">"; 
* 
* print_r($data); 
* 
class xml 
{ 
var $dbase; //Database, the XML file to read
var $dbname; //The database name, the top-level element, is the same as the database file name
var $dbtable; //Data table, the node to get
var $parser; //profiler
var $vals; //attribute
var $index; //The index
var $dbtable_array;//Nodes in the array
var $array; //An array of child nodes
var $result; //Return result
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(" Unable to 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); 
//Iterate through the index and filter out the node name to be valued :$dbtable
foreach ($this->index as $key=>$val) { 
if ($key == $this->dbtable) { 
// achieve Nodes in the 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 a sequence in the array array specified by the offset and length parameters.
//An array of children of the selected node
$value=array_slice($this->vals,$offset,$len); 
//Gets a valid array and merges into the resulting array
$this->array[]=$this->parseEFF($value); 
} 
return true; 
} 
//Read in the XML file and return the string
function ReadXml($file) 
{ 
return file_get_contents($file); 
} 
//Get a valid array
function parseEFF($effective) { 
for ($i=0; $i < count($effective); $i++){ 
$effect[$effective[$i]["tag"]] = $effective[$i]["value"]; 
} 
return $effect; 
} 
//Xml_query (method, condition, multi-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 an XML array
function xml_fetch_array($condition,$if) 
{ 
//$this->querys++; 
$row = $this->array; //Initializes the data array
if($condition) { 
//Whether there are conditions, if there are conditions to generate the array that meets the conditions
//Generate the condition array, condition format field,operator,match
$condition=explode(",",$condition);//Conditions of the 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;$r<count($row);$r++){ 
for($c=0;$c<$cs;$c++){ 
//$i++; 
$condition=$conditions[$c]; //The current conditions
$field=$condition['field']; //field
$operator=$condition["operator"];//The operator
$match=$condition['match']; //matching
if(($operator=='=')&&($row[$r][$field]==$match)){ 
$true++;//If the condition is satisfied, the number of matches is increased by 1
} elseif(($operator=='!=')&&($row[$r][$field]!=$match)){ 
$true++;//If the condition is satisfied, the number of matches is increased by 1
} elseif(($operator=='<')&&($row[$r][$field]<$match)){ 
$true++;//If the condition is satisfied, the number of matches is increased by 1
} elseif(($operator=='<=')&&($row[$r][$field]<=$match)){ 
$true++;//If the condition is satisfied, the number of matches is increased by 1
} elseif(($operator=='>')&&($row[$r][$field]>$match)){ 
$true++;//If the condition is satisfied, the number of matches is increased by 1
} elseif(($operator=='>')&&($row[$r][$field]>=$match)){ 
$true++;//If the condition is satisfied, the number of matches is increased by 1
} 
} 
//Conditional value
if($if=='and'){ 
//If multiple conditions are and, an array is generated when the number of conformances is equal to the number of conditions
if($true==$cs){ 
$result[]=$row[$r]; 
} 
} else { 
//If the multiple conditions are or, an array is generated when there is a match record
if($true!=0){ 
$result[]=$row[$r]; 
} 
} 
//echo $true; 
//echo "<pre style="font-size:12px;text-align:left">"; 
//print_r($true); 
$true=0;//The qualified number returns to zero and goes to the next cycle
} 
} else { 
$result=$this->array; 
} 
//echo "<pre style="font-size:12px;text-align:left">"; 
//print_r($this->result); 
return $result; 
} 
//Screening 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; // After inserting Total data array
$this->array=$data; //Update total group
$this->WriteXml($data); 
} 
//Get the updated XML and rewrite it
function xml_update($condition,$if,$array) 
{ 
$datas=$this->array; //Total data array
$subtract=$this->xml_fetch_array($condition,$if);//The array to update
//echo "<pre style="font-size:12px;text-align:left">"; 
//print_r($data); 
//print_r($datas); 
//Echo "in each record ".count($datas[0])." value <Br>" ;
for($i=0;$i<count($datas);$i++){ 
$data=$datas[$i]; 
//Echo ".$I." in the original record <Br>" ;
foreach($data as $k=>$v){ 
//Echo "- the value of ".$k." in ".$I." is ".$v."<Br>" ;
//Echo "-- the array to find ".$k. The value is ".$subtract[0][$k]."<Br>" ;
if($v==$subtract[0][$k]){ 
$is++; 
} 
} 
if($is==count($data)){ 
//Echo "-- corresponds to clause ".$I." <Br>" ;
$datas[$i]=$array; 
//array_splice($datas,$i,$i+1); 
} 
//Echo ".$i." in the original record matches ".$is." <Br>" ;
//Echo "line ".$I." in the original record ends <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); 
} 
//Write to XML files (all written)
function WriteXml($array) 
{ 
if(!is_writeable($this->dbase)){ 
die(" Unable to write ".$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); 
} 
//Write XML line by line (I tried to write 10,000 lines, but I didn't feel like it was faster than once, so I didn't use this type of writing)
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,"<?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); 
} 
} 
?> 

How to use: insert a record
 
require_once('xml.class.php'); 
$xml = new xml("exemple.xml","item"); 
$newarray = array( 
"title"=>"XML The title ", 
"text"=>"PHP the XML Class test! " 
); 
$insert=$xml->xml_query('insert','','',$newarray);//The second and third variable positions are conditions, leaving a blank means inserted at the end

Modify the record
 
require_once('xml.class.php'); 
$xml = new xml("exemple.xml","item"); 
$array = array( 
"title"=>"XML The title ", 
"text"=>"PHP the XML Class test! " 
); 
$insert=$xml->xml_query('update','title,=,20 What will the world be like in the New Year? ','and',$array);//The title tag is equal to XXX replaced by $array

Delete records
 
require_once('xml.class.php'); 
$xml = new xml("exemple.xml","item"); 
$array = array(); 
$insert=$xml->xml_query('update','title,=,20 What will the world be like in the New Year? ','and',$array);//An array of blank

We can modify xml_update() to determine the value of $array before generating the XML file. If the value is null, it will not be written to the final array. Writing to XML files is fast (I've tested 30,000 records), inserting only one record, and modifying it fairly quickly, so it's good for mid-sized sites generating XML, so I recommend it.

Related articles: