【发布时间】:2011-08-28 01:30:50
【问题描述】:
我有以下代码:
try {
Builder builder = new Builder();
Document doc = builder.build(Config.PATH +"incasation.xml");
Element root = doc.getRootElement();
Elements childs = root.getChildElements("locations");
int nodeToDelete = 0;
for(int i=0; i < childs.size(); i++)
{
Element child = childs.get(i);
System.out.print(child.getFirstChildElement("name").getValue());
System.out.print(" - ");
System.out.print(getLocationName().getText());
System.out.print(" - ");
System.out.println(child.getFirstChildElement("name").getValue().equals(getLocationName().getText()));
if(child.getFirstChildElement("name").getValue().equals(getLocationName().getText()))
{
nodeToDelete = i;
}
}
root.removeChild(nodeToDelete);
Simplexml sx = new Simplexml();
sx.save(root, Config.PATH +"incasation.xml");
}
catch(Exception e) {}
我的问题是:
为什么removeChild(i) 不起作用?
编辑: 一周后我找到了解决方案:)
root.removeChild(nodeToDelete);
Element root2 = (Element) root.copy();
Simplexml sx = new Simplexml();
sx.save(root2, Config.PATH +"incasation.xml");
【问题讨论】:
-
请解释
is not working... 另外,nodeToDelete在传递给 removeChild 之前的值是多少? -
我在
org.w3c.dom.Element中看不到任何带有签名 removeChild(int) 的方法,您的 Element 对象的类是什么? -
class 是 XOM,并且 nodeToDelete int i to n,而
its not working我的意思是没有被删除