0

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.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) {}

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

Community
  • 1
  • 1
kskaradzinski
  • 4,954
  • 10
  • 48
  • 70
  • 1
    please explain `is not working`... Also, what is the value of `nodeToDelete` before passing it to removeChild ? – Grooveek May 18 '11 at 13:17
  • I can't see any method with signature removeChild(int) in `org.w3c.dom.Element`, what is the class of your Element object ? – Grooveek May 18 '11 at 13:23
  • class is XOM, and nodeToDelete int i to n, And `its not working` i mean that is not removed – kskaradzinski May 18 '11 at 16:35

1 Answers1

0

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?

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268