[jdom-interest] Important proposal: Element/Document changes

Jools Enticknap jools at jools.org
Thu Jul 27 04:09:55 PDT 2000


On Thu, 27 Jul 2000, Jason Hunter wrote:

> > OK, I see what you are getting at now.
> > 
> > So to achieve;
> > 
> >         <!-- comment -->
> >         <root/>
> >         <!-- comment -->
> > 
> > You would write;
> > 
> >         Document doc = new Document();
> >         Comment comment = new Comment("comment");
> >         Element element = new Element("root");
> > 
> >         doc.addContent(comment);
> >         doc.addContent(element);
> >         doc.addContent(comment);
> > 
> > Fine happy with that.
> 
> You could have issues with double use of the Comment instance should you
> get deep into manipulating the document, but basically yes.
> 
> > But what if you want to change the "root" element ? 
> 
> How about just
>   doc.setRootElement(newRoot);
> 
> setRootElement() can be smart enough to look for the sole element and
> replace it.

I was under the impression that setRootElement() was going due to the
problems involved in making it behave ? 

Anyhow ... so, in order to add the "root" element to a Document you could;

	Document doc = new Document();	
	Element elem = new Element("root")

	// Which will call elem.setIsRootElement(true);
	doc.setRootElement(elem);

In this scenario we don't get an Exception, because the method is explicit
about what it does.

However.

        Document doc = new Document();
        Element elem = new Element("root")

	try {
        	doc.addContent(elem);
 	} catch(IllegalAddException ila) {
		// handle the exception or just call setRootElement(elem);
	}
 

addContent(Element) is implicit, it states it'll add an Element to a
document, and set it as the 'root' element unless one is there already, 
then it'll throw an exception.

setRootElement(Element) is explicitly stating that it will set the Element
and remove the old Element calling setIsRootElement() on the respective
elements.

So to summarize;

Do we need two methods for setting the Root element of a Document ?

--Jools
 

> 
> -jh-
> 






More information about the jdom-interest mailing list