简体   繁体   中英

How to import XML to MySQL with parent and child elements?

Using the following query I can read all <vehicle> elements from the XML and save it into database but I want that each vehicle also store its parent <timestep> time so that I know which time step it belongs to.

query

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

I tired creating a table timesteps where I added a time column and all the other columns too but it gets the last vehicle from each timestep only!, while I want to get all the children.

Part of my 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>

You have to have a column named time in your vehicles table.

The official Documentation describes your case:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM