Xpath解析XML(使用DOM4J)
阿新 • • 發佈:2018-12-09
最近在專案中需要使用XML作為資料對接載體,故解析XML檔案與字串、建立XML節點、修改XML節點的資訊及其attribute資訊、刪除節點、查詢節點的作為最使用的最基礎階段。 對比了多種xpath的多種包,發現dom4j是其中功能較為強大的工具,其中https://www.tutorialspoint.com/java_xml/java_dom4j_parser.htm包含了較為簡單的使用方式;本文章列一些其他便於除錯的小方法,希望對dom4j的使用者帶來幫助。 dom4j 列印一個node及其子節點資訊 Document document = DocumentHelper.createDocument(); document.setRootElement(root); System.out.println(document.asXML())
xml檔案的換行與縮排:
OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); format.setNewLineAfterDeclaration(false); format.setIndent(true); XMLWriter writer = new XMLWriter(out, format); document.setXMLEncoding("UTF-8"); writer.write(document);
時間關係未完待續。