[Problem solving] SAXReader: How to read an xml file with an external DTD file specified in an offline environment

environment

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>

Thing you want to do

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>

problem

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

Cause

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");

Reference site

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

[Problem solving] SAXReader: How to read an xml file with an external DTD file specified in an offline environment
How to solve an Expression Problem in Java
[Rails] How to build an environment with Docker
[Rails] How to read the XML file uploaded from the screen with Hash type
How to store an object in PostgreSQL as JSON with MyBatis (Mapper XML)
How to use git with the power of jgit in an environment without git commands
How to save a file with the specified extension under the directory specified in Java to the list
How to write an external reference key in FactoryBot
How to make a jar file with no dependencies in Maven
How to get the length of an audio file in java
How to insert an external library
`SAXReader`: Get the xml file with the default namespace set in XPath format
How to execute with commands of normal development language in Docker development environment
How to read log4j configuration file in Java project summarized in jar file Memo
How to start a Docker container with a volume mounted in a batch file
How to crop an image with libGDX
How to reflect seeds.rb in production environment
How to add jar file in ScalaIDE
How to achieve file download with Feign
Read xlsx file in Java with Selenium
How to use environment variables in RubyOnRails
How to achieve file upload with Feign
How to build Rails 6 environment with Docker
How to output jar with main class specified by gradle in Intellij IDEA
How to send custom metrics and events to datadog with laravel in docker-compose environment
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
How to create an Excel form using a template file with Spring MVC
How to set environment variables in the properties file of Spring boot application