利用PHP实现 XML和MySQL的相互转换_PHP

WBOY
Release: 2016-06-01 12:20:48
Original
894 people have browsed it

mysql2xml.php类文件:用于备份MySQL数据的!

PHP代码

  1. class MySQL2XML {   
  2.         protected $conn;   
  3.         protected $result;   
  4.         protected $tables;   
  5.         protected $saveFolder = 'datas/';   
  6.            
  7.         public function __construct($config = NULL) {   
  8.                 if($config !== NULL && is_array($config)) {   
  9.                         $this->connect($config);   
  10.                 }   
  11.         }   
  12.            
  13.         public function connect($config) {   
  14.                 $this->conn = mysql_connect($config['host'], $config['username'], $config['password']);   
  15.                 if($this->conn) {   
  16.                         mysql_select_db($config['database']);   
  17.                         return true;   
  18.                 }   
  19.                 return false;   
  20.         }   
  21.            
  22.         public function setSaveFolder($folder) {   
  23.                 if(is_dir($folder)) {   
  24.                         $this->saveFolder = rtrim(str_replace("\\", "/", $folder),'/');  
  25.                         return true;  
  26.                 }  
  27.                 return false;  
  28.         }  
  29.           
  30.         public function setTables($tables) {  
  31.                 if(is_array($tables)) {  
  32.                         $this->tables = $tables;  
  33.                         return true;  
  34.                 }  
  35.                 return false;  
  36.         }  
  37.           
  38.         public function query($query) {  
  39.                 if(!isset($query) || trim($query) == '') return false;  
  40.                 $this->result = mysql_query($query);  
  41.                 if($this->result) return true;  
  42.                 return false;  
  43.         }  
  44.           
  45.         public function toXML() {  
  46.                 if(!isset($this->tables)) return false;  
  47.                 foreach($this->tables as $table) {  
  48.                         $file = $this->saveFolder.$table.'.xml';  
  49.                         $fp = @fopen($file, 'w');  
  50.                         if(!$fp) exit('Can not write file');  
  51.                         fwrite($fp, $this->tableToXML($table));  
  52.                         fclose($fp);  
  53.                         unset($fp);  
  54.                 }  
  55.                 return true;  
  56.         }  
  57.           
  58.         public function tableToXML($table) {  
  59.                 header("content-type:text/xml;charset=utf-8");  
  60.                 $xml = ""1.0\" encoding=\"utf-8\" ?>\n\n";   
  61.                 $fields = $this->getFields($table);   
  62.                 $datas = $this->getDatas($table);   
  63.                 $cdata = array();   
  64.                 foreach($datas as $data) {   
  65.                         foreach($data as $key => $value)   
  66.                                 $cdata[$key][] = $value;   
  67.                 }   
  68.                 foreach($fields as $element) {   
  69.                         $xml .= "\t\n";   
  70.                         foreach($cdata[$element['Field']] as $value) {   
  71.                                 $xml .= "\t\t{$value}\n";   
  72.                         }   
  73.                         $xml .= "\t\n";   
  74.                 }   
  75.                 $xml .= '';   
  76.                 return $xml;   
  77.         }   
  78.            
  79.         protected function getFields($table) {   
  80.                 $query = "SHOW FIELDS FROM {$table}";   
  81.                 $this->query($query);   
  82.                 return $this->fetchAll();   
  83.         }   
  84.            
  85.         protected function getDatas($table) {   
  86.                 $query = "SELECT * FROM {$table}";   
  87.                 $this->query($query);   
  88.                 return $this->fetchAll();   
  89.         }   
  90.            
  91.         protected function fetch() {   
  92.                 if(is_resource($this->result)) {   
  93.                         return mysql_fetch_assoc($this->result);   
  94.                 }   
  95.                 return false;   
  96.         }   
  97.            
  98.         protected function fetchAll() {   
  99.                 if(is_resource($this->result)) {   
  100.                         $return = array();   
  101.                         $row = NULL;   
  102.                         while($row = mysql_fetch_assoc($this->result)) {   
  103.                                 $return[] = $row;   
  104.                         }   
  105.                         return $return;   
  106.                 }   
  107.                 return false;   
  108.         }   
  109. }   
  110. ?>  

调用方法:

PHP代码

  1. $xml = new MySQL2XML(array('host'=>'localhost''username'=>'root''password'=>'''database'=>'mysql'));   
  2. $xml->setTables(array('wp_term_relationships','wp_terms'));//设置备份的表   
  3. $xml->setSaveFolder('datas/');//保存备份文件的文件夹   
  4. $xml->toXML();//备份开始   
  5. ?>  

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!