XMLUtil工具类有两个功能一个是读取文档和写入文档的方法
public class XMLUtil {
//读取文档的方法
public static Document getDocument(){
try {
Document document = new SAXReader().read(new File("/Users/zk/Desktop/Contact.xml"));
return document;
} catch (DocumentException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
//写入文档的方法
public static void XMLWriter(Document document){
try {
OutputStream outputStream = new FileOutputStream("/Users/zk/Desktop/Contact.xml");
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(outputStream,outputFormat);
xmlWriter.write(document);
xmlWriter.close();
}catch (Exception e){
e.printStackTrace();
}
}
}