[jdom-interest] detach() [eg]

Uwe_Hale at i2.com Uwe_Hale at i2.com
Mon Apr 23 11:33:16 PDT 2001

>I question the use case. How many times do you want
>to move the root element of a document?

In my case quite often.  I'm working on a editor.  I parse text and then
grab the root element of the parsed document and add it to or replace a
element in another tree.  Since the element I'm trying to add has a
document as a parent, it won't allow it to be added to another element.  I
first have to set the documents root to null.  Like this:

          // parse text
            SAXBuilder builder = new SAXBuilder(false);
            doc = builder.build(new StringReader(xmlText));

            Element newElem = doc.getRootElement();
            doc.setRootElement(null); // detach it first

          // replace elem with
            Element parentElem = elem.getParent();
            List list = parentElem.getChildren();

          // get around list problem with only 1 child
            if( list.size() == 1 )
                int index = list.indexOf(elem);
                list.add(index, newElem);


More information about the jdom-interest mailing list