A very convenient XML class !! Original Oh _ PHP Tutorial

Source: Internet
Author: User
A very convenient XML class !! Original Oh .? I am not familiar with XML, and may have incorrect description of XML. this is just an idea. to implement this, I should slightly trim it. I can save some data that is not frequently used in condition query using XML. // I am not familiar with XML, and the description of XML may be incorrect.
// This is just an idea. to implement it, you should slightly trim it. you can save data that is not frequently used in condition queries in XML.
// For example, user information in a virtual community is displayed only when the user himself or another user specifies his or her USERNAME.
// The remaining consumption of enabling, querying, interpreting datasets, and disabling databases each time.

Define ("enter", chr (13). chr (10 ));
Class DATA_XML // key !!! A custom class
{
Var $ parser; // XML interpreter
Var $ tags; // XML tag
Var $ on ;//
Var $ root; // root element
Var $ Data = array (); // array of elements

Function DATA_XML ($ filename, $ root) // class initial function filename file name, root element
{
$ This-> root = $ root; // initial root element
$ This-> parser = xml_parser_create (); // creates an interpreted object
Xml_set_object (& $ this-> parser, & $ this); // sets the object
Xml_set_element_handler ($ this-> parser, "tag_on", "tag_off"); // sets the element management function
Xml_set_character_data_handler ($ this-> parser, "getdata"); // Sets data management events
Xml_parser_set_option ($ this-> parser, XML_OPTION_CASE_FOLDING, 0); // case sensitive

If (file_exists ($ filename) // find the file?
{// Find
$ Fp = fopen ($ filename, "r"); // read-only open
$ C = fread ($ fp, filesize ($ filename); // read data
Fclose ($ fp); // Close
$ This-> parse ($ c); // sets the explanatory object
}
Else
{
Echo "ooooo ";//!?! It seems to be used for debugging. it's better to remove else.
}


}

Function parse ($ data) // explains the function
{
Xml_parse ($ this-> parser, $ data );
}

Function tag_on ($ parser, $ tag, $ attributes) // trigger a function by a discovery element
{
$ This-> on = true; // triggered
$ This-> tags = $ tag;
// Echo $ tag;
}

Function tag_off ($ parser, $ tag) // element termination function
{
$ This-> on = false; // element Terminator
}

Function getdata ($ parser, $ cdata) // Retrieve the element
{
If ($ this-> on & $ this-> tags! = $ This-> root)
{// Verify the result. (is it the beginning of the element and not the root element ?)

$ This-> Data [$ this-> tags] = trim ($ cdata );
// Key !! Use tags as an array table and add data to this array element.
// Echo trim ($ cdata );
// Echo $ this-> tags;
// Echo "\ n ";
}
}
Function check ($ str)
{// Check characters
If (strlen ($ str) <1) // if it is a null character
Return ''; // return''
Else
Return $ str; // otherwise, the original path is returned (otherwise, XML does not seem to end with the mark)
}
Function saveas ($ filename) // Save
{
$ C =' '. Enter;
// Needless to say, enter is define ("enter", chr (13). chr (10 ));
$ C. = "<". $ this-> root. ">". enter;
// First set the root element
For (reset ($ this-> Data); $ I = key ($ this-> Data); next ($ this-> Data ))
{// Start to list all the data
$ C. = "<". $ I. ">". $ this-> check ($ this-> Data [$ I])." ". Enter;
// Use the array table name as an element label and add data content
}
// Echo $ c;
$ C. =" Root. ">". enter; // end tag
$ Fp = fopen ($ filename, "w"); // write a file
Fwrite ($ fp, $ c );
Fclose ($ fp );

}
};




/* New Key: an XML file
$ Xml = new DATA_XML (path. "arm. xml", "DATA"/* Root element */);
$ Xml-> Data ["UserName"] = "Guan Yu ";
$ Xml-> Data ["Nick"] = "";
$ Xml-> saveas (path. "arm. xml ");
Unset ($ xml );
// Read an object
$ Xml = new DATA_XML (path. "arm. xml", "DATA ");
Echo $ xml-> Data ["Nick"]; // display it?
$ Xml-> Data ["Nick"] = ""; // UPDATE
$ Xml-> Data ["Master"] = "Liu Bei"; // INSERT
$ Xml-> saveas (path. "arm. xml"); // Save as overwrite itself
*/
?>

Http://www.bkjia.com/PHPjc/313924.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/313924.htmlTechArticle? // I am not familiar with XML, and the description of XML may be incorrect. // This is just an idea. to implement it, we should slightly trim it, you can save some data that is not frequently used for condition query in XML //...

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.