【问题标题】:Java XML processing entity problem?Java XML处理实体问题?
【发布时间】:2010-11-04 09:52:32
【问题描述】:

当我尝试运行我的 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)

【问题讨论】:

  • 你能给我们看看xml吗?是“ html编码?

标签: java xml entity entityresolver


【解决方案1】:

实体 ’ 不是 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>
    &rsquo;
</demo>

DTD:

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

将 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

【讨论】:

  • 所以我应该将 行添加到我的 xml 文档中,我的问题就会消失吗?
【解决方案2】:

按照 Christian 的回答,您还可以在 XML 中声明您的实体

<!DOCTYPE your_type [
   <!ENTITY rsquo "&#8217;">
   <!ENTITY lsquo "&#8216;">
]>

【讨论】:

    【解决方案3】:
    /**
             * 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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多