loading xml in php. It is a xml that contains DB query, I want to convert them to real queries:
<?php
$myXMLData ="<?xml version='1.0' encoding='UTF-8'?>
<createTable tableName='devices'>
<column name='id' type='INT' autoIncrement='true'>
<constraints primaryKey='true' />
</column>
<column name='name' type='VARCHAR(128)'>
<constraints nullable='false' />
</column>
<column name='uniqueid' type='VARCHAR(128)'>
<constraints nullable='false' />
</column>
<column name='status' type='VARCHAR(128)' />
<column name='lastupdate' type='TIMESTAMP' />
<column name='positionid' type='INT' />
</createTable>"
$dom = new DOMDocument();
$dom->loadXML($myXMLData);`
using this classes, first class is table():`
class Table
{
var $tableName;
var $columns= array();
public function set_tableName($name)
{
$this->tableName=$name;
}
public function get_tableName()
{
return $this->tableName;
}
public function add_column($column)
{
array_push($this->columns,$column);
}
public function addTableToDatabase()
{
$query= "create table ".$this->get_tableName()."(";
for($i=0;$i<count($this->columns);$i++)
{
$name= $this->columns[$i]->get_name()." ";
$type= $this->columns[$i]->get_type()." ";
$inc= $this->columns[$i]->get_autoIncrement()." ";
$null= $this->columns[$i]->get_nullable()." ";
$key= $this->columns[$i]->get_primaryKey()."<br> ";
$query.=$name." ".$type." ".$inc." ".$null." ".$key.", ";
}
$query=rtrim($query, ",");
echo $query.")";
}
}
and column() class:
<?php
class Column
{
var $name="";
var $type="";
var $autoIncrement="";
var $nullable="";
var $primaryKey="";
////////////
public function set_name($name)
{
$this->name=$name;
}
public function get_name()
{
return $this->name;
}
////////////////
public function set_type($type)
{
$this->type=$type;
}
public function get_type()
{
return $this->type;
}
//////////
public function set_autoIncrement($inc)
{
$this->autoIncrement=$inc;
}
public function get_autoIncrement()
{
return $this->autoIncrement;
}
////////////
public function set_nullable($null)
{
$this->nullable=$null;
}
public function get_nullable()
{
return $this->nullable;
}
///////////
public function set_primaryKey($key)
{
$this->primaryKey=$key;
}
public function get_primaryKey()
{
return $this->primaryKey;
}
/////////
public function Column()
{
$this->name=null;
$this->type=null;
$this->autoIncrement=null;
$this->nullable=null;
$this->primaryKey=null;
}
//////////
}
and this is the index file:
foreach($dom->getElementsByTagName('createTable') as $name)
{
$table =new Table();
$table->set_tableName($name->getAttribute('tableName'));
foreach($name->getElementsByTagName('column') as $col)
{
$column=new Column();
$column->set_name($col->getAttribute('name'));
$column->set_type($col->getAttribute('type'));
$column->set_autoIncrement($col->getAttribute('autoIncrement'));
foreach ($name->getElementsByTagName('constraints') as $const)
{
if($const->getAttribute('nullable'))
{
$column->set_nullable($const->getAttribute('nullable'));
}
if($const->getAttribute('primaryKey'))
{
$column->set_primaryKey($const->getAttribute('primaryKey'));
}
}
$table->add_column($column);
}
$table->addTableToDatabase();
}
result: create table devices(id INT true false true , name VARCHAR(128) false true , uniqueid VARCHAR(128) false true , status VARCHAR(128) false true , lastupdate TIMESTAMP false true , positionid INT false true)
As you can see it must have 5 output for every column but first column is 5 and others are 4. It seems that the error comes from my loops when I want t parse xml into column class. But I can not figure out how to manage to fix my problem
Can someone please help me with this problem?