Home > Article > Backend Development > PHP operates XML as a database class_PHP tutorial
xml.class.php file code
";
*
* 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>$s$k>rn";
}
$xml.="$this->dbtable>rn";
}
$xml.="$this->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>$s$k>rn");
}
fputs($fp,"$this->dbtable>rn");
}
fputs( $fp,"$this->dbname>");
fclose($fp);
}
}
?>
Use Method: Insert a record