dsa4214 2016-08-28 21:13
浏览 36
已采纳

在php中解析xml会返回错误

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?

  • 写回答

1条回答 默认 最新

  • doutuoben6908 2016-08-28 21:24
    关注

    This line of code is critical:

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

    autoIncrement attribute is defined only for first field, so what you have to do is to check if element contains autoIncrement attribute:

    $has_ai = 'false';

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

    $column->set_autoIncrement($has_ai);

    Or you can update your set method:

    public function set_autoIncrement($inc)
    {
        $this->autoIncrement = (is_null($inc) || empty($inc)) ? 'false' : $inc;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)