0
我有以下代碼:使用Java XML問題removeChild之
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");
Java appending XML docs to existing docs
請解釋'不工作'...另外,在傳遞給removeChild之前'nodeToDelete'的值是多少? – Grooveek 2011-05-18 13:17:08
我看不到'org.w3c.dom.Element'中有簽名removeChild(int)的方法,Element對象的類是什麼? – Grooveek 2011-05-18 13:23:39
類是XOM,並且nodeToDelete int i到n,並且'它不工作'我的意思是不會被刪除 – 2011-05-18 16:35:32