使用“&”、“®”解析 xml,但仍然出现错误
无论我看什么,帖子都告诉我用 html 实体转义 xml 特殊字符,但我仍然遇到 XML 解析错误。我收到的错误消息是“身份不明的实体”,它发生在 &和®标记(不带空格)。我该如何解决这个问题以及为什么这仍然会引发错误?
<?xml version="1.0" encoding="UTF-8"?>
<maps>
<location id="tx">
<item label="Lobby & Entrance" xpos="125" ypos="112" />
<item label="Restaurant & Bar" xpos="186" ypos="59" />
<item label="Swimming Pool" xpos="183" ypos="189" />
<item label="Nautilus Gym®" xpos="154" ypos="120" />
</location>
</maps>
Everywhere I look, posts are telling me to escape xml special characters with their html entity, but I'm still getting XML parsing errors. The error message I'm receiving is "unidentified entity", and it occurs at the & ; and ® ; marks (without the spaces). How can I fix this and why would this still be throwing errors?
<?xml version="1.0" encoding="UTF-8"?>
<maps>
<location id="tx">
<item label="Lobby & Entrance" xpos="125" ypos="112" />
<item label="Restaurant & Bar" xpos="186" ypos="59" />
<item label="Swimming Pool" xpos="183" ypos="189" />
<item label="Nautilus Gym®" xpos="154" ypos="120" />
</location>
</maps>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
代替:
®
作者:®
和
&
by:&
并且您的 XML 将有效
Replace:
®
by:®
and
&
by:&
and your XML will be valid
XML 仅具有少量“内置”字符实体名称。 “amp”是内置函数之一,因此您似乎不太可能在那里遇到错误。然而,“reg”不是内置的。
要解决此问题,您可以在 reg 的位置使用数字引用、使用实际字符,或者包含 reg 的实体声明,如下所示:
您可以查看 XHTML DTD 获取 HTML 实体的完整实体声明集。
XML only has a small number of "built-in" character entity names. "amp" is one of the built-ins, so it seems unlikely that you're getting an error there. "reg" is not built-in, however.
To fix this you can either use a numeric reference on place of reg, use the actual character, or include an entity declaration for reg, like this:
You can look in the XHTML DTDs to get the complete set of entity declarations for HTML entities.
XML 仅定义实体
&
、<
和>
。除非您以某种方式声明,否则®
无效。XML only defines the entities
&
,<
und>
.®
is invalid unless you declare in some way.不要这样做。使用 XML 实体。
您不应该遇到
&
问题,因为它是 XML 的一部分。您必须使用损坏的解析器。但很难说,因为您没有提供任何用于解析此内容的代码。另一方面,
®
不应由 XML 解析器进行解析,除非包含定义它的 DTD。使用数字实体或(更好)真实字符和合适的 (UTF-8) 字符编码。Don't. Use XML entities.
You shouldn't get a problem with
&
as that is part of XML. You must be using a broken parser. It is hard to tell though as you have not provided any of the code you are using to parse this.®
on the other hand should not be parsed by an XML parser unless you include a DTD that defines it. Use numeric entities or (better yet) the real character and a suitable (UTF-8) character encoding.