Tutorials to .com

Tutorials to .com » Php » Xml » A very convenient type of XML! ! Original Oh!

A very convenient type of XML! ! Original Oh!

Print View , by: iSee ,Total views: 10 ,Word Count: 866 ,Date: Thu, 16 Apr 2009 Time: 1:40 AM

<?
/ / I am not familiar with XML, may comment on the xml description of an error
/ / It's just a thought, in order to achieve what should be a little trimming can be used to the conditions of some of the data query xml Save
/ / If the virtual community of users to information in general, only the user's own or other user-specified before his show USERNAME
/ / Left open every time, query, data sets to explain the closure of the database consumption.

define ( "enter", chr (13). chr (10));
class DATA_XML / / key! ! ! A custom type
(
var $ parser; / / xml parser
var $ tags; / / XML tags
var $ on; / /
var $ root; / / root element
var $ Data = array (); / / element array

function DATA_XML ($ filename, $ root) / / function type of initial filename file name, root the root element
(
$ this-> root = $ root; / / initial root element
$ this-> parser = xml_parser_create ();// create object explained
xml_set_object (& $ this-> parser, & $ this); / / set the target
xml_set_element_handler ($ this-> parser, "tag_on", "tag_off ");// element management function set
xml_set_character_data_handler ($ this-> parser, "getdata"); / / set data management events
xml_parser_set_option ($ this-> parser, XML_OPTION_CASE_FOLDING, 0); / / demanding case sensitive

if (file_exists ($ filename)) / / find a file?
(/ / Find
$ fp = fopen ($ filename, "r"); / / read-only open
$ c = fread ($ fp, filesize ($ filename)); / / read data
fclose ($ fp); / / Close
$ this-> parse ($ c); / / set up to explain the object
)
else
(
echo "ooooo"; / /! ? ! Seems to be used for debugging, removed else better
)


)

function parse ($ data) / / interpretation function
(
xml_parse ($ this-> parser, $ data);
)

function tag_on ($ parser, $ tag, $ attributes) / / find elements to trigger function
(
$ this-> on = true; / / has already triggered off
$ this-> tags = $ tag;
/ / echo $ tag;
)

function tag_off ($ parser, $ tag) / / element function PPD
(
$ this-> on = false; / / element PPD
)

function getdata ($ parser, $ cdata) / / remove the element
(
if ($ this-> on & & $ this-> tags! = $ this-> root)
(/ / Test is better, (now the beginning of the element and not the root element?)

$ this-> Data [$ this-> tags] = trim ($ cdata);
/ / Key ah! ! Table with an array of labels to do, and to pay the array of data elements
/ / echo trim ($ cdata);
/ / echo $ this-> tags;
/ / echo "\ n";
)
)
function check ($ str)
(/ / Check character
if (strlen ($ str) <1) / / If it is null
return ''; / / return ''
else
return $ str; / / Otherwise, return same way (or will not like the end of XML tags)
)
function saveas ($ filename) / / Save As
(
$ C++ = '<? xml version = "1.0" encoding = "gb2312 "?>'. enter;
/ / This goes without saying that the bar, enter the define ( "enter", chr (13). Chr (10));
$ c. ="<".$ this-> root .">". enter;
/ / First set the root element you
for (reset ($ this-> Data); $ i = key ($ this-> Data); next ($ this-> Data))
(/ / The beginning of all the data you cite
$ c. = "<".$ i. ">".$ this-> check ($ this-> Data [$ i ])."</".$ i. ">". enter;
/ / Table name used to do an array element label, and together with the data content
)
/ / echo $ c;
$ c. ="</".$ this-> root .">". enter; / / closing tag
$ fp = fopen ($ filename, "w"); / / write to file
fwrite ($ fp, $ c);
fclose ($ fp);

)
);




/ * Key to a new XML document
$ xml = new DATA_XML (the path. "arm.xml", "DATA" / * root element * /);
$ xml-> Data [ "UserName"] = "Guan Yu";
$ xml-> Data [ "Nick"] = "Guan Yunchang";
$ xml-> saveas (the path. "arm.xml");
unset ($ xml);
/ / Read a file
$ xml = new DATA_XML (the path. "arm.xml", "DATA");
echo $ xml-> Data [ "Nick "];// show about?
$ xml-> Data [ "Nick"] = "old customs"; / / UPDATE
$ xml-> Data [ "Master"] = "Liu Bei"; / / INSERT INTO
$ xml-> saveas (the path. "arm.xml ");// Save for the cover itself
* /
?>


Php XML Application Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.