Rechercher dans le manuel MySQL

13.2.8 LOAD XML Syntax

  1. LOAD XML
  2.     [LOW_PRIORITY | CONCURRENT] [LOCAL]
  3.     INFILE 'file_name'
  4.     [REPLACE | IGNORE]
  5.     INTO TABLE [db_name.]tbl_name
  6.     [CHARACTER SET charset_name]
  7.     [ROWS IDENTIFIED BY '<tagname>']
  8.     [IGNORE number {LINES | ROWS}]
  9.     [(field_name_or_user_var
  10.         [, field_name_or_user_var] ...)]
  11.     [SET col_name={expr | DEFAULT},
  12.         [, col_name={expr | DEFAULT}] ...]

The LOAD XML statement reads data from an XML file into a table. The file_name must be given as a literal string. The tagname in the optional ROWS IDENTIFIED BY clause must also be given as a literal string, and must be surrounded by angle brackets (< and >).

LOAD XML acts as the complement of running the mysql client in XML output mode (that is, starting the client with the --xml option). To write data from a table to an XML file, you can invoke the mysql client with the --xml and -e options from the system shell, as shown here:

shell> mysql --xml -e 'SELECT * FROM mydb.mytable' > file.xml

To read the file back into a table, use LOAD XML. By default, the <row> element is considered to be the equivalent of a database table row; this can be changed using the ROWS IDENTIFIED BY clause.

This statement supports three different XML formats:

  • Column names as attributes and column values as attribute values:

    <row column1="value1" column2="value2" .../>
  • Column names as tags and column values as the content of these tags:

    <row>
      <column1>value1</column1>
      <column2>value2</column2>
    </row>
  • Column names are the name attributes of <field> tags, and values are the contents of these tags:

    <row>
      <field name='column1'>value1</field>
      <field name='column2'>value2</field>
    </row>

    This is the format used by other MySQL tools, such as mysqldump.

All three formats can be used in the same XML file; the import routine automatically detects the format for each row and interprets it correctly. Tags are matched based on the tag or attribute name and the column name.

The following clauses work essentially the same way for LOAD XML as they do for LOAD DATA:

  • LOW_PRIORITY or CONCURRENT

  • LOCAL

  • REPLACE or IGNORE

  • CHARACTER SET

  • SET

See Section 13.2.7, “LOAD DATA Syntax”, for more information about these clauses.

(field_name_or_user_var, ...) is a list of one or more comma-separated XML fields or user variables. The name of a user variable used for this purpose must match the name of a field from the XML file, prefixed with @. You can use field names to select only desired fields. User variables can be employed to store the corresponding field values for subsequent re-use.

The IGNORE number LINES or IGNORE number ROWS clause causes the first number rows in the XML file to be skipped. It is analogous to the LOAD DATA statement's IGNORE ... LINES clause.

Suppose that we have a table named person, created as shown here:

  1. USE test;
  2.  
  3. CREATE TABLE person (
  4.     person_id INT NOT NULL PRIMARY KEY,
  5.     fname VARCHAR(40) NULL,
  6.     lname VARCHAR(40) NULL,
  7.     created TIMESTAMP
  8. );

Suppose further that this table is initially empty.

Now suppose that we have a simple XML file person.xml, whose contents are as shown here:

<list>
  <person person_id="1" fname="Kapek" lname="Sainnouine"/>
  <person person_id="2" fname="Sajon" lname="Rondela"/>
  <person person_id="3"><fname>Likame</fname><lname>Örrtmons</lname></person>
  <person person_id="4"><fname>Slar</fname><lname>Manlanth</lname></person>
  <person><field name="person_id">5</field><field name="fname">Stoma</field>
    <field name="lname">Milu</field></person>
  <person><field name="person_id">6</field><field name="fname">Nirtam</field>
    <field name="lname">Sklöd</field></person>
  <person person_id="7"><fname>Sungam</fname><lname>Dulbåd</lname></person>
  <person person_id="8" fname="Sraref" lname="Encmelt"/>
</list>

Each of the permissible XML formats discussed previously is represented in this example file.

To import the data in person.xml into the person table, you can use this statement:

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

Here, we assume that person.xml is located in the MySQL data directory. If the file cannot be found, the following error results:

ERROR 2 (HY000): File '/person.xml' not found (Errcode: 2)

The ROWS IDENTIFIED BY '<person>' clause means that each <person> element in the XML file is considered equivalent to a row in the table into which the data is to be imported. In this case, this is the person table in the test database.

As can be seen by the response from the server, 8 rows were imported into the test.person table. This can be verified by a simple SELECT statement:

  1. mysql> SELECT * FROM person;
  2. +-----------+--------+------------+---------------------+
  3. | person_id | fname  | lname      | created             |
  4. +-----------+--------+------------+---------------------+
  5. |         1 | Kapek  | Sainnouine | 2007-07-13 16:18:47 |
  6. |         2 | Sajon  | Rondela    | 2007-07-13 16:18:47 |
  7. |         3 | Likame | Örrtmons   | 2007-07-13 16:18:47 |
  8. |         4 | Slar   | Manlanth   | 2007-07-13 16:18:47 |
  9. |         5 | Stoma  | Nilu       | 2007-07-13 16:18:47 |
  10. |         6 | Nirtam | Sklöd      | 2007-07-13 16:18:47 |
  11. |         7 | Sungam | Dulbåd     | 2007-07-13 16:18:47 |
  12. |         8 | Sreraf | Encmelt    | 2007-07-13 16:18:47 |
  13. +-----------+--------+------------+---------------------+
  14. 8 rows in set (0.00 sec)

This shows, as stated earlier in this section, that any or all of the 3 permitted XML formats may appear in a single file and be read using LOAD XML.

The inverse of the import operation just shown—that is, dumping MySQL table data into an XML file—can be accomplished using the mysql client from the system shell, as shown here:

shell> mysql --xml -e "SELECT * FROM test.person" > person-dump.xml
shell> cat person-dump.xml
<?xml version="1.0"?>

<resultset statement="SELECT * FROM test.person" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <row>
	<field name="person_id">1</field>
	<field name="fname">Kapek</field>
	<field name="lname">Sainnouine</field>
  </row>

  <row>
	<field name="person_id">2</field>
	<field name="fname">Sajon</field>
	<field name="lname">Rondela</field>
  </row>

  <row>
	<field name="person_id">3</field>
	<field name="fname">Likema</field>
	<field name="lname">Örrtmons</field>
  </row>

  <row>
	<field name="person_id">4</field>
	<field name="fname">Slar</field>
	<field name="lname">Manlanth</field>
  </row>

  <row>
	<field name="person_id">5</field>
	<field name="fname">Stoma</field>
	<field name="lname">Nilu</field>
  </row>

  <row>
	<field name="person_id">6</field>
	<field name="fname">Nirtam</field>
	<field name="lname">Sklöd</field>
  </row>

  <row>
	<field name="person_id">7</field>
	<field name="fname">Sungam</field>
	<field name="lname">Dulbåd</field>
  </row>

  <row>
	<field name="person_id">8</field>
	<field name="fname">Sreraf</field>
	<field name="lname">Encmelt</field>
  </row>
</resultset>
Note

The --xml option causes the mysql client to use XML formatting for its output; the -e option causes the client to execute the SQL statement immediately following the option. See Section 4.5.1, “mysql — The MySQL Command-Line Client”.

You can verify that the dump is valid by creating a copy of the person table and importing the dump file into the new table, like this:

  1. mysql> USE test;
  2. mysql> CREATE TABLE person2 LIKE person;
  3. Query OK, 0 rows affected (0.00 sec)
  4.  
  5. mysql> LOAD XML LOCAL INFILE 'person-dump.xml'
  6.     ->   INTO TABLE person2;
  7. Query OK, 8 rows affected (0.01 sec)
  8. Records: 8  Deleted: 0  Skipped: 0  Warnings: 0
  9.  
  10. mysql> SELECT * FROM person2;
  11. +-----------+--------+------------+---------------------+
  12. | person_id | fname  | lname      | created             |
  13. +-----------+--------+------------+---------------------+
  14. |         1 | Kapek  | Sainnouine | 2007-07-13 16:18:47 |
  15. |         2 | Sajon  | Rondela    | 2007-07-13 16:18:47 |
  16. |         3 | Likema | Örrtmons   | 2007-07-13 16:18:47 |
  17. |         4 | Slar   | Manlanth   | 2007-07-13 16:18:47 |
  18. |         5 | Stoma  | Nilu       | 2007-07-13 16:18:47 |
  19. |         6 | Nirtam | Sklöd      | 2007-07-13 16:18:47 |
  20. |         7 | Sungam | Dulbåd     | 2007-07-13 16:18:47 |
  21. |         8 | Sreraf | Encmelt    | 2007-07-13 16:18:47 |
  22. +-----------+--------+------------+---------------------+
  23. 8 rows in set (0.00 sec)

There is no requirement that every field in the XML file be matched with a column in the corresponding table. Fields which have no corresponding columns are skipped. You can see this by first emptying the person2 table and dropping the created column, then using the same LOAD XML statement we just employed previously, like this:

  1. mysql> TRUNCATE person2;
  2. Query OK, 8 rows affected (0.26 sec)
  3.  
  4. mysql> ALTER TABLE person2 DROP COLUMN created;
  5. Query OK, 0 rows affected (0.52 sec)
  6. Records: 0  Duplicates: 0  Warnings: 0
  7.  
  8. mysql> SHOW CREATE TABLE person2\G
  9. *************************** 1. row ***************************
  10.        Table: person2
  11. Create Table: CREATE TABLE `person2` (
  12.   `person_id` int(11) NOT NULL,
  13.   `fname` varchar(40) DEFAULT NULL,
  14.   `lname` varchar(40) DEFAULT NULL,
  15.   PRIMARY KEY (`person_id`)
  16. 1 row in set (0.00 sec)
  17.  
  18. mysql> LOAD XML LOCAL INFILE 'person-dump.xml'
  19.     ->   INTO TABLE person2;
  20. Query OK, 8 rows affected (0.01 sec)
  21. Records: 8  Deleted: 0  Skipped: 0  Warnings: 0
  22.  
  23. mysql> SELECT * FROM person2;
  24. +-----------+--------+------------+
  25. | person_id | fname  | lname      |
  26. +-----------+--------+------------+
  27. |         1 | Kapek  | Sainnouine |
  28. |         2 | Sajon  | Rondela    |
  29. |         3 | Likema | Örrtmons   |
  30. |         4 | Slar   | Manlanth   |
  31. |         5 | Stoma  | Nilu       |
  32. |         6 | Nirtam | Sklöd      |
  33. |         7 | Sungam | Dulbåd     |
  34. |         8 | Sreraf | Encmelt    |
  35. +-----------+--------+------------+
  36. 8 rows in set (0.00 sec)

The order in which the fields are given within each row of the XML file does not affect the operation of LOAD XML; the field order can vary from row to row, and is not required to be in the same order as the corresponding columns in the table.

As mentioned previously, you can use a (field_name_or_user_var, ...) list of one or more XML fields (to select desired fields only) or user variables (to store the corresponding field values for later use). User variables can be especially useful when you want to insert data from an XML file into table columns whose names do not match those of the XML fields. To see how this works, we first create a table named individual whose structure matches that of the person table, but whose columns are named differently:

  1. mysql> CREATE TABLE individual (
  2.     ->     individual_id INT NOT NULL PRIMARY KEY,
  3.     ->     name1 VARCHAR(40) NULL,
  4.     ->     name2 VARCHAR(40) NULL,
  5.     ->     made TIMESTAMP
  6.     -> );
  7. Query OK, 0 rows affected (0.42 sec)

In this case, you cannot simply load the XML file directly into the table, because the field and column names do not match:

  1. mysql> LOAD XML INFILE '../bin/person-dump.xml' INTO TABLE test.individual;
  2. ERROR 1263 (22004): Column set to default value; NULL supplied to NOT NULL column 'individual_id' at row 1

This happens because the MySQL server looks for field names matching the column names of the target table. You can work around this problem by selecting the field values into user variables, then setting the target table's columns equal to the values of those variables using SET. You can perform both of these operations in a single statement, as shown here:

  1. mysql> LOAD XML INFILE '../bin/person-dump.xml'
  2.     ->     INTO TABLE test.individual (@person_id, @fname, @lname, @created)
  3.     ->     SET individual_id=@person_id, name1=@fname, name2=@lname, made=@created;
  4. Query OK, 8 rows affected (0.05 sec)
  5. Records: 8  Deleted: 0  Skipped: 0  Warnings: 0
  6.  
  7. mysql> SELECT * FROM individual;
  8. +---------------+--------+------------+---------------------+
  9. | individual_id | name1  | name2      | made                |
  10. +---------------+--------+------------+---------------------+
  11. |             1 | Kapek  | Sainnouine | 2007-07-13 16:18:47 |
  12. |             2 | Sajon  | Rondela    | 2007-07-13 16:18:47 |
  13. |             3 | Likema | Örrtmons   | 2007-07-13 16:18:47 |
  14. |             4 | Slar   | Manlanth   | 2007-07-13 16:18:47 |
  15. |             5 | Stoma  | Nilu       | 2007-07-13 16:18:47 |
  16. |             6 | Nirtam | Sklöd      | 2007-07-13 16:18:47 |
  17. |             7 | Sungam | Dulbåd     | 2007-07-13 16:18:47 |
  18. |             8 | Srraf  | Encmelt    | 2007-07-13 16:18:47 |
  19. +---------------+--------+------------+---------------------+
  20. 8 rows in set (0.00 sec)

The names of the user variables must match those of the corresponding fields from the XML file, with the addition of the required @ prefix to indicate that they are variables. The user variables need not be listed or assigned in the same order as the corresponding fields.

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>

You can again use the test.person table as defined previously in this section, after clearing all the existing records from the table and then showing its structure as shown here:

  1. mysql< TRUNCATE person;
  2. Query OK, 0 rows affected (0.04 sec)
  3.  
  4. mysql< SHOW CREATE TABLE person\G
  5. *************************** 1. row ***************************
  6.        Table: person
  7. Create Table: CREATE TABLE `person` (
  8.   `person_id` int(11) NOT NULL,
  9.   `fname` varchar(40) DEFAULT NULL,
  10.   `lname` varchar(40) DEFAULT NULL,
  11.   PRIMARY KEY (`person_id`)
  12. ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4
  13. 1 row in set (0.00 sec)

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

  1. CREATE TABLE address (
  2.     address_id INT NOT NULL PRIMARY KEY,
  3.     person_id INT NULL,
  4.     street VARCHAR(40) NULL,
  5.     zip INT NULL,
  6.     city VARCHAR(40) NULL,
  7.     created TIMESTAMP
  8. );

To import the data from the XML file into the person table, execute the following LOAD XML statement, which specifies that rows are to be specified by the <person> element, as shown here;

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

You can verify that the records were imported using a SELECT statement:

  1. mysql> SELECT * FROM person;
  2. +-----------+--------+-------+---------------------+
  3. | person_id | fname  | lname | created             |
  4. +-----------+--------+-------+---------------------+
  5. |         1 | Robert | Jones | 2007-07-24 17:37:06 |
  6. |         2 | Mary   | Smith | 2007-07-24 17:37:06 |
  7. +-----------+--------+-------+---------------------+
  8. 2 rows in set (0.00 sec)

Since the <address> elements in the XML file have no corresponding columns in the person table, they are skipped.

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

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

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

  1. mysql> SELECT * FROM address;
  2. +------------+-----------+-----------------+-------+--------------+---------------------+
  3. | address_id | person_id | street          | zip   | city         | created             |
  4. +------------+-----------+-----------------+-------+--------------+---------------------+
  5. |          1 |         1 | Mill Creek Road | 45365 | Sidney       | 2007-07-24 17:37:37 |
  6. |          2 |         1 | Main Street     | 28681 | Taylorsville | 2007-07-24 17:37:37 |
  7. |          3 |         2 | River Road      | 80239 | Denver       | 2007-07-24 17:37:37 |
  8. +------------+-----------+-----------------+-------+--------------+---------------------+
  9. 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.

Security Considerations.  As with the LOAD DATA statement, the transfer of the XML file from the client host to the server host is initiated by the MySQL server. In theory, a patched server could be built that would tell the client program to transfer a file of the server's choosing rather than the file named by the client in the LOAD XML statement. Such a server could access any file on the client host to which the client user has read access.

In a Web environment, clients usually connect to MySQL from a Web server. A user that can run any command against the MySQL server can use LOAD XML LOCAL to read any files to which the Web server process has read access. In this environment, the client with respect to the MySQL server is actually the Web server, not the remote program being run by the user who connects to the Web server.

You can disable loading of XML files from clients by starting the server with --local-infile=0 or --local-infile=OFF. This option can also be used when starting the mysql client to disable LOAD XML for the duration of the client session.

To prevent a client from loading XML files from the server, do not grant the FILE privilege to the corresponding MySQL user account, or revoke this privilege if the client user account already has it.

Important

Revoking the FILE privilege (or not granting it in the first place) keeps the user only from executing the LOAD XML statement (as well as the LOAD_FILE() function; it does not prevent the user from executing LOAD XML LOCAL. To disallow this statement, you must start the server or the client with --local-infile=OFF.

In other words, the FILE privilege affects only whether the client can read files on the server; it has no bearing on whether the client can read files on the local file system.

For partitioned tables using storage engines that employ table locks, such as MyISAM, any locks caused by LOAD XML perform locks on all partitions of the table. This does not apply to tables using storage engines which employ row-level locking, such as InnoDB. For more information, see Partitioning and Locking.


Zoek in de MySQL-handleiding

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-load-xml.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:en Manuel MySQL : https://dev.mysql.com/

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut