The entity "entityname" was referenced, but not declared

2.5k views Asked by At

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

There are 0 answers