xml_fetch_array(); * * echo "
"; * * print_r($data); *"/>

xml_fetch_array(); * * echo "
"; * * print_r($data); *">






















HomeBackend DevelopmentPHP Tutorialxinputemulator PHP class for manipulating XML as a database

xml.class.php file code

Copy the code The code is as follows:


* example to read data:
*
* $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; //Database, XML file to be read <br>var $dbname; / /Database name, top-level element, consistent with database file name<br>var $dbtable; //Data table, node to be obtained<br>var $parser; //Parser<br>var $vals; //Attributes<br>var $index; // Index <br>var $dbtable_array; //Node array <br>var $array; //Array of subordinate nodes <br>var $result; //Returned results <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("Cannot read $this->dbname.xml"); <br>} <br>$this- > ;parser,$data ,$this->vals,$this->index); <br>xml_parser_free($this->parser); <br>//Traverse the index and filter out the node name of the node to be valued: $dbtable <br>foreach ($this ->index as $key=>$val) { <br>if ($key == $this->dbtable) { <br>//Get the node array<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() returns a sequence in the array specified by the offset and length parameters.<br>//Get the lower-level array of the node<br>$value=array_slice($this->vals,$offset,$len); <br>//Get the valid array and merge it into the result array<br>$this->array[]=$ this->parseEFF($value); <br>} <br>return true; <br>} <br>//Read the XML file and return the string <br>function ReadXml($file) <br>{ <br>return file_get_contents($file); <br>} <br> //Get an effective array <br>function parseEFF($effective) { <br>for ($i=0; $i $effect[$effective[$i]["tag"] ] = $effective[$i]["value"]; <br>} <br>return $effect; <br>} <br>//xml_query (method, condition, logical operator and or or for multiple conditions, array inserted or updated) <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>//Get xml array<br>function xml_fetch_array($condition,$ if) <br>{ <br>//$this->querys++; <br>$row = $this->array; //Initialize the data array <br>if($condition) { <br>//Whether there is a condition, if so, generate a matching Conditional array<br>//Generate conditional array, conditional format field, operator, match <br>$c//Conditional array<br>$cs=count($condition)/3; //Conditional number<br>for($i=0;$i&lt ;$cs;$i++){ <br>$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>$c //Current condition<br>$field=$condition['field']; //Field<br>$operator=$ condition["operator"]; //Operator <br>$match=$condition['match']; //Match <br>if(($operator=='=')&&($row[$r][$field] ==$match)){ <br>$true++;//If the conditions are met, the number of matches is increased by 1 <br>} elseif(($operator=='!=')&&($row[$r][$field]!=$ match)){ <br>$true++;//If the conditions are met, the number of matches is increased by 1 <br>} elseif(($operator=='$true++;//If the conditions are met, the number of matches is increased by 1 <br>} elseif(($operator=='$true++;//If the conditions are met, add 1 to the matching number <br>} elseif(($operator=='>')&&($row[$r][$field]>$match)){ <br>$true++ ;//If the conditions are met, add 1 to the matching number <br>} elseif(($operator=='>')&&($row[$r][$field]>=$match)){ <br>$true++;/ /If the condition is met, add 1 to the matching number <br>} <br>} <br>//Get the value according to the condition <br>if($if=='and'){ <br>//If the multiple conditions are and, when the matching number is equal to the condition number, generate Array <br>if($true==$cs){ <br>$result[]=$row[$r]; <br>} <br>} else { <br>//If multiple conditions are or, when there is a matching record, generate an array <br>if ($true!=0){ <br>$result[]=$row[$r]; <br>} <br>} <br>//echo $true; <br>//echo "<pre class="brush:php;toolbar:false">"; <br>//print_r($true ; ; <br>//print_r($this->result); <br>return $result; <br>} <br>//Filtering or statistics <br>function xml_select($method,$condition,$if) <br>{ <br>$result=$this-&gt ;xml_fetch_array($condition,$if); <br>if($method=='select'){ <br>return $result; <br>} else { <br>return count($result); <br>} <br>} <br>//Insert data <br>function xml_insert($condition,$if,$array) <br>{ <br>$data=$this->xml_fetch_array($condition,$if);//Total data array <br>$data[]=$array; //After insertion Total data array <br>$this->array=$data; //Update the total array <br>$this->WriteXml($data); <br>} <br>//Get the updated XML and rewrite <br>function xml_update($condition,$ if,$array) <br>{ <br>$datas=$this->array; //Total data array<br>$subtract=$this->xml_fetch_array($condition,$if);//Array to be updated<br>// echo "<pre class="brush:php;toolbar:false">"; <br>//print_r($data); <br>//print_r($datas); <br>//echo "Each record has ".count($datas[0])." values <br>"; <br>for($i=0;$i<count>$data=$datas[$i]; <br>//echo "No. 1 in the original record." $i."item<br>"; <br>foreach($data as $k=>$v){ <br>//echo "-The ".$k." value of ".$i." item is" .$v."<br>"; <br>//echo "--The value of the array ".$k." to be found is ".$subtract[0][$k]."<br>"; <br> if($v==$subtract[0][$k]){ <br>$is++; <br>} <br>} <br>if($is==count($data)){ <br>//echo "----With the first ".$i." matches<br>"; <br>$datas[$i]=$array; <br>//array_splice($datas,$i,$i+1); <br>} <br>//echo "original The ".$i." item in the record matches ".$is." to be found<br>"; <br>//echo "The ".$i." item in the original record ends<br>$is=0; <br>} <br>//array_splice($datas,2,2+1,$array); <br>//echo "<pre class="brush:php;toolbar:false">"; <br>//print_r($datas); <br> $this->array=$datas; <br>$this->WriteXml($datas); <br>}<br>//Write XML file (write all) <br>function WriteXml($array) <br>{ <br>if(!is_writeable($this->dbase)){ <br>die("Unable to write".$this-> dbname.".xml"); <br>} <br>$xml.="<?xml version="1.0" encoding="utf-8"?>rn"; <br>$xml.="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-&gt ;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>//Write xml line by line (I tried to write 10,000 lines, but it didn’t happen once Writing is fast, so there is no need to use this writing method) <br>function WriteLine($array) <br>{ <br>if(!is_writeable($this->dbase)){ <br>die("Cannot write".$this-&gt ;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>Usage: Insert a record <br><p><span><u>Copy code </u></span> The code is as follows: </p>
<p><br>require_once('xml.class.php'); <br>$xml = new xml("exemple .xml","item"); <br>$newarray = array( <br>"title"=>"XML title", <br>"text"=>"PHP’s XML class test! " <br>); <br>$insert=$xml->xml_query('insert','','',$newarray);//The second and third variable positions are conditions, leaving them blank means inserting <br></p> at the end <br>Modify record<br><p><span><u>Copy code</u></span> The code is as follows:</p>
<p><br>require_once('xml.class.php'); <br>$xml = new xml("exemple.xml","item"); <br>$array = array( <br>"title"=>"XML title", <br>"text"=>"PHP's XML class test!" <br>); <br>$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 unique attribute tag, such as id, so that a certain record can be modified) <br></p>
<br>Delete record<br><p><span><u>Copy code</u></span> The code is as follows:</p>
<p><br>require_once('xml.class.php'); <br>$xml = new xml("exemple.xml","item"); <br>$ array = array(); <br>$insert=$xml->xml_query('update','title,=,What will the world be like in 20 years?','and',$array);//Leave the array empty<br> </p>
<br>Remarks: When deleting, the value is actually made empty. We can modify xml_update() to determine the value of $array before generating the xml file. If the value is empty, it will not be written to the final array, which is the effect of deletion. . The speed of writing xml files is very fast (I have tested 30,000 records). When inserting, only one record is inserted, and the modification speed is also very fast. It is very suitable for medium-sized websites to use when generating XML, so I recommend it.
                
                
                <p>
                    The above introduces the xinputemulator PHP class that operates XML as a database, including the content of xinputemulator. I hope it will be helpful to friends who are interested in PHP tutorials. </p>
                <p>
                    </p></count></count>
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
Related Article
Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function