php中实现xml与mysql数据相互转换的方法

yipeiwu_com5年前Mysql基础

本文实例讲述了php中实现xml与mysql数据相互转换的方法。分享给大家供大家参考。具体分析如下:

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

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

复制代码 代码如下:
<?php
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 = "<?xml version="1.0" encoding="utf-8" ?>n<datas>n";
                $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 .= "t<fields name="{$element['Field']}" type="{$element['Type']}" null="{$element['Null']}" key="{$element['Key']}" default="{$element['Default']}" extra="{$element['Extra']}">n";
                        foreach($cdata[$element['Field']] as $value) {
                                $xml .= "tt<data>{$value}</data>n";
                        }
                        $xml .= "t</fields>n";
                }
                $xml .= '</datas>';
                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;
        }
}
?>

调用方法,代码如下:
复制代码 代码如下:
<?php
$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程序设计有所帮助。

相关文章

php写的带缓存数据功能的mysqli类

复制代码 代码如下: <?php /** * Mysqli类 */ class db_mysqli { protected $mysqli; protected $sql; pro...

mysql 查询指定日期时间内sql语句实现原理与代码

在数据库教程设计时必须注意时间字段为int(11)这样,保存在数据库的是一个数字型日期时间戳,我们可以用mktime函数求出当前日期的时间戳进行加减就OK了,下面看实例 //一个月 复制...

在VS2008中编译MYSQL5.1.48的方法

1、 下载MYSQL5.1.48源码,CMAKE,VS2008 2、 安装CMAKE和VS2008,解压MYSQL5.1.48到D:\mysql 3、 打开CMD;CD D:\mysql...

PHP+Mysql+jQuery实现发布微博程序 jQuery篇

PHP+Mysql+jQuery实现发布微博程序 jQuery篇

该应用实现了即时统计输入字数,并且通过ajax与后台交互,将输入内容插入到话题列表中。我将整个流程分为两部分,本文讲解第一部分jquery实现前端交互操作。 首先查看示例:DEMO X...

ExtJS与PHP、MySQL实现存储的方法

ExtJS与PHP、MySQL实现存储的方法

1 建立数据库、注册表 复制代码 代码如下: create database db_register; create table db_register.tb_register( reg...