gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:38:59 25 4
gpt4 key购买 nike

使用以下查询我可以读取所有 <vehicle>来自 XML 的元素并将其保存到数据库中,但我希望每辆车也存储其父项 <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>

最佳答案

您必须有一个名为 time 的列在你的vehicles表。

官方Documentation描述你的情况:

Using a ROWS IDENTIFIED BY '<tagname>' clause, it is possible to import data from the same XML file into database tables with different definitions. For this example, suppose that you have a file named address.xml which contains the following 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>

Now create an address table in the test database using the following CREATE TABLE statement:

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

...

To import the data from the elements into the address table, use the LOAD XML statement shown here:

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

You can see that the data was imported using a SELECT statement such as this one:

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)

The data from the <address> element that is enclosed in XML comments is not imported. However, since there is a person_id column in the address table, the value of the person_id attribute from the parent <person> element for each <address> is imported into the address table.

关于mysql - 如何使用父子元素将 XML 导入 MySQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48494860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com