pom.xml
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>
I want to read the following xml file with an external DTD file specified by ʻorg.dom4j.io.SAXReader`. However, I want to load it in an offline environment.
struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
...
</struts-config>
When I ran the following code in an offline environment, I got a ʻUnknownHostException`.
Bad.java
SAXReader reader = new SAXReader();
Document doc = reader.read("struts-config.xml");
Exception
org.dom4j.DocumentException: jakarta.apache.org Nested exception: jakarta.apache.org
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.dom4j.io.SAXReader.read(SAXReader.java:321)
at test2.Test2.main(Test2.java:24)
Nested exception:
java.net.UnknownHostException: jakarta.apache.org
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
~~~
The cause is that the dtd file specified in the DOCTYPE declaration cannot be read. The solution was to not read the external DTD file.
Good.java
SAXReader reader = new SAXReader();
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
Document doc = reader.read("struts-config.xml");
https://xerces.apache.org/xerces2-j/features.html http://qiita.com/yoshi389111/items/3d0da72b1f2ccd947052 http://k-motohashi.hatenablog.com/entry/2013/04/24/115413
Recommended Posts