Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

[jdom-interest] Removing and adding element

Dimitrios Kolovos

2005-02-25

Replies:

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

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.