A very convenient XML class !! Original Oh

Source: Internet
Author: User


<?
// 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.
// Such as virtual Community The user information is usually displayed only when the user himself or another User specifies his 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 = '<? XML version = "1.0" encoding = "gb2312"?> '. 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]). "</". $ I. "> ". enter;
// Use the array table name as an element label and add data content
}
// Echo $ C;
$ C. = "</". $ this-> 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 a file
$ xml = new data_xml (path. "arm. XML "," data ");
echo $ XML-> data [" Nick "]; // display it?
$ XML-> data ["Nick"] = ""; // update
$ XML-> data ["master"] = "Liu Bei "; // insert into
$ XML-> saveas (path. "arm. XML "); // Save As overwrite
*/
?>

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.