jdom 中removeContent的问题?
代码如下:
<accounts>
<account>
<name>admin</name>
<password>admin</password>
<age>29</age>
<phone>4543545</phone>
</account>
<account>
<name>qq</name>
<password>qq</password>
<age>22</age>
<phone>4453454545</phone>
</account>
<accounts>
private Element init() throws Exception{
File file=new File(filePath);
SAXBuilder builder=new SAXBuilder();
Document doc=builder.build(file);
return doc.getRootElement();
}
private void save(Element root)throws Exception{
XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setEncoding("GB2312"));
FileWriter file = new FileWriter(filePath);
out.output(root, file);
out.output(root, System.out);
}
public void remove(String name)throws Exception{
//初始化得到跟元素
Element root=init();
List rootChildren= init().getChildren();
for(int i=0;i<rootChildren.size();i++){
Element e=(Element) rootChildren.get(i);
List eChildren =e.getChildren();
for(int j=0;j<eChildren.size();j++){
Element el=(Element)eChildren.get(j);
if(el.getName().equals("name")&&el.getValue().equals(name)){
boolean b=root.removeContent(e);
System.out.println(b);
break;
}
}
}
//保存成xml文件
save(root);
}
为什么removeContent总是返回false?
试了好多遍都不行,帮忙一下谢咯!