首页 > 其他 > 详细

Dom4j操作XML

时间:2018-08-26 13:38:38      阅读:177      评论:0      收藏:0      [点我收藏+]

创建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);

Dom4j操作XML

原文:https://www.cnblogs.com/52liming/p/9536980.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!