2016-08-28 16 views
-1

がPHPで読み込まれます。これは、DBのクエリが含まれているXMLで、私は本当のクエリに変換したい: `PHPでxmlを解析すると、エラー

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.")"; 
    } 
} 

とカラム()クラス:

<?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);` 

このクラスを使用して、最初のクラスは、テーブル()です:

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



    ////////// 

} 

、これはインデックスファイルです:

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(); 
} 

結果: 表デバイスを作成(IDのINT真偽真 、名前VARCHAR(128)偽真 、一意ID VARCHAR(128)偽真 、ステータスVARCHAR(128)偽真 、最終更新日TIMESTAMP偽真 、positionid INT false真実)

あなたが見ることができるように、各列に5つの出力が必要ですが、最初の列は5であり、他は4です。xmlを列クラスに変換したいとき、エラーが私のループから来ているようです。しかし、私の問題を解決する方法を理解することができません

誰かがこの問題を手伝ってくれますか?

答えて

1

このコード行は重要です:

$has_ai = 'false';

$column->set_autoIncrement($col->getAttribute('autoIncrement'));

autoIncrement属性は最初のフィールドだけのために定義されているので、何をしなければならないことは要素がAUTOINCREMENT属性が含まれているかどうかを確認することです

if($col->hasAttribute('autoIncrement')) $has_ai = $col->getAttribute('autoIncrement');

$column->set_autoIncrement($has_ai);

またはあなたのsetメソッド更新することができます。

public function set_autoIncrement($inc) 
{ 
    $this->autoIncrement = (is_null($inc) || empty($inc)) ? 'false' : $inc; 
} 
+0

をいただき、ありがとうございますので、much.Iは、あなたが言ったことでしたが、いくつかの列にautoincreamentまたは主キーがありませんが、それはthem.whatのために真の設定あなたの意見では問題ですか?結果: 'create table devices(ID INT真true偽true 、名前VARCHAR(128)真偽真true 、ユニークID VARCHAR(128)真偽真 、ステータスVARCHAR(128)真偽真 、最終更新日TIMESTAMP真偽真 、positionid INT true false true) ' –

+0

まず、これらのCreate Tableコマンド(postgre、mysql、sql)をどのデータベースタイプで作成するのかを知る必要がありますか? –

+0

mysqlの場合。構文について考えているなら、修正する必要があることを知っています。 –