How to convert xml and mysql data into each other in php, xmlmysql
The example in this article describes the method of converting xml and mysql data in php. Share it with everyone for your reference. The specific analysis is as follows:
A mysql2xml conversion class is provided here, which can quickly convert xml to mysql and vice versa. See the code below.
mysql2xml.php class file is used to back up MySQL data. The code is as follows:
Copy code The code is as follows:
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 = "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 .= "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;
}
}
?>
调用方法,代码如下:
复制代码 代码如下:
$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程序设计有所帮助。
http://www.bkjia.com/PHPjc/932476.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/932476.htmlTechArticlephp中实现xml与mysql数据相互转换的方法,xmlmysql 本文实例讲述了php中实现xml与mysql数据相互转换的方法。分享给大家供大家参考。具体分析如...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn