changing the document element of a DOM

A

Andy Fish

Hi,

I'm using XML DOM in Java and I have a function like this

Element makeSomeXml(Document doc) {..}

This returns an Element owned by (but not appended to) the specified
document.

Now, when I call this function, sometimes I want to append the result
somewhere in the document, but sometimes I want to have the returned element
as a document in it's own right (i.e. as the root of the XML document). In
principle this seems like a reasonable requirement. I have an XML Element
and it's up to me whether I want to use it as the root element or plug it
into a bigger document.

The problem is that AFAIK a document must always have a root element, and
the root element cannot be replaced. nor can an existing element be
"upgraded" to be a document in it's own right. So I can't see a clean way of
doing what I want.

This seems a bit of a limitation in the way DOM represents the document
element - it seems to treat the root element and the document itself as the
same thing which isn't really the case. If I was editing XML in a file I
could leave all the namespace declarations and other stuff in place and
replace the root element.

Have I misunderstood the way the DOM works at the top level? Is there a
simple clean solution to what I want to do?

Many thanks in advance

Andy
 
M

Martin Honnen

Andy Fish wrote:

This seems a bit of a limitation in the way DOM represents the document
element - it seems to treat the root element and the document itself as the
same thing which isn't really the case.

The W3C DOM clearly distinguishes Document nodes and Element nodes. I
don't see why you cannot replace the documentElement node of a document:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Test20040703 {
public static void main (String[] args) {
try {
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder();

DOMImplementation domImplementation =
documentBuilder.getDOMImplementation();

Document xmlDocument = domImplementation.createDocument("",
"gods", null);

Element god = xmlDocument.createElement("god");

xmlDocument.getDocumentElement().appendChild(god);

Element devils = xmlDocument.createElement("devils");

xmlDocument.replaceChild(devils, xmlDocument.getDocumentElement());

Element devil = xmlDocument.createElement("devil");

xmlDocument.getDocumentElement().appendChild(devil);

System.out.println(xmlDocument.getDocumentElement().getNodeName());
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top