包含“&”的字符串出错在 XML 中。我该如何解决这个问题?
我使用以下代码将数据从 XML 导入到 mySQL。 有没有什么办法可以编码&在使用 PHP 加载它之前而不是从文件中手动更改它?
这是错误
Warning: simplexml_load_file() [function.simplexml-load-file]: products.xml:4: parser error : xmlParseEntityRef: no name in ... on line 5
Warning: simplexml_load_file() [function.simplexml-load-file]: <description> DAYLIGHT & LED / SMD Tech</description> in ... on line 5
Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in ... on line 5
Warning: Invalid argument supplied for foreach() in ... on line 8
这是我的 xml 的示例元素
<?xml version="1.0" encoding="UTF-8"?>
<description>Νέας γενιάς & τεχνολογίας DAYLIGHT LED / SMD Tech</description>
I am using the following code to import my data from XML to mySQL.
Is there any way to encode & before loading it using PHP instead of manually change it from the file?
this is the error
Warning: simplexml_load_file() [function.simplexml-load-file]: products.xml:4: parser error : xmlParseEntityRef: no name in ... on line 5
Warning: simplexml_load_file() [function.simplexml-load-file]: <description> DAYLIGHT & LED / SMD Tech</description> in ... on line 5
Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in ... on line 5
Warning: Invalid argument supplied for foreach() in ... on line 8
this is a sample element of my xml
<?xml version="1.0" encoding="UTF-8"?>
<description>Νέας γενιάς & τεχνολογίας DAYLIGHT LED / SMD Tech</description>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试如下函数
utf8_encode
来使用您的 mySql 表:
) ENGINE=MyISAM DEFAULT COLLATION utf8_general_ci CHARSET=utf8;
希望它对您有用。
更新:我错过了 $data 是一个数组......
正确的代码是:
try the function
utf8_encode
as followsfor your mySql table use:
) ENGINE=MyISAM DEFAULT COLLATION utf8_general_ci CHARSET=utf8;
hope it work for you.
UPDATE: I've missed that $data was an array...
the right code is:
您没有从 MySQL 服务器收到错误。您的错误来自
simplexml_load_file()
,因为您正在读取的源 XML 文件包含错误。特别是,文字&
必须编码为&
。You are not getting an error from the MySQL Server. Your error comes from
simplexml_load_file()
because the source XML file you are reading contains errors. In particular, a literal&
must be encoded as&
.