Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I clone an entire Document using the Java DOM?

Tags:

java

dom

xml

I'm looking for a reliable, implementation-independent way to clone an entire Document. The Javadocs specifically say that calling cloneNode on a Document is implementation-specific. I've tried passing the Document through a no-op Transformer, but the resulting Node has no owner Document.

I could create a new Document and import the nodes from the old one, but I'm afraid there might be bits of Document metadata that get lost. Same thing with writing the Document to a string and parsing it back in.

Any ideas?

By the way, I'm stuck at Java 1.4.2, for reasons beyond my control.

like image 759
Adam Crume Avatar asked Nov 10 '08 21:11

Adam Crume


1 Answers

As some of the comments point out, there are problems with serializing and re-parsing a document. In addition to memory usage, performance considerations, and normalization, there's also loss of the prolog (DTD or schema), potential loss of comments (which aren't required to be captured), and loss of what may be significant whitespace. Serialization should be avoided.

If the real goal is to make a copy of an existing DOM Document object, then it should be handled programmatically, in memory. Thanksfully there is a relatively easy way to do this using features available in Java 5 or using external XSLT libraries like Xalan, which is to a a pass-through transformation.

Below is shown the Java 5 solution:

TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer tx   = tfactory.newTransformer();
DOMSource source = new DOMSource(doc);
DOMResult result = new DOMResult();
tx.transform(source,result);
return (Document)result.getNode();

That's basically it. You'll need to handle exceptions and may wish to configure the transformer, but I leave that as an exercise for the reader.

like image 133
Ichiro Furusato Avatar answered Sep 18 '22 16:09

Ichiro Furusato