[jdom-interest] Important proposal: Element/Document changes
Jools Enticknap
jools at jools.org
Thu Jul 27 01:47:27 PDT 2000
<snip/>
> > > Now some methods on Document:
> > >
> > > Document addContent(Element root)
> > > Document addContent(ProcessingInstruction pi)
> > > Document addContent(Comment comment)
> > > Document addContent(Entity entity) // Do we need this?
> > > Similar to the methods on Element. Cleans things up since there's no
> > > reason to have addComment(Comment) on Document but addContent(Comment)
> > > on Element. The addContent() method will be checked so only one Element
> > > can be added to a document.
> >
> > +1 for all except addContent(Element root).
> > I'm guessing that addContent(Element root) will replace the previous
> > root element ? then it's not adding its replacing.
>
> The issue is how you do a document like this:
>
> <!-- comment -->
> <root/>
> <!-- comment -->
>
> It's a whole lot easier with the above using addContent(Element) than
> trying to make setRootElement() behave right. If you
> addContent(Element) and there's already an element, then we throw an
> IllegalAddEx.
>
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.
But what if you want to change the "root" element ? I'm guessing that I'd
have to do;
Element foo = new Element("foo");
doc.removeContent(element);
doc.addContent(foo);
But won't that end up with;
<!-- comment -->
<!-- comment -->
<foo>
Or have I missed the plot ?
--Jools
More information about the jdom-interest
mailing list