使用JAVA自带dom包(org.w3c.dom)时,想写入doctype <!DOCTYPE alert PUBLIC "SYSTEM" "TEST.dtd"> 百度不可靠,搜半天没找到,google下就OK了TransformerFactory transformerFactory = TransformerFactory.newInstance();Transformer transformer = transformerFactory.newTransformer();transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");transformer.setOutputProperty(OutputKeys.METHOD, "xml");DOMImplementation domImpl = document.getImplementation();DocumentType doctype = domImpl.createDocumentType("doctype","SYSTEM","TEST.dtd");transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId());transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId());DOMSource source = new DOMSource(document);StreamResult result = new StreamResult(new File(database));transformer.transform(source, result);一