我是MYSQL的新手,我有一个酒店的XML文件,其中包括HotelCode和酒店描述。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>
发布于 2019-06-29 12:35:40
第二个引号是正确的单引号(’ ’ U+2019)。
如果使用XSLT,可以使用translate()函数替换它,例如:
<xsl:value-of select='translate(description, "’", "'")'/>发布于 2019-06-29 07:38:45
尝尝这个
$content = file($rs_file_path);
$filedata = str_replace("'", "\'", $content);
$filedata = str_replace('"', '\"', $content);
$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')
;");它将打开您的文件,并在单引号和双引号上添加斜杠。
当您检索表数据时,请像这样使用以防止斜杠被打印。
echo stripslashes($str);https://stackoverflow.com/questions/56813136
复制相似问题