php中實現(xiàn)xml與mysql數(shù)據(jù)相互轉(zhuǎn)換的方法
更新時間:2014年12月25日 14:20:06 投稿:shichen2014
這篇文章主要介紹了php中實現(xiàn)xml與mysql數(shù)據(jù)相互轉(zhuǎn)換的方法,實例封裝了一個類文件,可實現(xiàn)XML與MySQL數(shù)據(jù)的相互轉(zhuǎn)換,具有一定的參考借鑒價值,需要的朋友可以參考下
本文實例講述了php中實現(xiàn)xml與mysql數(shù)據(jù)相互轉(zhuǎn)換的方法。分享給大家供大家參考。具體分析如下:
這里提供了一個mysql2xml轉(zhuǎn)換類,可以快速的把xml轉(zhuǎn)換成mysql同時反之也可以把mysql轉(zhuǎn)換成xml,下面看代碼.
mysql2xml.php類文件,用于備份MySQL數(shù)據(jù)的,代碼如下:
復制代碼 代碼如下:
<?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;
}
}
?>
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;
}
}
?>
調(diào)用方法,代碼如下:
復制代碼 代碼如下:
<?php
$xml = new MySQL2XML(array('host'=>'localhost', 'username'=>'root', 'password'=>'', 'database'=>'mysql'));
$xml->setTables(array('wp_term_relationships','wp_terms'));//設置備份的表
$xml->setSaveFolder('datas/');//保存?zhèn)浞菸募奈募A
$xml->toXML();//備份開始
?>
$xml = new MySQL2XML(array('host'=>'localhost', 'username'=>'root', 'password'=>'', 'database'=>'mysql'));
$xml->setTables(array('wp_term_relationships','wp_terms'));//設置備份的表
$xml->setSaveFolder('datas/');//保存?zhèn)浞菸募奈募A
$xml->toXML();//備份開始
?>
希望本文所述對大家的php程序設計有所幫助。
您可能感興趣的文章:
- PHP實現(xiàn)動態(tài)添加XML中數(shù)據(jù)的方法
- PHP解析xml格式數(shù)據(jù)工具類示例
- PHP操作XML作為數(shù)據(jù)庫的類
- PHP中使用xmlreader讀取xml數(shù)據(jù)示例
- PHP生成和獲取XML格式數(shù)據(jù)的方法
- php操作XML、讀取數(shù)據(jù)和寫入數(shù)據(jù)的實現(xiàn)代碼
- php處理復雜xml數(shù)據(jù)示例
- PHP XML備份Mysql數(shù)據(jù)庫
- php操作xml并將其插入數(shù)據(jù)庫的實現(xiàn)方法
- PHP XML數(shù)據(jù)解析代碼
- PHP實現(xiàn)動態(tài)刪除XML數(shù)據(jù)的方法示例
相關文章
php中字符集轉(zhuǎn)換iconv函數(shù)使用總結(jié)
這篇文章主要介紹了php中字符集轉(zhuǎn)換iconv函數(shù)使用總結(jié),本文同時介紹了mb_convert_encoding函數(shù),需要的朋友可以參考下2014-10-10PHP+Mysql+jQuery實現(xiàn)動態(tài)展示信息
在本文中,我將介紹如何在頁面上實現(xiàn)動態(tài)展示用戶發(fā)表的信息,將用戶發(fā)表的信息逐條播放展示。該效果可以在展示系統(tǒng)動態(tài)、商品評論等場景應用2011-10-10PHP在終端中實現(xiàn)進度條來處理數(shù)據(jù)的示例詳解
在PHP中,有時候你需要在終端中處理大量數(shù)據(jù)或執(zhí)行長時間運行的任務,同時希望能夠?qū)崟r跟蹤任務的進度,在 PHP 中,有時候你需要在終端中處理大量數(shù)據(jù)或執(zhí)行長時間運行的任務,同時希望能夠?qū)崟r跟蹤任務的進度,需要的朋友可以參考下2023-10-10