I could not find a way to do this using LOAD XML INFILE while preserving the contents of CDATA. However, the following works and uses the good old LOAD DATA INFILE along with ExtractValue() to achieve the same:
If you have an 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 "string" that it stores in the local @tmp variable. Then we pass this to the ExtractValue function as an XML fragment and select the values ββfrom them that we want using the appropriate XPath expressions.
source share