phpにおけるxmlとmysqlデータの相互変換を実現する方法

6139 ワード

この例では、phpにおけるxmlとmysqlデータの相互変換を実現する方法について説明する.皆さんの参考にしてください.具体的な分析は以下の通りである.
ここではmysql 2 xml変換クラスを提供し、xmlをmysqlに迅速に変換するとともに、逆にmysqlをxmlに変換することもできる.以下、コードを見る.
mysql2xml.phpクラスファイル、MySQLデータのバックアップに使用されるコードは以下の通りです.

    class MySQL2XML { 
  
        protected $conn;
        protected $result;
        protected $tables;
        protected $saveFolder = 'datas/';
        
        public function __construct($config = NULL) {
                if($config !== NULL && is_array($config)) {
                        $this->connect($config);
                }
        }
        
        public function connect($config) {
                $this->conn = mysql_connect($config['host'], $config['username'], $config['password']);
                if($this->conn) {
                        mysql_select_db($config['database']);
                        return true;
                }
                return false;
        }
        
        public function setSaveFolder($folder) {
                if(is_dir($folder)) {
                        $this->saveFolder = rtrim(str_replace("\", "/", $folder),'/');
                        return true;
                }
                return false;
        }
        
        public function setTables($tables) {
                if(is_array($tables)) {
                        $this->tables = $tables;
                        return true;
                }
                return false;
        }
        
        public function query($query) {
                if(!isset($query) || trim($query) == '') return false;
                $this->result = mysql_query($query);
                if($this->result) return true;
                return false;
        }
        
        public function toXML() {
                if(!isset($this->tables)) return false;
                foreach($this->tables as $table) {
                        $file = $this->saveFolder.$table.'.xml';
                        $fp = @fopen($file, 'w');
                        if(!$fp) exit('Can not write file');
                        fwrite($fp, $this->tableToXML($table));
                        fclose($fp);
                        unset($fp);
                }
                return true;
        }
        
        public function tableToXML($table) {
                header("content-type:text/xml;charset=utf-8");
                $xml = "nn";
                $fields = $this->getFields($table);
                $datas = $this->getDatas($table);
                $cdata = array();
                foreach($datas as $data) {
                        foreach($data as $key => $value)
                                $cdata[$key][] = $value;
                }
                foreach($fields as $element) {
                        $xml .= "tn";
                        foreach($cdata[$element['Field']] as $value) {
                                $xml .= "tt{$value}n";
                        }
                        $xml .= "t
n";
                }
                $xml .= '
';
                return $xml;
        }
        
        protected function getFields($table) {
                $query = "SHOW FIELDS FROM {$table}";
                $this->query($query);
                return $this->fetchAll();
        }
        
        protected function getDatas($table) {
                $query = "SELECT * FROM {$table}";
                $this->query($query);
                return $this->fetchAll();
        }
        
        protected function fetch() {
                if(is_resource($this->result)) {
                        return mysql_fetch_assoc($this->result);
                }
                return false;
        }
        
        protected function fetchAll() {
                if(is_resource($this->result)) {
                        $return = array();
                        $row = NULL;
                        while($row = mysql_fetch_assoc($this->result)) {
                                $return[] = $row;
                        }
                        return $return;
                }
                return false;
        }
}
?>

呼び出し方法、コードは次のとおりです.

    $xml = new MySQL2XML(array('host'=>'localhost', 'username'=>'root', 'password'=>'', 'database'=>'mysql')); 
  
$xml->setTables(array('wp_term_relationships','wp_terms'));//
$xml->setSaveFolder('datas/');//
$xml->toXML();//
?>

本稿で述べたphpプログラム設計に役立つことを願っています.