LoremIpsum
LoremIpsum

Reputation: 540

Error while unmarshal an XML with JAXB caused by DTD file

I try to unmarhshal a file XML file (test.xml) with JAXB (javax.xml.bind.JAXB) but it gives me this error:

[org.xml.sax.SAXParseException; systemId: file:/C:/Users/EXAMPLE/AppData/Local/Eclipse/workspace_4.4.0/EXAMPLE/test.xml; lineNumber: 2; columnNumber: 42; Externe DTD: Lesen von externer DTD "example.dtd" nicht erfolgreich, da "file"-Zugriff wegen der von der Eigenschaft "accessExternalDTD" festgelegten Einschränkung nicht zulässig ist.]

English Translation:

Reading from external DTD "example.dtd" not succesful , cause "File"-Access is not allowed by the Restriction set by the Properties "accessExternalDTD"

Solutions already tried:

Stuff to notice:

Upvotes: 10

Views: 18338

Answers (3)

Peter Kelley
Peter Kelley

Reputation: 4196

Programmatically:

System.setProperty(
    "javax.xml.accessExternalDTD", "file" );

or at startup include
-Djavax.xml.accessExternalDTD=all

Either way resolved it for me.
The value can be "file" or "all" or a comma-separated list.
I tried the accepted answer but in my case setting the property on the unmarshaller caused a javax.xml.bind.PropertyException
More details of my environment:
I'm using the

  • Microsoft OpenJDK 11 on Windows added the jars for JaxB 2.x
  • Migrated from OpenJDK 8 from RH

Upvotes: 0

Petter
Petter

Reputation: 4165

The accessExternalDTD property can be controlled with the system property javax.xml.accessExternalDTD, so start your program with -Djavax.xml.accessExternalDTD=true and it should work. It should also be possible to set the property on the unmarshaller, try this:

unmarshaller.setProperty(javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD, Boolean.TRUE);

Upvotes: 12

Spartan
Spartan

Reputation: 3401

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        XMLInputFactory xif = XMLInputFactory.newFactory();
        xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("input.xml"));

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Customer customer = (Customer) unmarshaller.unmarshal(xsr);
    }

}

Upvotes: 6

Related Questions