dom4j删除节点
答案:1 悬赏:70 手机版
解决时间 2021-11-28 00:49
- 提问者网友:我一贱你就笑
- 2021-11-27 03:43
dom4j删除节点
最佳答案
- 五星知识达人网友:冷風如刀
- 2021-11-27 04:16
Document doc = DocumentHelper.createDocument();
Element scohool = doc.addElement("school");
scohool.addElement("teacher").addComment("this is a 优秀的老师").addAttribute("age","33").addAttribute("face","帅").setText("老吴");
Element ele = scohool.addElement("haha");
ele.addElement("node1-01").addAttribute("name", "11");
ele.addElement("node1-01").addAttribute("name", "12");
ele.addElement("node1-01").addAttribute("name", "13");
ele.addElement("node1-01").addAttribute("name", "14");
ele.addElement("node1-01").addAttribute("name", "15");
List list = ele.elements();
for (int i = 0; i < list.size(); i++) {
Element e = (Element)list.get(i);
String value = e.attributeValue("name",null);
if("14".equals(value)){
ele.remove(e);
}
}
try {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GB2312");
XMLWriter out = new XMLWriter(new FileOutputStream("e:/1.xml"),format);
out.write(doc);
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}追问谢谢,我试试哦
Element scohool = doc.addElement("school");
scohool.addElement("teacher").addComment("this is a 优秀的老师").addAttribute("age","33").addAttribute("face","帅").setText("老吴");
Element ele = scohool.addElement("haha");
ele.addElement("node1-01").addAttribute("name", "11");
ele.addElement("node1-01").addAttribute("name", "12");
ele.addElement("node1-01").addAttribute("name", "13");
ele.addElement("node1-01").addAttribute("name", "14");
ele.addElement("node1-01").addAttribute("name", "15");
List list = ele.elements();
for (int i = 0; i < list.size(); i++) {
Element e = (Element)list.get(i);
String value = e.attributeValue("name",null);
if("14".equals(value)){
ele.remove(e);
}
}
try {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GB2312");
XMLWriter out = new XMLWriter(new FileOutputStream("e:/1.xml"),format);
out.write(doc);
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}追问谢谢,我试试哦
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯