与符号(&)处的XML错误 [英] XML error at ampersand (&)

查看:84
本文介绍了与符号(&)处的XML错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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​​,因此会引发错误.您应该使用&amp;对其进行转义.

& in XML starts an entity. As you haven't defined an entity &WhateverIsAfterThat an error is thrown. You should escape it with &amp;.

$string = str_replace('&', '&amp;', $string);

如何在XML中转义&"符号

要转义其他保留字符:

function xmlEscape($string) {
    return str_replace(array('&', '<', '>', '\'', '"'), array('&amp;', '&lt;', '&gt;', '&apos;', '&quot;'), $string);
}

这篇关于与符号(&)处的XML错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆