require_once 'mysql2xml.php';
class XML2MySQL extends MySQL2XML {
private $XMLFiles = array();
private $tableName = NULL;
private $fields = array();
private $datas = array();
public function __construct($config = NULL) {
parent::__construct($config);
if(!function_exists('simplexml_load_file')) throw new Exception("Your server isn't suppost this class.");
}
public function setXMLFiles($file) {
if(is_array($file)) {
foreach($file as $f) {
if(file_exists($f)) {
$this->XMLFiles[] = $f;
return true;
} else return false;
}
} else {
if(file_exists($file)) {
$this->XMLFiles[] = $file;
return true;
} else return false;
}
}
public function getXMLFromFolder($dir) {
if(!is_dir($dir)) return false;
$dir = rtrim(str_replace("\\", "/", $dir), '/').'/';
$dp = @opendir($dir);
if(!$dp) throw new Exception("Can not open folder");
while(($f = readdir($dp)) !== false) {
if($f != '.' && $f != '..') {
if(!$this->setXMLFiles($dir.$f)) throw new Exception("Error:Files are not xml file or files are not exists.");
}
}
closedir($dp);
return true;
}
public function toMySQL() {
$buff = '';
foreach($this->XMLFiles as $xml) {
$this->getDataFromXML($xml);
$drop = 'DROP TABLE IF EXISTS `'.$this->tableName.'`;';
if($this->query($drop)) $buff .= 'Drop table ['.$this->tableName.'] success'."
\n";
else $buff .= 'Drop table ['.$this->tableName.'] fail'."
\n";
$pk = NULL;
$uk = NULL;
$sql = 'CREATE TABLE `'.$this->tableName."`(\n";
foreach($this->fields as $field) {
$sql .= '`'.$field['name'].'`'.' '.$field['type'].' '.($field['null'] == 'NO' ? 'NOT NULL ':'').(trim($field['extra']) != '' ? strtoupper($field['extra']).' ' : '').(trim($field['default']) != '' ? 'DEFAULT '."'".$field['default']."'" : '').','."\n";
if($field['key'] == 'PRI') $pk = $pk.(strpos($pk, 'PRIMARY KEY') !== FALSE ? '`'.$field['name'].'`,' : 'PRIMARY KEY (`'.$field['name'].'`,');
if($field['key'] == 'UNI') $uk = $uk.(strpos($uk, 'UNIQUE KEY') !== FALSE ? '`'.$field['name'].'`,' : "UNIQUE KEY `".$field['name']."` (`".$field['name']."`,");
$fields[$this->tableName][] = $field['name'];
}
if($pk !== NULL) {
$pk = rtrim($pk, ",")."),\n";
$sql .= $pk;
}
if($uk !== NULL) {
$uk = rtrim($uk, ",")."),\n";
$sql .= $uk;
}
$sql = rtrim($sql, ",\n");
$sql .= ');';
if($this->query($sql)) $buff .= 'Create table ['.$this->tableName.'] success'."
\n";
else $buff .= 'Create table ['.$this->tableName.'] fail'."
\n";
unset($sql);
$datas = 'INSERT INTO `'.$this->tableName.'` (';
foreach($fields as $table_name => $f) {
foreach($f as $element) {
$datas .= '`'.$element.'`,';
}
}
$this->datas = $this->r2l($this->datas);
$datas = rtrim($datas, ',').') VALUES ';
foreach($this->datas as $data) {
$datas .= "(";
foreach($data as $d) {
$datas .= "'".$d."',";
}
$datas = rtrim($datas, ',');
$datas .= '),';
}
$datas = rtrim($datas, ',').';';
if($this->query($datas)) $buff .= 'Insert data in table ['.$this->tableName.'] success'."
\n";
else $buff .= 'Insert data in table ['.$this->tableName.'] fail'."
\n";
unset($fields);
}
return $buff;
}
private function r2l($array) {
$temp = array();
for($i = 0; $i
for($j = 0; $j
$temp[$j][$i] = $array[$i][$j];
}
}
return $temp;
}
private function getDataFromXML($xml) {
//Use the SimpleXML Object
$this->tableName = substr(basename($xml), 0, strlen(basename($xml)) - 4);
$simplexml = simplexml_load_file($xml);
$fields = array();
$index = 0;
foreach($simplexml->children() as $e) {
$fields[$index]['name'] = (string)$e->attributes()->name;
$fields[$index]['type'] = (string)$e->attributes()->type;
$fields[$index]['null'] = (string)$e->attributes()->null;
$fields[$index]['key'] = (string)$e->attributes()->key;
$fields[$index]['default'] = (string)$e->attributes()->default;
$fields[$index]['extra'] = (string)$e->attributes()->extra;
$index++;
}
$this->fields = $fields;
$datas = array();
$index = 0;
foreach($simplexml->children() as $e) {
foreach($e->children() as $d) {
$datas[$index][] = (string)$d;
}
$index++;
}
$this->datas = $datas;
}
}
?>