Java XML处理实体问题? [英] Java XML processing entity problem?

查看:178
本文介绍了Java XML处理实体问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行我的java程序时,我得到以下错误(它应该读取一个xml文件并打印出一些内容)。

I get the following error when I try to run my java program(it's supposed to read an xml file and print out some of the content).

从什么我知道有一个未引用的实体,它不是xml标准的一部分,所以我的问题是;我该如何解决这个问题?

From what I understand there is an unreferenced entity which is not part of the xml standard so my question is; how can I fix this problem?

谢谢,

[Fatal Error] subject.xml:4:233: The entity "rsquo" was referenced, but not declared.
org.xml.sax.SAXParseException: The entity "rsquo" was referenced, but not declared.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at DomParserExample2.parseXmlFile(DomParserExample2.java:42)
at DomParserExample2.runExample(DomParserExample2.java:24)
at DomParserExample2.main(DomParserExample2.java:115)
Exception in thread "main" java.lang.NullPointerException
at DomParserExample2.parseDocument(DomParserExample2.java:54)
at DomParserExample2.runExample(DomParserExample2.java:27)
at DomParserExample2.main(DomParserExample2.java:115)


推荐答案

实体& rsquo; 不是XML实体。它在HTML中定义: http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

The entity ’ is not an XML-Entity. Its defined in HTML: http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

如果您创建了XML,则可以向DTD添加实体。

If you created the XML you can add Entitys to you DTD.

像这样的东西可以提供帮助: http://gv.ca/dtd/character-entities.dtd

Something like this one could help: http://gv.ca/dtd/character-entities.dtd

编辑:
要解决此问题,您可以将DTD添加到XML文件中(如果尚未定义)。

edit: To fix this issue you can add an DTD to your XML File (if not already defined).

您的XML:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE demo SYSTEM "./demo.dtd">
<demo>
    &rsquo;
</demo>

您的DTD:

<!ELEMENT demo (#PCDATA)>
<!ENTITY rsquo   "&#8217;">

如果您向应用程序提供DTD,则错误消失。我不会自己写一个Entites,我会使用W3C中的一个 http: //www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent

If you provide the DTD to your Application, the error goes away. I wouldn't write alle Entites myself, I would use one from W3C http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent

如何为您的XML包含DTD是另一个问题。据我所知,您可以设置DTD或目录文件的路径。

How to include the DTD for your XML is another Question. As far as I remember you can set the path to the DTD, or an Catalog-File.

编辑2:
看一下EntityResolver:
http://download.oracle.com/javase/1.4.2/docs/api/org/xml/sax/ EntityResolver.html

这篇关于Java XML处理实体问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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