Php operation XML addition, deletion, query, modification
I used xml to replace the database for a small website with a small amount of data.
The execution efficiency of this short code is too low. Inserting 1000 data records cyclically will time out.
[Code] [PHP] code
Class XMLDataBase extends DomDocument {var $ dbname; // attribute database file name with the root element name var $ dbpath; // attribute database path // var $ debug; // property debug flag var $ debug = true; // enable the debug mode public function XMLDataBase ($ dbname, $ dbpath) {// Constructor if (empty ($ dbname )) {if ($ this-> debug) echo "failed to create the database file. Please check the instance parameter"; return false;} else {$ this-> dbname = $ dbname; // name the database as the specified name $ this-> dbpath = $ dbpath; // specify the path for data if (! File_exists ($ dbpath. "/". $ this-> dbname. ". xml ") {// if there is no database file $ xmlstr = 'dbname. 'SN = "0"/> '; $ this-> loadXML ($ xmlstr); $ this-> save ($ dbpath. "/". $ this-> dbname. ". xml "); // create a database file} $ this-> load ($ dbpath. "/". $ this-> dbname. ". xml "); // load database file} return true;} // Insert data public function insert_xml ($ myfields, $ values, $ mytype) {if (empty ($ mytype )) {// if the type is null, return the following node $ Root = $ this-> documentElement;} else {$ Root = $ this-> g EtElementsByTagName ($ mytype); // Get $ mytype node $ Root = $ Root-> item (0); if (empty ($ Root-> nodeValue )) {// if the $ mytype node is null, it is created. Does empty () exist? It does not exist !! $ This-> InserTabeNode ($ mytype); $ Root = $ this-> getElementsByTagName ($ mytype); $ Root = $ Root-> item (0 );}} $ arrfield = explode ("# cut #", $ myfields); $ arrval = explode ("# cut #", $ values ); // Get the insert record $ AutoID = $ this-> AutoKey (); $ Node_AutoID = $ this-> createElement ("autoid "); $ text = $ this-> createTextNode ($ AutoID); $ Node_AutoID-> appendChild ($ text); $ I = 0; foreach ($ arrfield as $ myfield) {$ node} = $ arrval [$ I]; $ {"Node _". $ myfield }=$ this-> createElement ($ myfield); $ text = $ this-> createTextNode ($ {$ node}); $ {"Node _". $ myfield}-> appendChild ($ text); $ I ++;} // create a record $ Node_record = $ this-> createElement ("record "); $ Node_record-> appendChild ($ Node_AutoID); foreach ($ arrfield as $ fieldn) {$ Node_record-> appendChild ($ {"Node _". $ fieldn});} // add to $ Root-> appendChild ($ Node_record) under $ dbtable node; // update Root node SN $ this-> documentEle Ment-> setAttribute ("sn", "$ AutoID"); // save $ this-> save ($ this-> dbpath. "/". $ this-> dbname. ". xml "); return true;} // modify the public function update_xml ($ AutoID, $ mytype) {if (empty ($ mytype) {// if the type is empty, return the following node $ Root = $ this-> documentElement; $ xpath = new DOMXPath ($ this ); $ Node_Record = $ xpath-> query ("// record [autoid = $ AutoID]");} else {$ Root = $ this-> getElementsByTagName ($ mytype ); // Get $ mytype node $ Root = $ Root-> it Em (0); $ xpath = new DOMXPath ($ this ); $ Node_Record = $ xpath-> query ("/$ this-> dbname/$ mytype/record [autoid = $ AutoID]");} // end if (empty ($ Node_Record-> item (0)-> nodeName) echo "no records to be modified are found \ r \ n "; $ K = 0; foreach ($ Node_Record-> item ($ I)-> childNodes as $ articles) {$ Field [$ K] = $ articles-> textContent; echo "$ articles-> nodeName \ r \ n"; $ K ++ ;}// save the public function besave_xml record ($ AutoID, $ values, $ mytype) {if (em Pty ($ mytype) {// if the type is null, return the following node $ Root = $ this-> documentElement; $ xpath = new DOMXPath ($ this ); $ Node_Record = $ xpath-> query ("// record [autoid = $ AutoID]");} else {$ Root = $ this-> getElementsByTagName ($ mytype ); // Get $ mytype node $ Root = $ Root-> item (0); $ xpath = new DOMXPath ($ this ); $ Node_Record = $ xpath-> query ("/$ this-> dbname/$ mytype/record [autoid = $ AutoID]");} // end if (empty ($ Node_Record-> item (0)-> nodeValu E) {// if the record does not exist if ($ this-> debug) echo "AutoID = {$ AutoID} records do not exist in the {$ mytype} node, check input parameters! "; Return false ;}$ Replace = explode (" # cut # ", $ values); $ temparr = array_reverse ($ Replace); // array_push ($ temparr, $ AutoID); $ temparr [] = $ AutoID; // use array_push () to add a unit to the array. Instead, use $ array [] =, this is because there is no additional burden for calling functions. -- By PHP Manual $ Replace = array_reverse ($ temparr); $ K = 0; // modify foreach ($ Node_Record-> item (0)-> childNodes as $ articles) {$ Node_newText = $ this-> createTextNode ($ Replace [$ K]); $ articles-> replaceChild ($ Node_newText, $ articles-> lastChild ); // *************** have some doubts about $ K ++;} $ this-> save ($ this-> dbpath. "/". $ this-> dbname. ". xml "); return true;} // filter or count public function select_xml ($ sfield, $ keyword, $ mytype, $ rows, $ I Sonly) {// $ sfield specifies the field $ mytype category name $ specify the number of records during rows loop output $ isonly checks whether the specified keyword record exists if ($ sfield = '') $ sfield = 'autoid'; // set the default value of the field // if ($ keyword = '') $ keyword = '1'; $ xpath = new DOMXPath ($ this ); // if (empty ($ keyword) return 0; if ($ mytype = '') {$ querystr =" // record [contains ($ sfield, '$ keyword')] "; $ Node_Record = $ xpath-> query ($ querystr );} else {$ querystr = "/$ this-> dbname/$ mytype/record [contains ($ sfield, '$ key Word')] "; $ Node_Record = $ xpath-> query ($ querystr);} // end if $ Node_Record_Length = $ Node_Record-> length; // obtain the total number of records if ($ isonly = 1) {if ($ Node_Record_Length> 0) return 999;} // loop output field and its content if ($ Node_Record_Length <1) return 0; $ rerows = array (); // number of record sets $ rerow = array (); // Single record array for ($ I = 0; $ iitem ($ I) -> childNodes as $ articles) {$ Field [$ K] = $ articles-> textContent; $ rerow ["$ articles-> nodeName"] = $ Field [$ K]; // Node Name => The node value is pushed into the array $ K ++;} $ rerows [$ I] = $ rerow ;} // for end // do not know if there are any better methods for reverse sorting results $ echorows = array_reverse ($ rerows); // returns an array in reverse order $ countall = count ($ echorows ); // count the total number of array records if ($ rows> 0) $ countall = $ rows; // if the specified number of rows changes the total number of data if ($ rows> count ($ echorows )) $ countall = count ($ echorows); // if the specified number of rows is greater than the total number, the total number remains unchanged $ myrs = array (); // The number of new record sets for ($ j = 0; $ jdocumentElement; $ xpath = new DOMXPath ($ this); $ Node_Record = $ xpath-> query ("// recor D [autoid = $ AutoID] ");} else {$ Root = $ this-> getElementsByTagName ($ mytype ); // Get $ mytype node $ Root = $ Root-> item (0); $ xpath = new DOMXPath ($ this ); $ Node_Record = $ xpath-> query ("/$ this-> dbname/$ mytype/record [autoid = $ AutoID]");} // query the deleted record if (empty ($ Node_Record-> item (0)-> nodeValue) {// if the record does not exist if ($ this-> debug) the record of echo "AutoID = {$ AutoID} does not exist in the {$ mytype} node. check the input parameters! "; // Return false} $ Root-> removeChild ($ Node_Record-> item (0 )); // remove $ this-> save ($ this-> dbpath from the node. "/". $ this-> dbname. ". xml "); return true; // return true} // Insert category node public function InserTabeNode ($ mytype) {$ Root = $ this-> documentElement; $ Node_record = $ this-> createElement ($ mytype); $ Root-> appendChild ($ Node_record); $ this-> save ($ this-> dbpath. "/". $ this-> dbname. ". xml ");} public function AutoKey () {// Generate an auto-increment serial number // read the Root node sn attribute $ Root = $ this-> documentElement; if ($ Root-> hasAttributes ()) $ attributes = $ Root-> attributes; $ AutoKey = $ attributes-> item (0)-> nodeValue + 1; // increase the original sn value by 1 return $ AutoKey ;}} // class end // call is similar to this: $ fields = "item # cut # description # cut # quantity # cut # amount # cut # email # cut # email2 # cut # paytime # cut # posttime "; $ values = "$ item # cut # $ description # cut # $ quantity # cut # $ amount # cut # $ email # cut # $ email2 # cut # $ pa Ytime # cut # $ posttime "; $ Myxml = new XMLDataBase (" orderlist ", XMLDATAPATH); if ($ Myxml-> insert_xml ($ fields, $ values ,"")) {ShowMsg ("order published successfully! "," Orderlist. php "); exit ();}