如何使用父元素和子元素将XML导入MySQL?

时间:2018-01-29 05:24:22

标签: mysql xml

使用以下查询,我可以读取XML中的所有<vehicle>元素并将其保存到数据库中,但我希望每辆车也存储其父<timestep> time,以便我知道哪个它属于的时间步。

查询

LOAD XML LOCAL INFILE 'vehicle.xml'
INTO TABLE vehicles
ROWS IDENTIFIED BY '<vehicle>'

我厌倦了创建一个表格时间步,我在其中添加了一个时间列和所有其他列,但它只从每个时间步长获得最后一个车辆!,而我想要让所有的孩子。

我的部分XML

<?xml version="1.0" encoding="UTF-8"?>
<fcd-export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/fcd_file.xsd">
    <timestep time="0.00">
        <vehicle id="0" x="3654.27" y="3699.20" angle="327.39" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="21059650#7_0" slope="0.00"/>
    </timestep>
    <timestep time="1.00">
        <vehicle id="0" x="3653.49" y="3700.41" angle="327.39" type="DEFAULT_VEHTYPE" speed="1.44" pos="6.54" lane="21059650#7_0" slope="0.00"/>
        <vehicle id="1" x="2592.95" y="3497.59" angle="60.95" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="22408082_0" slope="0.00"/>
    </timestep>
    <timestep time="2.00">
        <vehicle id="0" x="3651.81" y="3703.04" angle="327.39" type="DEFAULT_VEHTYPE" speed="3.12" pos="9.66" lane="21059650#7_0" slope="0.00"/>
        <vehicle id="1" x="2595.17" y="3498.82" angle="60.95" type="DEFAULT_VEHTYPE" speed="2.54" pos="7.64" lane="22408082_0" slope="0.00"/>
        <vehicle id="2" x="4551.99" y="4411.11" angle="328.95" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="23657587_0" slope="0.00"/>
    </timestep>
    <timestep time="3.00">
        <vehicle id="0" x="3649.14" y="3707.21" angle="327.39" type="DEFAULT_VEHTYPE" speed="4.95" pos="14.61" lane="21059650#7_0" slope="0.00"/>
        <vehicle id="1" x="2599.61" y="3501.29" angle="60.95" type="DEFAULT_VEHTYPE" speed="5.08" pos="12.73" lane="22408082_0" slope="0.00"/>
        <vehicle id="2" x="4550.98" y="4412.79" angle="328.95" type="DEFAULT_VEHTYPE" speed="1.96" pos="7.06" lane="23657587_0" slope="0.00"/>
        <vehicle id="3" x="4460.55" y="5390.34" angle="58.38" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="-22975228#1_0" slope="0.00"/>
    </timestep>
    <timestep time="4.00">
        <vehicle id="0" x="3645.25" y="3713.30" angle="327.39" type="DEFAULT_VEHTYPE" speed="7.23" pos="21.83" lane="21059650#7_0" slope="0.00"/>
        <vehicle id="1" x="2605.71" y="3504.68" angle="60.95" type="DEFAULT_VEHTYPE" speed="6.98" pos="19.70" lane="22408082_0" slope="0.00"/>
        <vehicle id="2" x="4549.12" y="4415.87" angle="328.95" type="DEFAULT_VEHTYPE" speed="3.59" pos="10.66" lane="23657587_0" slope="0.00"/>
        <vehicle id="3" x="4461.94" y="5391.20" angle="58.38" type="DEFAULT_VEHTYPE" speed="1.63" pos="6.73" lane="-22975228#1_0" slope="0.00"/>
        <vehicle id="4" x="6607.38" y="6648.96" angle="238.83" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="22983344_0" slope="0.00"/>
    </timestep>
    <timestep time="5.00">
        <vehicle id="0" x="3640.37" y="3720.81" angle="326.68" type="DEFAULT_VEHTYPE" speed="8.95" pos="30.78" lane="21059650#7_0" slope="0.00"/>
        <vehicle id="1" x="2613.55" y="3509.03" angle="60.95" type="DEFAULT_VEHTYPE" speed="8.97" pos="28.67" lane="22408082_0" slope="0.00"/>
        <vehicle id="2" x="4545.93" y="4421.17" angle="328.95" type="DEFAULT_VEHTYPE" speed="6.19" pos="16.84" lane="23657587_0" slope="0.00"/>
        <vehicle id="3" x="4465.05" y="5393.11" angle="58.38" type="DEFAULT_VEHTYPE" speed="3.64" pos="10.37" lane="-22975228#1_0" slope="0.00"/>
        <vehicle id="4" x="6605.78" y="6648.00" angle="238.83" type="DEFAULT_VEHTYPE" speed="1.86" pos="6.96" lane="22983344_0" slope="0.00"/>
        <vehicle id="5" x="6028.95" y="3131.17" angle="40.09" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="264312282#1_0" slope="0.00"/>
    </timestep>
</fcd-export>

1 个答案:

答案 0 :(得分:1)

您的time表格中必须有一个名为vehicles的列。

官方Documentation描述了您的案例:

  

使用ROWS IDENTIFIED BY '<tagname>'子句,可以   将数据从同一XML文件导入到具有不同的数据库表中   定义。对于此示例,假设您有一个名为的文件   address.xml包含以下XML:

<?xml version="1.0"?>

<list>
  <person person_id="1">
    <fname>Robert</fname>
    <lname>Jones</lname>
    <address address_id="1" street="Mill Creek Road" zip="45365" city="Sidney"/>
    <address address_id="2" street="Main Street" zip="28681" city="Taylorsville"/>
  </person>

  <person person_id="2">
    <fname>Mary</fname>
    <lname>Smith</lname>
    <address address_id="3" street="River Road" zip="80239" city="Denver"/>
    <!-- <address address_id="4" street="North Street" zip="37920" city="Knoxville"/> -->
  </person>

</list>
  

现在使用以下内容在测试数据库中创建一个地址表   CREATE TABLE语句:

CREATE TABLE address (
    address_id INT NOT NULL PRIMARY KEY,
    person_id INT NULL,
    street VARCHAR(40) NULL,
    zip INT NULL,
    city VARCHAR(40) NULL,
    created TIMESTAMP
);

...

  

要将元素中的数据导入地址表,   使用此处显示的LOAD XML语句:

mysql> LOAD XML LOCAL INFILE 'address.xml'
    ->   INTO TABLE address
    ->   ROWS IDENTIFIED BY '<address>';
Query OK, 3 rows affected (0.00 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0
  

您可以看到使用SELECT语句导入数据   就像这个:

mysql> SELECT * FROM address;
+------------+-----------+-----------------+-------+--------------+---------------------+
| address_id | person_id | street          | zip   | city         | created             |
+------------+-----------+-----------------+-------+--------------+---------------------+
|          1 |         1 | Mill Creek Road | 45365 | Sidney       | 2007-07-24 17:37:37 |
|          2 |         1 | Main Street     | 28681 | Taylorsville | 2007-07-24 17:37:37 |
|          3 |         2 | River Road      | 80239 | Denver       | 2007-07-24 17:37:37 |
+------------+-----------+-----------------+-------+--------------+---------------------+
3 rows in set (0.00 sec)
  

XML注释中包含的<address>元素的数据   没有导入。但是,由于中有person_id列   address表,父项的person_id属性的值   每个<person>的{​​{1}}元素都会导入<address>   表