Problems with Java XML RemoveChild
I have the following code:
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.prin开发者_StackOverflowt(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) {}
And my question is:
Why is removeChild(i)
not working?
EDIT: After a week i found the solution :)
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
Compile time error? org.w3c.dom.Element#removeChild
expects a Node
object rather then an integer value. Or do you use a different class named Element
?
精彩评论