<decconf>
<admin id="o " name="tianxian">
<user sid="1" name="">
<chn no="" />
<chn no="" />
<chn no="" />
<chn no="" />
</decconf>我想删除 chn节点如何删除 no的值 我未知 我想读xml时 发现有这个节点 就删除 不知 如何操作谢谢了
<admin id="o " name="tianxian">
<user sid="1" name="">
<chn no="" />
<chn no="" />
<chn no="" />
<chn no="" />
</decconf>我想删除 chn节点如何删除 no的值 我未知 我想读xml时 发现有这个节点 就删除 不知 如何操作谢谢了
// jdom读取xml
@SuppressWarnings("unchecked")
public static void readXML() {
Document doc = null;
SAXBuilder sb = new SAXBuilder(); // 建立构造器
try {
doc = sb.build(new FileInputStream("f:/test.xml"));
Element root = doc.getRootElement(); // 获得根节点
List list = root.getChildren(); // 将根节点下的所有子节点放入List中
int size = list.size();
for (int i = 0; i < size; i++) {
Element item = (Element) list.get(i); // 取得节点实例
if ("chn".equals(item.getName())) { // 如果是chn标记。。则在if中写自己的操作
// root.removeChild("chn"); // 移除节点 都可以
root.removeContent(item); // 移除节点 都可以
i--;
size--;
}
item.removeAttribute("sid"); // 移除属性sid
}
XMLOutputter op = new XMLOutputter();
op.output(doc, new FileOutputStream("f:/test.xml"));
op.clone();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
因为当移除一个Element后,List中的总长度会减少1...也就是总的树会实时减少1.LZ可以自己debug看一下!!不然会移除不完整