• 技术文章 >后端开发 >php教程

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

    2016-05-16 20:27:18原创394
    本文实例讲述了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("\", "//m.sbmmt.com/m/", $folder),'//m.sbmmt.com/m/');
    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" ?>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;
    }
    }
    ?>

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

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php xml mysql 数据 转换 方法
    上一篇:php将图片保存入mysql数据库失败的解决方法_php技巧 下一篇:PHP调用MySQL存储过程并返回值的方法_php技巧
    PHP编程就业班

    相关文章推荐

    • 面试官:列举几种PHP拓展的实现手段及其性能比较?• GD输出汉字的函数的分析_PHP• javascript - 哪里有可以下载的股票历史数据?• Yii rules常用规则示例_php实例• 日常整理PHP中简单的图形处理(经典)_php实例

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网