[jdom-interest] Removing and adding element

Jason Hunter jhunter at xquery.com
Fri Feb 25 11:35:43 PST 2005


Per the XML spec whitespace counts as content nodes.  Your math seems to 
be ignoring the whitespace text nodes.  If you want to reorder child 
elements, get the child elements as a list -- getChildren() -- and 
manipulate that list -- doing detaches before moves with .detach().

-jh-

Dimitrios Kolovos wrote:

> 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@yourhost.com
> 


More information about the jdom-interest mailing list