PHP operations XML Class read query delete data (Support level three node)

Source: Internet
Author: User
Tags foreach dbase flock logical operators rewind

FileName: xml.class.php

The code is as follows Copy Code
/***
* *
* FileName: xml.php *
* Function: XML class, perfect, only support level three node *
* Author: loking (biyees) *
* *
* 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= "document";
$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, total data array, 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; 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 "– Array to find". $k. " The value is ". $subtract [0][$k]." <br> ";
if ($v = = $subtract [0][$k]) {
$is + +;
}
}
if ($is ==count ($data)) {
echo "--with the $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=\" gb2312\ ">\r\n";
$xml. = "< $this->dbname>\r\n";
for ($i =0; $i <count ($array); $i + +) {
$xml. = "< $this->dbtable>\r\n";
foreach ($array [$i] as $k = > $s) {
$xml. = "< $k > $s </$k >\r\n";
}
$xml. = "</$this->dbtable>\r\n";
}
$xml. = "</$this->dbname>";
dBASE, "W ' > $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 ");
}
dBASE, "W ' > $fp = @fopen ($this->dbase," w ");
Rewind ($FP);
Flock ($FP, LOCK_EX);
Fputs ($fp, "<?xml version=\" 1.0\ "encoding=\" gb2312\ ">\r\n");
Fputs ($fp, "< $this->dbname>\r\n");
for ($i =0; $i <count ($array); $i + +) {
Fputs ($fp, "< $this->dbtable>\r\n");
$xml. = "< $this->dbtable>\r\n";
foreach ($array [$i] as $k = > $s) {
Fputs ($fp, "< $k > $s </$k >\r\n");
}
Fputs ($fp, "</$this->dbtable>\r\n");
}
Fputs ($fp, "</$this->dbname>");
Fclose ($FP);
}
}
?>

How to use:

Insert a record

The code is as follows Copy Code

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 a record

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

The code is as follows Copy Code

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 write to 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.