正常的 dom4j 读取 xml 的方式如下: SAXReader reader = new SAXReader(); reader.setEntityResolver(resolver); Document doc = reader.read(resource.getInputStream()); Element root = doc.getRootElement(); 1. 如何忽略 DTD 的验证 SAXReader reader = new SAXReader(); reader.setEntityResolver(new EntityResolver () { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new ByteArrayInputStream("<?xml version='1.0' encoding='utf-8'?>".getBytes())); } }); xmldoc = reader.read(new File(fileName)); xmlroot = xmldoc.getRootElement(); 2. 使用本地的DTD文件 EntityResolver resolver = new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { if (publicId.equals("-//Humpic Organization//DTD Walrus Statement Configuration 1.5//EN") || systemId.equals("http://www.humpic.com/walrus/dtds/walrus-statement-config-1.5.dtd") || systemId.endsWith("walrus-statement-config-1.5.dtd")) { InputStream in = this.getClass().getResourceAsStream("/META-INF/walrus-statement-config-1.5.dtd"); return new InputSource(in); } else if (publicId.equals("-//Humpic Organization//DTD Walrus Configuration 1.5//EN") || systemId.equals("http://www.humpic.com/walrus/dtds/walrus-config-1.5.dtd") || systemId.endsWith("walrus-config-1.5.dtd")) { InputStream in = this.getClass().getResourceAsStream("/META-INF/walrus-config-1.5.dtd"); return new InputSource(in); } return null; } }; SAXReader reader = new SAXReader(); reader.setEntityResolver(resolver); Document doc = reader.read(resource.getInputStream()); Element root = doc.getRootElement(); 3. 保存文件 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding(xmldoc.getXMLEncoding()); XMLWriter writer = new XMLWriter(new FileOutputStream(fileName), format); writer.write(xmldoc); writer.close();