当前位置:首页 > PHP教程 > php高级应用 > 列表

php中实现xml与mysql数据相互转换代码

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-11 08:53:14 浏览: 评论:0 

本文章提供了一个mysql2xml转换类,可以快速的把xml转换成mysql同时反之也可以把mysql转换成xml,下面看代码.

mysql2xml.php类文件,用于备份MySQL数据的,代码如下:

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

调用方法,代码如下:

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

Tags: xmll数据转换 mysql数据转换

分享到: