【发布时间】:2019-11-10 18:43:21
【问题描述】:
我是 MYSQL 新手,我有一个酒店的 XML 文件,其中包括酒店代码和酒店描述。如下所示的xml文件
<hotels>
<hotel>
<hotelcode>1</hotelcode>
<description>San cassiano residenzia D’epocaVenice’s Grand Canal.Overview Situated overlooking Venice’s Grand Canal, San Cassiano Residenzia D’Epoca is a historic hotel with plenty of charm</description>
</hotel>
<hotel>
<hotelcode>2</hotelcode>
<description>San cassiano residenzia D’epocaVenice’s Grand Canal.Overview Situated overlooking Venice’s Grand Canal, San Cassiano Residenzia D’Epoca is a historic hotel with plenty of charm</description>
</hotel>
<hotel>
<hotelcode>3</hotelcode>
<description>San cassiano residenzia D’epocaVenice’s Grand Canal.Overview Situated overlooking Venice’s Grand Canal, San Cassiano Residenzia D’Epoca is a historic hotel with plenty of charm</description>
</hotel>
<hotels>
我也使用下面的 sql 查询将 xml 数据插入数据库
$conn_1->query("LOAD DATA LOCAL INFILE '".$rs_file_path."'
INTO TABLE hotels
CHARACTER SET utf8
LINES STARTING BY '<hotel>' TERMINATED BY '</hotel>'
(@tmp)
SET
hotelcode = ExtractValue(@tmp, 'hotelcode'),
description= ExtractValue(@tmp, 'description')
;");
但是这里的数据没有插入到酒店表中。因为描述中包含一些特殊字符,如',""等。
有什么办法像mysqli_real_escape_string
更新: “但现在我发现引号在 xml 中有两种类型,如下图所示”
如何替换第二种引号?
请检查附件。
<hotels>
<hotel>
<hotelcode>1</hotelcode>
<description>Located near S'Arenal Venice’s yacht club</description>
</hotel>
<hotel>
<hotelcode>2</hotelcode>
<description>Located near S'Arenal Venice’s yacht club</description>
</hotel>
<hotel>
<hotelcode>3</hotelcode>
<description>Located near S'Arenal Venice’s yacht club</description>
</hotel>
</hotels>
【问题讨论】:
-
您是否考虑过使用 LOAD XML 而不是 LOAD DATA?老实说,我没有任何经验,但它似乎对 XML 更有效。
-
我更新问题
-
您仍然没有使用
LOAD XML,这使得使用您的平面 XML 导入非常容易。