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 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献