简体   繁体   中英

Problems with MySQL LOAD XML INFILE

I have a XML document in the format of...

<?xml version="1.0" encoding="UTF-8"?>
<yahootable>
    <row>
        <various><![CDATA[ multiline 
        text, "&" 
        other <stuff> ]]>
        </various>
        <id>1</id>
        <message><![CDATA[
                sdfgsdfg
                dsfsdfsd ]]>
        </message>
    </row>
<yahootable>

...and want to use MySQL's LOAD XML LOCAL INFILE to insert it into a table with columns; (various, id, message). I can't seem to get any data from the unparsed CDATA tags into the database columns. Is it that the data between CDATA tags is completely ignored, or is there something I've missed? I was expecting the CDATA would just escape the illegal XML characters and insert it as regular text.

Thanks.

I couldn't find a way to do this using LOAD XML INFILE while preserving the CDATA contents. However, the following works and uses good old LOAD DATA INFILE along with ExtractValue() to accomplish the same thing:

If we have your example file and this table:

CREATE TABLE `yahootable` (
  `id` int(11) NOT NULL PRIMARY KEY,
  `various` text,
  `message` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8
;

then running this statement will import the contents of the file into the table:

LOAD DATA INFILE 
    '/tmp/yahootable.xml'
INTO TABLE 
    yahootable
CHARACTER SET 'utf8'
LINES STARTING BY '<row>' TERMINATED BY '</row>'
(@tmp)
SET
  id      = ExtractValue(@tmp, '//id'),
  various = ExtractValue(@tmp, '//various'),
  message = ExtractValue(@tmp, '//message')
;

This works by telling LOAD DATA INFILE that each <row>...</row> is a logical 'line', which it stores in the local variable @tmp . We then pass this to the ExtractValue function as an XML fragment and select the values from it that we want using the appropriate XPath expressions.

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