问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/13107639/problems-with-mysql-load-xml-infile