在使用dom4j 对 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();
}