[jdom-interest] Strange Element.addContent(index,Content) problem

Kai Woerner kai.woerner at uni-hamburg.de
Wed Mar 24 02:07:09 PST 2004


Hi all...

I just reconginzed that I'm too stupid to send my answers to the mailing
list, so i thought I'll let you know how my problem was solved:

It turned out that I had mixed content due to whitespace in my document 
(still don't know where that comes from) that kept me from getting the 
right index; suggestions for a solution included using 
elmt.getChildren().add("one") (which doesn't help, since it adds the 
content at the end of the whole content, not behind the last "one" 
child). I came out using the following code:

if (elmt.getChildren("two").size() > 0) {
     myIndex = elmt.getContent().indexOf(elmt.getChild("two"));
} else {
     myIndex = elmt.getContentSize();
}
elmt.addContent(myIndex,newElmt);

(as a reminder: i want to add an element "one" behind the last element 
"one" in a list like <one /><one /><two /><two />, whether there's 
whitespace somewhere or not)
This works for now, but I'm still wondering if there's a more 
straightforward solution (like elmt.addBeforeChild("two") or something 
like that - or is my scenario really that uncommon?

Kai Wörner






More information about the jdom-interest mailing list