php中实现xml与mysql数据相互转换的方法
程序员文章站
2023-10-15 15:01:17
本文实例讲述了php中实现xml与mysql数据相互转换的方法。分享给大家供大家参考。具体分析如下:
这里提供了一个mysql2xml转换类,可以快速的把xml转换成my...
本文实例讲述了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;
}
}
?>
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();//备份开始
?>
$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程序设计有所帮助。