与符号(&)处的XML错误 [英] XML error at ampersand (&)
问题描述
我有一个php文件,该文件基于MySql数据库打印xml.
I have a php file which prints an xml based on a MySql db.
每次在出现& 符号的位置都会出现错误.
I get an error every time at exactly the point where there is an & sign.
这是一些php:
$query = mysql_query($sql);
$_xmlrows = '';
while ($row = mysql_fetch_array($query)) {
$_xmlrows .= xmlrowtemplate($row);
}
function xmlrowtemplate($dbrow){
return "<AD>
<CATEGORY>".$dbrow['category']."</CATEGORY>
</AD>
}
输出是我想要的,即文件输出正确的类别,但仍然给出错误.
The output is what I want, i.e. the file outputs the correct category, but still gives an error.
错误提示: xmlParseEntityRef:无名称
然后指向确切的字符,该字符是& 符号.
And then it points to the exact character which is a & sign.
仅当$dbrow['category']
是带有& 签名的东西时才抱怨,例如:"汽车和卡车"或"电脑和电话".
This complains only if the $dbrow['category']
is something with an & sign in it, for example: "cars & trucks", or "computers & telephones".
有人知道是什么问题吗?
Anybody know what the problem is?
顺便说一句:我在所有文档中都将编码设置为UTF-8,并且将xml输出设置为
BTW: I have the encoding set to UTF-8 in all documents, as well as the xml output.
推荐答案
&
启动一个实体.由于您尚未定义实体&WhateverIsAfterThat
,因此会引发错误.您应该使用&
对其进行转义.
&
in XML starts an entity. As you haven't defined an entity &WhateverIsAfterThat
an error is thrown. You should escape it with &
.
$string = str_replace('&', '&', $string);
要转义其他保留字符:
function xmlEscape($string) {
return str_replace(array('&', '<', '>', '\'', '"'), array('&', '<', '>', ''', '"'), $string);
}
这篇关于与符号(&)处的XML错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!