dom4j之小小工具,dom4j

來源:互聯網
上載者:User

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);    }    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.