Home  >  Article  >  Backend Development  >  A very handy XML class! ! Original_PHP Tutorial

A very handy XML class! ! Original_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:11:301019browse


// I am not familiar with XML, and the description of XML may be wrong in the annotation
// This is just an idea. To be implemented, it should be slightly modified. Some data that are not commonly queried using XML can be used. Save
// For example, user information in a virtual community will generally be displayed only if the user himself or another user specifies his USERNAME
// The rest requires opening, querying, interpreting data sets, and closing the database every time consumption.

define("enter",chr(13).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(); // Element array

function DATA_XML($filename,$root) //Class initial function filename file name, root root element
{
$this->root = $root; //Initial root element
$this->parser = xml_parser_create();//Create 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"); //Set data management events
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); //Requires strict case sensitivity

if( file_exists($filename) ) //File found?
{//Find
$fp = fopen($filename,"r"); //Open for read only
$c = fread($fp,filesize($filename)); //Read data
fclose($fp);//Close
$this->parse($c); //Set the interpretation object
}
else
{
echo "ooooo "; //! ? ! It seems to be used for debugging, just remove the else
}


}

function parse($data)//Explanation function
{
xml_parse ($this->parser,$data);
}

function tag_on($parser,$tag,$attributes)//Discover element trigger function
{
$this- >on = true; // Already triggered
$this->tags = $tag;
//echo $tag;
}

function tag_off($parser,$ tag) //Element binding function
{
$this->on = false; // Element binding
}

function getdata($parser,$cdata) // Take out the element
{
if($this->on && $this->tags!=$this->root)
{//It is better to verify, (whether it is an element now starting, and not the root element? )

$this->Data[$this->tags]=trim($cdata);
                                                ! Use tags to make an array table and add data to the array elements
//echo trim($cdata);
//echo $this->tags;
//echo "n";
}
}
function check($str)
{//Check character
if( strlen($str)<1)//If it is a null character
return ' ' ;//Return ' '
else
return $str;//Otherwise return to the original path (otherwise the XML will not seem to have an end tag)
}
function saveas($filename)//Save as For
{
$c=''.enter; ",chr(13).chr(10));
$c.="<".$this->root.">".enter;
for( reset($this->Data);$i=key($this->Data);next($this->Data))
{//Start to list all the data
$c.= "<".$i.">".$this->check($this->Data[$i])."// Use the array table name as the element label and add the data content
}
//echo $c;
$c.="root.">".enter;//End tag
$fp = fopen( $filename , "w" );//Write file
fwrite($fp,$c);
fclose($fp);

}
};




/*New key an XML file
                                                      DATA_XML(path."arm.xml","DATA"/*root element*/);
$xml->Data["UserName"]="Guan Yu";
    $xml->Data[ "Nick"]="Guan Yunchang";
$xml->saveas(path."arm.xml");
unset( $xml );
// Read a file
$ xml = new DATA_XML(path."arm.xml","DATA");
                      echo $xml->Data["Nick"];//Show it?
$xml->Data["Nick"]="Master Guan";//UPDATE
$xml->Data["Master"]="Liu Bei";//INSERT INTO
$ xml->saveas(path."arm.xml");//Save as overwrite itself
*/
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313924.htmlTechArticle? // I am not familiar with XML. The description of XML may be wrong. // This is just an idea. If implemented, it should be slightly modified, and some data queried by uncommon conditions can be saved in XML //...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn