W3C home > Mailing lists > Public > www-dom@w3.org > October to December 2001

Re: setting document root element

From: Garret Wilson <garret@globalmentor.com>
Date: Sun, 11 Nov 2001 20:15:33 -0800
Message-ID: <001501c16b30$acc505c0$5b9436d0@odysseus2001>
To: "www-dom list" <www-dom@w3.org>
Cc: "Razvan Costea-Barlutiu" <cbrazvan@baltan.bsd.uchicago.edu>
Thanks for the comment, Razvan. See my reply below.

----- Original Message -----
From: "Razvan Costea-Barlutiu" <cbrazvan@baltan.bsd.uchicago.edu>
To: "www-dom list" <www-dom@w3.org>
Sent: Wednesday, September 19, 2001 10:12 AM
Subject: Re: setting document root element


> I think the approach taken so far is correct. The root node belongs only
to
> the document object and this one has to manage it.

I agree that the Document owns the root note and has to manage it. Why can't
the document *change* its root node, just like an element can change *its*
children?

> Even if any node cannot live outside the Document node, i don't think that
> an element should be allowed to modify the structure of the document in
> which resides.

I'm not asking for an element to be allowed to modify the structure of the
document in which it resides. I'm asking for the ability for a document to
change its own structure. Again, an element can change its structure -- why
not a document?

> I think this is a good separation...

I don't see why. An element is an element is an element -- the only
difference with the document element is that it happens to be at the top of
the hierarchy. (OK, it has to have the same name as the doctype, but that's
negligible.)

> A compromise approach would be to introduce a special node object in the
> DOM, called RootElement, or something like that, but i don't think it is
> worthing to go that way.

And that wouldn't address the problem I raised.

To better see what ugly code the current architecture forces, consider a
<russianDoll> element allows child RussianDoll elements. How would we
convert a Java object tree into a DOM tree?

Here's how one must currently do it:

main
{
  RussianDoll russianDoll; //object to serialize
  Document doc=createRussianDollDoc(russianDoll);
}

Document createRussianDollDoc(RussianDoll russianDoll)
{
  Document doc=DOMImplementation.createDocument("russianDoll");
  Element root=doc.getDocumentElement();
//add all the attributes to the <russianDoll> element
  Element child=createRussianDollElement(russianDoll.getChild());
  root.appendChild(child);
  return doc;
}

Element createRussianDollElement(Document doc, RussianDoll russianDoll)
{
  Element element=doc.createElement("russianDoll");
//add all the attributes to the <russianDoll> element
  Element child=createRussianDollElement(russianDoll.getChild());
  element.appendChild(child);
  return element;
}

Notice that we now have to have two duplicate methods to create and populate
<russianDoll> elements, based upon whether the element is the document
element. Yet the content of all <russianDoll> elements are identical,
wherever they are located.

> ...even if the coding gets somehow ugly following this approach (i agree
to that).

Yep. So, if anyone knows how I can contribute to something better getting
into DOM Level 3, let me know. If I were to join the WG, would that help?

Cheers,

Garret Wilson
GlobalMentor, Inc.
Received on Sunday, 11 November 2001 23:18:08 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:13:55 GMT