Error while unmarshal an XML with JAXB caused by DTD file
Solution 1
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);
Solution 2
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);
}
}
LoremIpsum
Updated on July 24, 2022Comments
-
LoremIpsum almost 2 years
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:
- Checked if all users including system has access to R/W both files.
- Deleted and used new files to test.
- Tried To find this
accessExternalDTD
properties.
Stuff to notice:
- Project is running in Subversion
- I used the same method on previous projects and the same
.dtd
and.xml
file and it worked well - Content of the LINE 2 From the XML File:
<!DOCTYPE EXAMPLE SYSTEM "example.dtd">
-
LoremIpsum almost 9 yearsThank you allot ! i will try it ASAP so i can accept your answer if its working :)
-
HammerNL about 7 yearsThis solves the exception indeed, but note that this DISABLES DTD checking, which might not be what you want!
-
HammerNL about 7 years"-Djavax.xml.accessExternalDTD=true" does not work, "-Djavax.xml.accessExternalDTD=all" does! A bit more portable would be to do this in code: System.setProperty("javax.xml.accessExternalDTD", "all"); The unmarshaller.setProperty() solution did not work for me.
-
leventunver over 6 years@HammerNL You are my saver! This might deserve its own thread or as another answer.
-
Graham Leggett over 3 yearsThis answer does not explain what accessExternalDTD=all does, or whether doing this is safe.