xml.class.php File Code
Copy Code code as follows:
<?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, XML file to read
var $dbname; Database name, top level element, consistent with database file name
var $dbtable; Data table, the node to be obtained
var $parser; Profiler
var $vals; Property
var $index; Index
var $dbtable _array;//node Array
var $array; An array of subordinate nodes
var $result; The result returned
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);
Traversing the index, filtering out the node name for the value to be fetched: $dbtable
foreach ($this->index as $key => $val) {
if ($key = = $this->dbtable) {
Get Array of nodes
$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 of arrays in the array specified by the offset and length parameters.
Fetched node sub array
$value =array_slice ($this->vals, $offset, $len);
Gets a valid array and merges it into an array of results
$this->array[]= $this->parseeff ($value);
}
return true;
}
Reads an XML file into and returns a 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 (methods, conditions, multiple conditions, logical operators and or OR, arrays inserted or updated)
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; Initializing an array of data
if ($condition) {
Whether there are conditions, if any, to generate an array that matches the criteria
Generates an array of conditions, conditional formatting Field,operator,match
$condition =explode (",", $condition);//condition array
$cs =count ($condition)/3; Number of conditions
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]; Current condition
$field = $condition [' field ']; Field
$operator = $condition ["operator"];//operator
$match = $condition [' Match ']; The
if ($operator = =) && ($row [$r] [$field]== $match)] {
$true ++;//If the conditions are met, the number plus 1
ElseIf ($operator = = '!= ') && ($row [$r] [$field]!= $match]) {
$true ++;//If the conditions are met, the number plus 1
} elseif ($operator = = ' < ') && ($row [$r] [$field]< $match]) {
$true ++;//If the conditions are met, the number plus 1
ElseIf ($operator = = ' <= ') && ($row [$r] [$field]<= $match]) {
$true ++;//If the conditions are met, the number plus 1
} elseif ($operator = = ' > ') && ($row [$r] [$field]> $match]) {
$true ++;//If the conditions are met, the number plus 1
} elseif ($operator = = ' > ') && ($row [$r] [$field]>= $match]) {
$true ++;//If the conditions are met, the number plus 1
}
}
Value based on criteria
if ($if = = ' and ') {
If multiple conditions are and, when the number equals the number of conditions, the array is generated
if ($true = = $cs) {
$result []= $row [$r];
}
} else {
If multiple conditions are or, when there is a record, an array is generated
if ($true!=0) {
$result []= $row [$r];
}
}
Echo $true;
echo "<pre style=" Font-size:12px;text-align:left ">";
Print_r ($true);
$true =0;//meets the condition number to zero, enters the next round cycle
}
} else {
$result = $this->array;
}
echo "<pre style=" Font-size:12px;text-align:left ">";
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);
}
}
Inserting data
function Xml_insert ($condition, $if, $array)
{
$data = $this->xml_fetch_array ($condition, $if);//Total data array
$data []= $array; The inserted total data array
$this->array= $data; Update Total Group
$this->writexml ($data);
}
Get the updated XML and overwrite
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 "Every record has". Count ($datas [0]). " Value <br> ";
for ($i =0; $i <count ($datas); $i + +) {
$data = $datas [$i];
echo "The first in the original". $i. " Article <br> ";
foreach ($data as $k => $v) {
echo "-$i." ". $k." Value 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 "----and". $i. " In line with <br> ";
$datas [$i]= $array;
Array_splice ($datas, $i, $i + 1);
}
echo "The first in the original". $i. " With the ". $is to find. Matching <br> ";
echo "The first in the original". $i. " The end of the article <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 file (write all)
function WriteXml ($array)
{
if (!is_writeable ($this->dbase)) {
Die ("Cannot 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-by-line (I try to write 10000 lines, I feel like I didn't write fast, so it doesn't work this way)
function WriteLine ($array)
{
if (!is_writeable ($this->dbase)) {
Die ("Cannot 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: Insert a record
Copy Code code as follows:
Require_once (' xml.class.php ');
$xml = new XML ("Exemple.xml", "item");
$newarray = Array (
"title" => "XML Headers",
"Text" => PHP's XML class test! "
);
$insert = $xml->xml_query (' Insert ', ', ', $newarray); the second and third variable position is the condition, and leave blank to indicate the last insertion
Modify Records
Copy Code code as follows:
Require_once (' xml.class.php ');
$xml = new XML ("Exemple.xml", "item");
$array = Array (
"title" => "XML Headers",
"Text" => PHP's XML class test! "
);
$insert = $xml->xml_query (' Update ', title,=,20) what will the world be like in the years to come? ', ' and ', $array);//title label equals XXX Replace with $array (a label that can build a unique property, such as ID, so you can modify a record)
Delete a record
Copy Code code as follows:
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 years to come? ', ' and ', $array);//Array blank
Note Delete is actually to empty the value, we can modify the Xml_update (), before generating the XML file to determine the value of the $array, if the value is empty will not be written into the final array is the effect of the deletion. Fast powder when writing to XML file (I tested 30,000 records), insert only one record, the modification speed is quite fast, it is suitable for medium Web site to generate XML, so recommend.