Ad Hoc Element Creation

I've been removing the dependency on the Oracle XML parser from some
server-side code and have run into an issue.  The current version of the
Oracle parser allows an Element to be created without reference to any
document:

	Element e = new XMLElement("foo");

(This functionality will be deprecated in the next release of the parser
according to the Beta code.)  This is primarily being used when serializing
elements to XML, e.g.:

class foo {
   public Node toXml() {
      Element e = new XMLElement("foo");
      // add attributes and child elements, etc.
      return e;
   }
}

The resulting Node can be added to any document without having to do a
cloneNode() since it isn't owned by any other document.

My question is:  what is the best way to provide for this type of behavior
in a parser-neutral way?  I can think of a couple of solutions:

1.  Create a global document and use that as an element factory.

This would seem to have a couple of problems: one, the resultant element
tree would have to be "cloneNode"-d before adding it to the new document;
and two, in a server-side environment, there would be an awful lot of
Elements being created that are owned by the factory document.


2.  Refactor the code so that the parent document that the element tree
would be added to is always around:

	Document doc = new XMLDocument();
	Element e = foo.toXml(doc);
	doc.appendChild(e);

This could be done, but it seems awkward to me.


What would be the best approach?  What was the reason behind Elements always
being created in the context of a specific document?


Thanks.

Received on Monday, 5 February 2001 11:54:49 UTC