Home > Article > Backend Development > PHP class for operating XML to create a database_PHP tutorial
* example read data:
*
* $xml = new xml("dbase.xml",'table');
*
* $data=$xml->xml_fetch_array();
*
* echo "
";
*
* print_r($data);
*
class xml
{
var $dbase; //Database, XML file to be read
var $dbname; //Database name, top-level element, consistent with database file name
var $dbtable; //Data table, node to be obtained
var $parser; //Parser
var $vals; //Attribute
var $index; //index
var $dbtable_array;//Node array
var $array; //Array of subordinate nodes
var $result; //returned result
var $querys;
function xml($dbase,$dbtable)
{
$this->dbase=$dbase;
$this->dbname=substr($dbase,strrpos($dbase,"/") 1,-4);
$this->dbtable=$dbtable;
$data=$this->ReadXml($this->dbase);
if(!$data){
die("Cannot read $this->dbname.xml");
}
$this->parser = xml_parser_create();
xml_parser_set_option($this->parser,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($this->parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct($this->parser,$data,$this->vals,$this->index);
xml_parser_free($this->parser);
//Traverse the index and filter out the nodes to be valued. Node name: $dbtable
foreach ($this->index as $key=>$val) {
if ($key == $this->dbtable) {
//Get node array
$this->dbtable_array = $val;
} else {
continue;
}
}
for ($i=0; $i < count($this->dbtable_array); $i =2) {
$offset = $this->dbtable_array[$i] 1;
$len = $this->dbtable_array[$i 1] - $offset;
//array_slice() returns a sequence in the array array specified by the offset and length parameters.
//The lower level array of the taken node
$value=array_slice($this->vals,$offset,$len);
//Get valid arrays and merge them into the result array
$this->array[]=$this->parseEFF($value);
}
return true;
}
//Read the XML file and return the string