java中xml dom中的特殊字符解析错误
我在java中使用org.w3c.dom来读取带有以下函数的xml,
try
{
cwNodes = xmlDoc.getElementsByTagName("cw-page");
}
catch(Exception doe)
{
BusinessLogic.WriteLog("Exp msg: "+doe.getMessage());
}
该xml
<cw-page id='4545' num='1'>dlfdlkfsdf</cw-page>
读得很好,但是当我传递以下xml时,
<cw-page id='4545' num='1'>dlfdlkfsdf&!@#%^*()</cw-page>
它没有读取它,异常essage也为空,不知道出了什么问题可以有人帮忙,谢谢
i am using org.w3c.dom in java to read an xml with the following function
try
{
cwNodes = xmlDoc.getElementsByTagName("cw-page");
}
catch(Exception doe)
{
BusinessLogic.WriteLog("Exp msg: "+doe.getMessage());
}
the xml is
<cw-page id='4545' num='1'>dlfdlkfsdf</cw-page>
it reads fine but when i pass the following xml
<cw-page id='4545' num='1'>dlfdlkfsdf&!@#%^*()</cw-page>
it doesnt read it, the exception essage is null as well, dont know what is wrong can anybody help, thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
&
不能单独出现在 XML 文档中,因为它用于启动 XML 实体引用。因此,如果解析器必须正确解析其内容并返回&
,则您的 XML 文档必须包含&
而不是&
文本内容。&
并不是 XML 解析器在单独存在时无法正确解析的唯一字符;还有其他字符必须作为 预定义 XML 实体 出现解析正确发生。您应该使用实体引用来更正文档,或者使用 CDATA 部分来传输有效负载中的特殊字符。
&
cannot occur by itself in a XML document as it is used to start a XML entity reference. Therefore, your XML document must contain&
and not&
if a parser has to parse its contents correctly and return&
in the text content.&
is not the only character that XML parsers will fail to parse incorrectly if they are present on their own; there are other characters that must be present as the predefined XML entities for parsing to occur correctly.You should either correct the document, by using entity references or use CDATA sections to transport the special characters in the payload.