dom 4 j dtd検証を無視
dom 4 jを使用してxmlファイルを解析する場合、dtd検証の問題によく遭遇します.
次のように宣言します.
読み込み中に次のエラーメッセージが表示されます.
解決方法:
その他のコードは次のとおりです.
次のように宣言します.
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
...
読み込み中に次のエラーメッセージが表示されます.
org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.dom4j.io.SAXReader.read(SAXReader.java:264)
解決方法:
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
その他のコードは次のとおりです.
try {
File file = new File("F:/zhucaiguai/hibernate.cfg.xml");
SAXReader reader = new SAXReader();
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
Document doc = reader.read(file);
Element root = doc.getRootElement();
System.out.println(root.asXML());
// OutputFormat format = new OutputFormat();
// XMLWriter output = new XMLWriter(new FileWriter(file), format);// XML
// output.write(doc);
// output.close();
} catch (Exception e) {
e.printStackTrace();
}