xml| original;?
I'm not familiar with XML, maybe annotations have incorrect description of XML
This is just a thought, to achieve the words should be slightly trimmed, you can put some of the data query is not commonly used in XML storage
such as the user information of the virtual community, usually only the user himself or other users specified his username will be displayed
All that remains is to open, query, interpret, and turn off the consumption of the database every time.
Define ("Enter", Chr. Chr (10));
Class Data_xml//Key!!! A custom class
{
var $parser; XML interpreter
var $tags; XML tags
var $on; //
var $root; root element
var $Data = array (); Array of elements
function Data_xml ($filename, $root)//class initial function filename filename, root root element
{
$this->root = $root; Initial root element
$this->parser = Xml_parser_create ();//Create an Interpretation object
Xml_set_object (& $this->parser,& $this);//Set Object
Xml_set_element_handler ($this->parser, "tag_on", "Tag_off");/Set element management function
Xml_set_character_data_handler ($this->parser, "GetData"); Setting up data Management events
Xml_parser_set_option ($this->parser, xml_option_case_folding, 0); Requires strict case sensitivity
if (file_exists ($filename))//Find the file?
{//Find
$fp = fopen ($filename, "R"); Read-Only Open
$c = Fread ($fp, FileSize ($filename)); Reading data
Fclose ($FP);//Close
$this->parse ($c); Set Interpretation Object
}
Else
{
echo "Ooooo"; !?! It seems to be used for debugging, remove else.
}
}
Function Parse ($data)//Interpretation functions
{
Xml_parse ($this->parser, $data);
}
function tag_on ($parser, $tag, $attributes)//Discovery element trigger functions
{
$this->on = true; It's been triggered.
$this->tags = $tag;
Echo $tag;
}
function Tag_off ($parser, $tag)//Element node functions
{
$this->on = false; Element binding
}
function GetData ($parser, $cdata)//remove element
{
if ($this->on && $this->tags!= $this->root)
{//verification is better, (now is the beginning of the element, and not the root element?) )
$this->data[$this->tags]=trim ($cdata);
The key!! Make an array table with tags and pay the data for this array element
echo Trim ($cdata);
Echo $this->tags;
echo "\ n";
}
}
function Check ($STR)
{//Check character
if (strlen ($STR) <1)//If it is a null character
Return ';
Else
return $STR//Otherwise the original path returns (otherwise the XML seems to not do the end tag)
}
function SaveAs ($filename)//Save As
{
$c = ' <?xml version= ' 1.0 ' encoding= ' gb2312 '?> '. Enter;
This goes without saying, enter is define ("Enter", Chr (). Chr (10));
$c. = "<" $this->root. " > ". Enter;
First set the root element
for (Reset ($this->data); $i =key ($this->data); next ($this->data))
{//start to cite all the data.
$c. = "<". $i. " > $this->check ($this->data[$i]). " </". $i." > ". Enter;
Use the array table name as the element label and add the data content
}
Echo $c;
$c. = "</". $this->root. " > ". enter;//end Tag
$fp = fopen ($filename, "w");//write 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"]= "close Cloud long";
$xml->saveas (path. " Arm.xml ");
Unset ($xml);
Read a file
$xml = new Data_xml (path. " Arm.xml "," DATA ");
echo $xml->data["Nick"];//show?
$xml->data["Nick"]= "Guan Master";//update
$xml->data["Master"]= "Liu Bei";//insert into
$xml->saveas (path. " Arm.xml ");/Save As overwrite itself
*/
?>
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.