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

前端之家收集整理的这篇文章主要介绍了php中实现xml与mysql数据相互转换的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

MysqL2xml.PHP文件,用于备份MysqL数据的,代码如下:

代码如下:
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" ?>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 .= "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{$value}n";
}
$xml .= "tn";
}
$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;
}
}
?>


调用方法,代码如下:
<div class="codetitle"><a style="CURSOR: pointer" data="82131" class="copybut" id="copybut82131" onclick="doCopy('code82131')"> 代码如下:
<div class="codebody" id="code82131"><?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程序设计有所帮助。

原文链接:https://www.f2er.com/php/23014.html

猜你在找的PHP相关文章