XML解析错误:未定义的实体-特殊字符 [英] XML Parsing Error: undefined entity - special characters

查看:534
本文介绍了XML解析错误:未定义的实体-特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么XML在某些特殊字符上显示错误,而某些可以吗?

例如,下面将创建错误,

<?xml version="1.0" standalone="yes"?>
<Customers>
    <Customer>
        <Name>L&ouml;ic</Name>
    </Customer>
</Customers>

但这没关系,

<?xml version="1.0" standalone="yes"?>
<Customers>
    <Customer>
        <Name>&amp;</Name>
    </Customer>
</Customers>

我通过php-htmlentities('Löic',ENT_QUOTES)转换特殊字符.

我该如何解决?

谢谢.

我发现如果我使用数字字符(例如L&#243;ic

),效果很好

现在我必须找到如何使用php将特殊字符转换为数字字符!

解决方案

XML规范中定义了五个实体-&amp;&lt;&gt;&apos;&quot;

在HTML DTD中定义了很多实体. /p>

您不能在通用XML中使用HTML中的内容.

您可以使用数字引用,但最好仅获取字符编码直接(基本上可以归结为:

  • 设置编辑器为将数据保存到UTF-8
  • 如果您使用编程语言处理数据,请确保其支持UTF-8格式
  • 如果您将数据存储在数据库中,请确保已将其配置为UTF-8
  • 处理文档时,请确保HTTP标头指定其为UTF-8(对于XML,默认为UTF-8,因此不指定任何内容几乎一样)

)

Why does XML display error on certain special characters and some are ok?

For instance, below will create error,

<?xml version="1.0" standalone="yes"?>
<Customers>
    <Customer>
        <Name>L&ouml;ic</Name>
    </Customer>
</Customers>

but this is ok,

<?xml version="1.0" standalone="yes"?>
<Customers>
    <Customer>
        <Name>&amp;</Name>
    </Customer>
</Customers>

I convert the special character through php - htmlentities('Löic',ENT_QUOTES) by the way.

How can I get around this?

Thanks.

EDIT:

I found that it works fine if I use numeric character such as L&#243;ic

now I have to find how to use php to convert special characters into numeric characters!

解决方案

There are five entities defined in the XML specification — &amp;, &lt;, &gt;, &apos; and &quot;

There are lots of entities defined in the HTML DTD.

You can't use the ones from HTML in generic XML.

You could use numeric references, but you would probably be better off just getting your character encodings straight (which basically boils down to:

  • Set your editor to save the data in UTF-8
  • If you process the data with a programming language, make sure it is UTF-8 aware
  • If you store the data in a database, make sure it is configured for UTF-8
  • When you serve up your document, make sure the HTTP headers specify that it is UTF-8 (in the case of XML, UTF-8 is the default, so not specifying anything is almost as good)

)

这篇关于XML解析错误:未定义的实体-特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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