LOAD XML File into a table – MySQL

This article shows how to import XML data into a MySQL table. Once the data is imported into the table, you’ll see the data in rows and columns.

Dataset: Copy the following data and save it into your local file system.

<CustInfo>
<Customer><field name=”CustomerID”>1</field><field name=”CustomerName”>Dalillah</field>
<field name=”PhoneNumber”>00971305374799</field></Customer>
<Customer><field name=”CustomerID”>2</field><field name=”CustomerName”>Melanie</field>
<field name=”PhoneNumber”>00971305374776</field></Customer>
</CustInfo>

Now go to your MySQL instance and create a table to accommodate these values.

CREATE TABLE CustTest(
CustomerID INT,
CustomerName VARCHAR(100),
PhoneNumber VARCHAR(100)
);

Now load the XML data into the table using the following statement.

LOAD XML LOCAL INFILE
‘C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/CustInfo.xml’
INTO TABLE CustTest
ROWS IDENTIFIED BY ‘<Customer>’;

Now, retrieve the data from the table.

SELECT * FROM CustTest;

LOAD_XML_MySQL

If you are facing any of the following error messages –

then refer to the earlier post that lets you know how to fix it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s