php & 符号 (&) 处的 XML 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4026502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML error at ampersand (&)
提问by NikiC
I have a php file which prints an xml based on a MySql db.
我有一个基于 MySql db 打印 xml 的 php 文件。
I get an error every time at exactly the point where there is an &sign.
我每次都在有&符号的地方出现错误。
Here is some php:
这是一些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.
输出是我想要的,即文件输出正确的类别,但仍然给出错误。
The error says: xmlParseEntityRef: no name
错误说:xmlParseEntityRef: no name
And then it points to the exact character which is a &sign.
然后它指向确切的字符,即&符号。
This complains only if the $dbrow['category']
is something with an &sign in it, for example: "cars & trucks", or "computers & telephones".
只有在$dbrow['category']
带有&符号的东西时才会抱怨,例如:“汽车和卡车”,或“电脑和电话”。
Anybody know what the problem is?
有谁知道问题是什么?
BTW: I have the encoding set to UTF-8 in all documents, as well as the xml output.
顺便说一句:我在所有文档以及 xml 输出中都将编码设置为 UTF-8。
回答by NikiC
&
in XML starts an entity. As you haven't defined an entity &WhateverIsAfterThat
an error is thrown. You should escape it with &
.
&
在 XML 中启动一个实体。由于您尚未定义实体&WhateverIsAfterThat
,因此会引发错误。你应该用&
.
$string = str_replace('&', '&', $string);
How do I escape ampersands in XML
To escape the other reserved characters:
要转义其他保留字符:
function xmlEscape($string) {
return str_replace(array('&', '<', '>', '\'', '"'), array('&', '<', '>', ''', '"'), $string);
}
回答by Pekka
You need to either turn &
into its entity &
, or wrap the contents in CDATAtags.
您需要将其转换&
为 entity &
,或者将内容包装在CDATA标签中。
If you choose the entity route, there are additional characters you need to turn into entities:
如果选择实体路线,则需要额外的字符才能变成实体:
> >
< <
' '
" "
Background: Beware of the ampersand when using XML
Wikipedia: List of XML character entity references
维基百科:XML 字符实体引用列表
回答by Denise Ignatova
public function sanitize(string $data) {
return str_replace('&', '&', $data);
}
You are right: here is more context - the example is in relation to the ' how to deal with data containing '&' when we pass this data to SimpleXml. Of course there is also other solution to use
<![CDATA[some stuff]]>
你是对的:这里有更多的上下文 - 该示例与'当我们将这些数据传递给 SimpleXml 时如何处理包含 '&' 的数据有关。当然还有其他解决方案可以使用
<![CDATA[some stuff]]>
回答by Huseyin Durmus
Switch and regex with using xml escape function.
使用 xml 转义函数切换和正则表达式。
function XmlEscape(str) {
if (!str || str.constructor !== String) {
return "";
}
return str.replace(/[\"&><]/g, function (match) {
switch (match) {
case "\"":
return """;
case "&":
return "&";
case "<":
return "<";
case ">":
return ">";
}
});
};