PHP operating XML as database class

Source: Internet
Author: User
Tags dbase flock rewind
Writing xml files is faster (I tested 30000 Records), insert only one record, and the modification speed is quite fast, it is suitable for medium-sized websites to generate XML, so we recommend that you. Xml. class. php file code

The code is as follows:


* Example:
*
* $ Xml = new xml ("dbase. xml", 'table ');
*
* $ Data = $ xml-> xml_fetch_array ();
*
* Echo"

";
*
* Print_r ($ data );
*
Class xml
{
Var $ dbase; // database, the XML file to be read
Var $ dbname; // database name, top-level element, consistent with the database file name
Var $ dbtable; // data table, the node to be obtained
Var $ parser; // parser
Var $ vals; // attribute
Var $ index; // index
Var $ dbtable_array; // Node array
Var $ array; // an array of lower-level nodes
Var $ result; // 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 );
// Retrieve the index and find the node name: $ dbtable
Foreach ($ this-> index as $ key => $ val ){
If ($ key = $ this-> dbtable ){
// Obtain 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 a sequence in the array specified by the offset and length parameters.
// The array of lower-level nodes
$ Value = array_slice ($ this-> vals, $ offset, $ len );
// Obtain a valid array and merge it into a result array.
$ This-> array [] = $ this-> parseEFF ($ value );
}
Return true;
}
// Read the XML file and return a string
Function ReadXml ($ file)
{
Return file_get_contents ($ file );
}
// Obtain a valid array
Function parseEFF ($ valid tive ){
For ($ I = 0; $ I <count ($ valid tive); $ I ++ ){
$ Effect [$ valid tive [$ I] ["tag"] = $ valid tive [$ I] ["value"];
}
Return $ effect;
}
// Xml_query (method, condition, logical operator and or when multiple conditions exist, 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 );
}
}
// Retrieve the xml array
Function xml_fetch_array ($ condition, $ if)
{
// $ This-> querys ++;
$ Row = $ this-> array; // initialize the data array
If ($ condition ){
// Whether conditions exist. if conditions exist, an array meeting the conditions is generated.
// Generate a condition array. the condition format is field, operator, and 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 For ($ c = 0; $ c <$ cs; $ c ++ ){
// $ I ++;
$ Condition = $ conditions [$ c]; // Current condition
$ 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 value must be 1
} Elseif ($ operator = '! = ') & ($ Row [$ r] [$ field]! = $ Match )){
$ True + +; // if the conditions are met, the value must be 1
} Elseif ($ operator = '<') & ($ row [$ r] [$ field] <$ match )){
$ True + +; // if the conditions are met, the value must be 1
} Elseif ($ operator = '<=') & ($ row [$ r] [$ field] <= $ match )){
$ True + +; // if the conditions are met, the value must be 1
} Elseif ($ operator = '>') & ($ row [$ r] [$ field]> $ match )){
$ True + +; // if the conditions are met, the value must be 1
} Elseif ($ operator = '>') & ($ row [$ r] [$ field] >=$ match )){
$ True + +; // if the conditions are met, the value must be 1
}
}
// Set the value based on the condition
If ($ if = 'and '){
// If multiple conditions are "and", an array is generated when the number is equal to the number of conditions.
If ($ true = $ cs ){
$ Result [] = $ row [$ r];
}
} Else {
// If multiple conditions are or, an array is generated when the record is met.
If ($ true! = 0 ){
$ Result [] = $ row [$ r];
}
}
// Echo $ true;
// Echo"
";
// Print_r ($ true );
$ True = 0; // The number of qualified conditions returns to zero and enters the next cycle.
}
} Else {
$ Result = $ this-> array;
}
// Echo"
";
// Print_r ($ this-> result );
Return $ result;
}
// Filter or count
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; // The total number of inserted data arrays
$ This-> array = $ data; // Total update Group
$ This-> WriteXml ($ data );
}
// Get the updated XML and rewrite it
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 contains". count ($ datas [0]). "values
";
For ($ I = 0; $ I $ Data = $ datas [$ I];
// Echo "the number in the original record". $ I ."
";
Foreach ($ data as $ k => $ v ){
// Echo "-". $ I. ". $ k." value: ". $ v ."
";
// Echo "-- the array to be searched". $ k. "value:". $ subtract [0] [$ k]."
";
If ($ v = $ subtract [0] [$ k]) {
$ Is ++;
}
}
If ($ is = count ($ data )){
// Echo "---- matches the". $ I ."
";
$ Datas [$ I] = $ array;
// Array_splice ($ datas, $ I, $ I + 1 );
}
// Echo "the". $ I. "in the original record matches". $ is ."
";
// Echo "the end of entry". $ 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 the 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 xml data row by row (I tried to write 10000 rows, so I didn't think this writing method was fast once)
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 );
}
}
?>


Usage: insert a record

The code is as follows:


Require_once ('XML. class. php ');
$ Xml = new xml ("exemple. xml", "item ");
$ Newarray = array (
"Title" => "XML title ",
"Text" => "php xml class test! "
);
$ Insert = $ xml-> xml_query ('insert', '','', $ newarray); // The location of the second and third variables is a condition. leave null to indicate insertion at the end.


Modify record

The code is as follows:


Require_once ('XML. class. php ');
$ Xml = new xml ("exemple. xml", "item ");
$ Array = array (
"Title" => "XML title ",
"Text" => "php xml class test! "
);
$ Insert = $ xml-> xml_query ('update', 'title, =, what will happen in the world in 20 years? ', 'And', $ array); // if the title tag is equal to xxx, replace it with $ array (you can create a tag with a unique attribute, such as id, so that you can modify a Record)


Delete record

The code is as follows:


Require_once ('XML. class. php ');
$ Xml = new xml ("exemple. xml", "item ");
$ Array = array ();
$ Insert = $ xml-> xml_query ('update', 'title, =, what will happen in the world in 20 years? ', 'And', $ array); // Leave the array empty.


When deleting the remarks, the value is actually null. we can modify xml_update () to determine the value of $ array before generating the xml file, if the value is null, it will be deleted if it is not written to the final array. Writing xml files is faster (I tested 30000 Records), insert only one record, and the modification speed is quite fast, it is suitable for medium-sized websites to generate XML, so we recommend that you.

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.