<?
// +----------------------------------------------------------------------+
// | xml2db |
// | Creating SQL Queries from a xml file |
// | Requirements: PHP5 with SimpleXML Support |
// +----------------------------------------------------------------------+
// | Author: Nico Puhlmann <hide@address.com> |
// +----------------------------------------------------------------------+
// $Id: class.xml2db.php,v 1.0 2oo5/o3/16 17:35:00 npuhlmann Exp $
class xml2db
{
private $root_element;
private $table;
private $data;
private $xml_file;
/**
* Constructor
*
* @param XML root element
*/
function xml2db($root)
{
$this->root_element = $root;
}
/**
* Sets the xml file to read
*
* @param file (can be local or remote http://..)
*/
function setXMLFile($xml_file)
{
$this->xml_file = $xml_file;
}
/**
* Sets the table for the sql insert
*
* @param the table
*/
function setTable($table)
{
$this->table = $table;
}
/**
* Sets the configuration data
*
* @param array configuration data
* @array colums: xml node, type, db field, attr name
*/
function setData($data)
{
if(!is_array($data)) return false;
$this->data = $data;
}
/**
* Build the query - Main Function
*
*/
function getQueries()
{
if (!file_exists($this->xml_file)) return false;
$c=0;
$xml = simplexml_load_file($this->xml_file);
foreach ($xml->{$this->root_element} as $item)
{
reset($this->data);
$ic=1;
$sql[$c] = "INSERT INTO " . $this->table ." SET ";
foreach($this->data as $d_array)
{
$sql[$c] .= $d_array[2] . " = '";
// node root attribuite
if ($d_array[1]=="attr" and empty($d_array[0]) and isset($item[ $d_array[3] ]))
{
$sql[$c] .= utf8_decode($item[ $d_array[3] ]);
}
// node attribuite
elseif ($d_array[1]=="attr" and !empty( $item->{$d_array[0]}[ $d_array[3] ] ))
{
$sql[$c] .= utf8_decode($item->{$d_array[0]}[ $d_array[3] ]);
}
// node contents
elseif ($d_array[1]=="node")
{
$sql[$c] .= utf8_decode($item->$d_array[0]);
}
$sql[$c] .= "'";
if($ic < count($this->data) )
{
$sql[$c] .= ", ";
}
$ic++;
}
$sql[$c] .= "\n";
$c++;
}
return $sql;
}
}
?>