引用了实体“entityname”,但未声明

时间:2015-04-21 07:09:07

标签: java xml xerces xerces2-j

我发现了一个问题,但似乎这是一个老问题,答案不再有效Java XML processing entity problem?

所以我的问题是我实际上使用的是XML文件,它确实有Entity References。我希望这些实体引用保留,DocumentBuilder不要解析它。但是它尝试解析它然后我得到The entity "entityname" was referenced, but not declared.异常。

这是我的XmlParser类构造函数:

public XmlParser(File file) throws ParserConfigurationException,
        SAXException, IOException {
    String provider = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(provider, null);
    factory.setExpandEntityReferences(false);
    factory.setValidating(false);
    factory.setFeature(
            "http://apache.org/xml/features/nonvalidating/load-external-dtd",
            false);
    DocumentBuilder dBuilder = factory.newDocumentBuilder();
    document = dBuilder.parse(file);
    document.getDocumentElement().normalize();
}

我还尝试使用DocumentBuilderFactory.newInstance()而没有任何参数,并得到了相同的错误。

如何解决此问题?

示例XML:

<?xml version="1.0" encoding="UTF-8"?>
<chapter >
    <title>&entityname; Some Title</title>
</chapter>

例外:

[Fatal Error] NHS.xml.keyref:3:21: The entity "entityname" was referenced, but not declared.
Exception in thread "main" org.xml.sax.SAXParseException; systemId: file:/C:/somedir/NHS.xml.keyref; lineNumber: 3; columnNumber: 21; The entity "entityname" 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 au.gov.nehta.mdht.datahierarchy2dita.XmlParser.<init>(XmlParser.java:43)
    at au.gov.nehta.mdht.datahierarchy2dita.Parse.parseDocbookToDita(Parse.java:53)
    at au.gov.nehta.mdht.datahierarchy2dita.Parse.main(Parse.java:83)

0 个答案:

没有答案