Java XML处理实体问题?

发布于 2024-10-01 06:48:05 字数 945 浏览 7 评论 0原文

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

据我了解,有一个未引用的实体不属于 xml 标准的一部分,所以我的问题是;我该如何解决这个问题?

谢谢,

[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)

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).

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?

Thanks,

[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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不在是我 2024-10-08 06:48:05

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

如果您创建了 XML,您可以将实体添加到 DTD 中。要解决此问题,请将 DTD 添加到 XML 文件(如果尚未定义)。

XML:

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

DTD:

<!ELEMENT demo (#PCDATA)>
<!ENTITY rsquo   "’">

向应用程序提供 DTD,错误就会消失。我不会自己编写所有实体,我会使用 W3C 中的一个,例如: http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent

如何在 XML 中包含 DTD 是另一个问题。据我记得您可以设置 DTD 或目录文件的路径。

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

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

If you created the XML you can add entitiess to your DTD. To fix the issue, add a DTD to the XML file (if not already defined).

XML:

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

DTD:

<!ELEMENT demo (#PCDATA)>
<!ENTITY rsquo   "’">

Provide the DTD to the application and the error goes away. I wouldn't write all entities myself, I would use one from W3C, such as: http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent

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.

edit 2:
Take a look at the EntityResolver:
http://download.oracle.com/javase/1.4.2/docs/api/org/xml/sax/EntityResolver.html

酷到爆炸 2024-10-08 06:48:05

按照 Christian 的回答,您还可以将您的实体直接声明到 XML 中

<!DOCTYPE your_type [
   <!ENTITY rsquo "’">
   <!ENTITY lsquo "‘">
]>

Following the answer of Christian, you also have the possibility to declare your entities right into the XML

<!DOCTYPE your_type [
   <!ENTITY rsquo "’">
   <!ENTITY lsquo "‘">
]>
天煞孤星 2024-10-08 06:48:05
/**
         * This Inner class is written to solve the XML parsing DTD validation
         * checking from online because if Internet is not connected, then it
         * throws Exception.
         * 
         * @author Ravi Thapa
         */




public class CustomEntityResolver implements EntityResolver
    {
        public InputSource resolveEntity(String publicId, String systemId)
        {
            InputSource source = null;
            Pattern pattern1 =
                    Pattern.compile("^-//(.*)//DTD(.*)$", Pattern.CASE_INSENSITIVE);
            Matcher match1 = pattern1.matcher(publicId.trim());

            Pattern pattern2 =
                    Pattern.compile("^http://(.*).dtd$", Pattern.CASE_INSENSITIVE);
            Matcher match2 = pattern2.matcher(systemId.trim());
            if (match1.find() || match2.find())
            {
                source = new InputSource(new ByteArrayInputStream("".getBytes()));
            }

            // return null to signal default behavior
            return source;
        }
    }
/**
         * This Inner class is written to solve the XML parsing DTD validation
         * checking from online because if Internet is not connected, then it
         * throws Exception.
         * 
         * @author Ravi Thapa
         */




public class CustomEntityResolver implements EntityResolver
    {
        public InputSource resolveEntity(String publicId, String systemId)
        {
            InputSource source = null;
            Pattern pattern1 =
                    Pattern.compile("^-//(.*)//DTD(.*)$", Pattern.CASE_INSENSITIVE);
            Matcher match1 = pattern1.matcher(publicId.trim());

            Pattern pattern2 =
                    Pattern.compile("^http://(.*).dtd$", Pattern.CASE_INSENSITIVE);
            Matcher match2 = pattern2.matcher(systemId.trim());
            if (match1.find() || match2.find())
            {
                source = new InputSource(new ByteArrayInputStream("".getBytes()));
            }

            // return null to signal default behavior
            return source;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文