PHP operation XML as a class _php tutorial for a database

Source: Internet
Author: User
Tags dbase flock rewind
xml.class.php File Code
Copy CodeThe code is as follows:
* Example Read data:
*
* $xml = new XML ("Dbase.xml", ' table ');
*
* $data = $xml->xml_fetch_array ();
*
* echo "
";
*
* Print_r ($DATA);
*
Class XML
{
var $dbase; database, the XML file to read
var $dbname; Database name, top-level element, consistent with database file name
var $dbtable; Data table, node to get
var $parser; Profiler
var $vals; Property
var $index; Index
var $dbtable _array;//node Array
var $array; An array of subordinate nodes
var $result; The returned 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, filtering out the node name to be evaluated: $dbtable
foreach ($this->index as $key = + $val) {
if ($key = = $this->dbtable) {
Get an 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 in an array of arrays specified by the offset and length parameters.
Subordinate array of the nodes taken
$value =array_slice ($this->vals, $offset, $len);
Gets a valid array, merged into the result array
$this->array[]= $this->parseeff ($value);
}
return true;
}
Reads the 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 (method, condition, multi-conditional when 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; Initializing an array of data
if ($condition) {
If there is a condition, generate an array that matches the condition if there is a condition
Generate condition array, conditional format 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], "operator" = $condition [$i *3+1], "Match" and "$condition" +2]);
}
echo count ($row);
for ($r =0; $r for ($c =0; $c < $cs; $c + +) {
$i + +;
$condition = $conditions [$c]; Current conditions
$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 condition
if ($if = = ' and ') {
If the multi-condition is and, the array is generated when the compliance number equals the number of conditions
if ($true = = $cs) {
$result []= $row [$r];
}
} else {
If the multi-condition is or, the array is generated when there are records that match
if ($true!=0) {
$result []= $row [$r];
}
}
Echo $true;
echo "
";
Print_r ($true);
$true =0;//matches the condition number to zero and into the next round cycle
}
} else {
$result = $this->array;
}
echo "
";
Print_r ($this->result);
return $result;
}
Filter or statistic
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; Array of total data after insertion
$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);//Array to update
echo "
";
Print_r ($data);
Print_r ($datas);
echo "has" in each record. Count ($datas [0]). " A value
";
for ($i =0; $i $data = $datas [$i];
echo "First in original record". $i. " Reviews
";
foreach ($data as $k = = $v) {
echo "-section". $i. " ". $k of the article." The value is ". $v."
";
echo "-the array to find". $k. " The value is ". $subtract [0][$k]."
";
if ($v = = $subtract [0][$k]) {
$is + +;
}
}
if ($is ==count ($data)) {
echo "----and section". $i. " Article matches
";
$datas [$i]= $array;
Array_splice ($datas, $i, $i + 1);
}
echo "First in original record". $i. " ". $is to find." The
";
echo "First in original record". $i. " Article end
";
$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 RN ";
}
$xml. = " Dbtable>rn ";
}
$xml. = " Dbname> ";
$fp = @fopen ($this->dbase, "w");
Flock ($FP, LOCK_EX);
Rewind ($FP);
Fputs ($fp, $xml);
Fclose ($FP);
}
Write the XML row by line (I try to write 10000 lines, I don't feel write fast, so I don't use this kind of writing method)
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, " RN ");
Fputs ($fp, "< $this->dbname>rn");
for ($i =0; $i Fputs ($fp, "< $this->dbtable>rn");
$xml. = "< $this->dbtable>rn";
foreach ($array [$i] as $k = = $s) {
Fputs ($fp, "< $k > $s RN ");
}
Fputs ($FP, " Dbtable>rn ");
}
Fputs ($FP, " Dbname> ");
Fclose ($FP);
}
}
?>

How to use: Insert a record
Copy CodeThe code is as follows:
Require_once (' xml.class.php ');
$xml = new XML ("Exemple.xml", "item");
$newarray = Array (
"title" and "XML title",
"Text" = "PHP XML class Test!" "
);
$insert = $xml->xml_query (' Insert ', ' ', ' ', $newarray);//The second and third variable position is the condition, leaving the empty representation at the last insertion

Modify a record
Copy CodeThe code is as follows:
Require_once (' xml.class.php ');
$xml = new XML ("Exemple.xml", "item");
$array = Array (
"title" and "XML title",
"Text" = "PHP XML class Test!" "
);
$insert = $xml->xml_query (' Update ', what will the world be like after title,=,20 years? ', ' and ', $array);//title tag equals xxx replaced with $array (a label that can build a unique property, such as an ID, so that a record can be modified)

Deleting records
Copy CodeThe code is as follows:
Require_once (' xml.class.php ');
$xml = new XML ("Exemple.xml", "item");
$array = Array ();
$insert = $xml->xml_query (' Update ', what will the world be like after title,=,20 years? ', ' and ', $array);//array left blank

Note When the deletion is actually the value of empty, we can modify the Xml_update (), before generating an XML file to determine the value of $array, if the value is empty do not write to the final array is the effect of deletion. When writing to an XML file, speed is fast (I have tested 30,000 records), insert only one record, the modification speed is quite fast, very suitable for medium-sized web sites to use when generating XML, so recommend.

http://www.bkjia.com/PHPjc/322775.html www.bkjia.com true http://www.bkjia.com/PHPjc/322775.html techarticle xml.class.php File Code Copy Code code is as follows:? PHP * Example Read data: * * $xml = new XML ("Dbase.xml", ' table '); * * $data = $xml-xml_fetch_ar Ray (); * * echo "pre style=" f ...

  • Related Article

    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.