[jdom-interest] Removing and adding element

Dimitrios Kolovos dk135 at york.ac.uk
Fri Feb 25 10:54:18 PST 2005


I have the following code:

  public static void main(String[] args) throws IOException, JDOMException {
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build("demo.xml");
    Element el = (Element) doc.getRootElement().getChildren().get(0);

    Element parent = (Element) el.getParent();

    parent.removeContent(1);
    parent.addContent(-->2, el);

    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    outputter.output(doc,System.out);

  }

and demo.xml

<?xml version="1.0"?>
<root>
 <child1/>
 <child2/>
 <child3/>
</root>

The output of this program should be (as I perceive it)

<?xml version="1.0"?>
<root>
 <child2/>
 <child1/>
 <child3/>
</root>

instead the output is:
<?xml version="1.0"?>
<root>
 <child1/>
 <child2/>
 <child3/>
</root>

What am I doing wrong?

Cheers,
Dimitrios



More information about the jdom-interest mailing list