Microsoft Developer
Microsoft Developer

Reputation: 5459

Multiple rows insert in a table in MySQL database using XML file

I am completely new in MySQL and trying to insert multiple rows in MySQL table using XMl file but can't do it. My query gets executed successfully but when inserts null in columns in table. Below is the code for query and XML file.

set @xml := load_file('c:/xmldistributortransaction.xml');
insert into xmldistributortransaction     (DistributorId,ProductId,Remarks,Quantity,Price,DIscount,TaxName,Total,AddedDate) values (
        extractValue(@xml,'/resultset/row[1]/field[1]/text()'),
        extractValue(@xml,'/resultset/row[1]/field[2]/text()'),
  extractValue(@xml,'/resultset/row[1]/field[3]/text()'),
  extractValue(@xml,'/resultset/row[1]/field[4]/text()'),
        extractValue(@xml,'/resultset/row[1]/field[5]/text()'),
        extractValue(@xml,'/resultset/row[1]/field[6]/text()'),
        extractValue(@xml,'/resultset/row[1]/field[7]/text()'),
        extractValue(@xml,'/resultset/row[1]/field[8]/text()'),
        extractValue(@xml,'/resultset/row[1]/field[9]/text()'));

XML file

<?xml version="1.0"?>
<resultset statement="select      DistributorId,ProductId,Remarks,Quantity,Price,DIscount,TaxName,Total,AddedDate from xmldistributortransaction">
<row>
<field name="DistributorId">2</field>
<field name="ProductId">20 </field>
<field name="Remarks">remarks for second</field>
<field name="Quantity" >300</field >
<field name="Price">30</field>
<field name="Discount">2 </field>
<field name="TaxName">VAT4</field>
<field name="Total">240</field>
<field name="AddedDate">2012-04-02</field>
</row>
</resultset>

Upvotes: 1

Views: 1775

Answers (1)

Devart
Devart

Reputation: 121922

You can try LOAD XML (MySQL) command.

LOAD XML statement syntax, added in MySQL 5.5.

For example -

LOAD XML LOCAL INFILE 'xmldistributortransaction.xml'
  INTO TABLE xmldistributortransaction
  ROWS IDENTIFIED BY '<row>';

Upvotes: 2

Related Questions