2

I found one question in regarding to that but it appears that it is an old question and answers no longer work Java XML processing entity problem?

So my problem is I am actually using an XML file and it does have Entity References. I would like these entity references to stay and DocumentBuilder not to parse it. However it tries to parse it and then I get The entity "entityname" was referenced, but not declared. exception.

This is my XmlParser class constructor:

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();
}

I also tried just using DocumentBuilderFactory.newInstance() without any parameters and got the same error.

How can I fix this issue?

Example XML:

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

Exception for that:

[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)
Community
  • 1
  • 1
Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103
  • You should provide us also with a sample of the XML document you are parsing. This kind of error often occurs in the data, not in the code your are invoking. – potame Apr 21 '15 at 07:14
  • @potame sure, i thought it's a general issue but I added an example. – Sarp Kaya Apr 21 '15 at 07:34
  • So actually you want the parser to completely skip entity expansion, and find *&entityname;* in the result document as if it was some text? – potame Apr 21 '15 at 08:07
  • 1
    @potame yes precisely – Sarp Kaya Apr 21 '15 at 09:48
  • Then I'm afraid it is not possible. An entity declaration **must** be found, even if the expansion can be avoided thanks to the `factory.setValidating(false)` feature. – potame Apr 21 '15 at 13:23
  • @potame is there any other parser that can be used instead of DOM that would allow me to do this? – Sarp Kaya Apr 21 '15 at 23:33

0 Answers0