dom4j之小小工具,dom4j
dom4j經常不用,方法忘了又記,故做出讀取xml和把document寫入xml的小小工具~~~
/** * 讀取document和將document對象寫入到xml的小工具 * 使用該類必須給出dom4j的jar包 * @author hui.zhang * */public class Dom4jUtils { private Dom4jUtils() {} /** * 通過路徑擷取document對象 * @param pathname xml的路徑 * @return 返回document對象 * @throws DocumentException */ public static Document getDocument(File pathname) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(pathname); return document; } /** * 傳一個document對象寫入到指定xml路徑下 * @param path 寫迴路徑 * @param document 傳一個document對象 * @throws IOException */ static void write2XML(File path, Document document) throws IOException { OutputFormat format = OutputFormat.createPrettyPrint(); //format.setEncoding("UTF-8");//預設的編碼就是UTF-8 XMLWriter xml = new XMLWriter(new FileOutputStream(path), format); xml.write(document); } }