How to Convert xml and mysql data in php

Source: Internet
Author: User
This article describes how to convert xml and mysql data in php. the instance encapsulates a class file to convert XML and MySQL data, it has some reference value. if you need it, you can refer to the following example to describe how to convert xml and mysql data in php. Share it with you for your reference. The specific analysis is as follows:

Here we provide a mysql2xml conversion class, which can quickly convert xml into mysql, and vice versa, mysql can also be converted into xml. see the code below.

Mysql2xml. php file used to back up MySQL data. the code is as follows:

The code is as follows:

<? 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 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 N ";
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;
}
}
?>


The code is as follows:

The code is as follows:

<? Php
$ Xml = new MySQL2XML (array ('host' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'mysql '));
$ Xml-> setTables (array ('WP _ term_relationships ', 'WP _ terms'); // you can specify a backup table.
$ Xml-> setSaveFolder ('datas/'); // Save the folder of the backup file
$ Xml-> toXML (); // backup start
?>

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.