Dom4j操作XML
創建XML
需要的jar包:dom4j.github.io
// 創建document對象
Document document = DocumentHelper.createDocument();
// 根節點
Element root = document.addElement("root");
// 添加屬性
root.addAttribute("id", "root");
// 添加子節點
Element head = root.addElement("head");
Element body = root.addElement("body");
// 添加內容
head.setText("hello world");
body.setText("hello body");
// 增加內容 ( 類似StringBuilder
head.addText(",this is head");
// 修改內容(再次set
body.setText("body內容已修改");
// 清除節點內容
body.clearContent();
// 刪除節點
root.remove(body);
System.out.println(document.asXML());
解析XML
URL url = new URL("https://www.zhihu.com/rss");
SAXReader reader = new SAXReader();
Document document = reader.read(url);

浙公網安備 33010602011771號