Java Utililty Methods XML Document Clone

List of utility methods to do XML Document Clone

Description

The list of methods to do XML Document Clone are organized into topic(s).

Method

Documentclone(Document doc)
Clone a document so it can be safely modified on a per-request basis.
Document d;
try {
    d = newBuilder().newDocument();
} catch (ParserConfigurationException e) {
    throw new IOException("Cannot clone document");
Node n = d.importNode(doc.getDocumentElement(), true);
d.appendChild(n);
...
Documentclone(Document inDoc)
Clone a Document.
Document rtnVal = (Document) inDoc.cloneNode(true);
return (rtnVal);
voidcloneAndAppend(Document document, Node node)
clone And Append
node = node.cloneNode(true);
document.adoptNode(node);
document.appendChild(node);
DocumentcloneDocument(Document document)
clone Document
if (document == null)
    return null;
Document result = newDocument();
try {
    identityTransformer.transform(new DOMSource(document), new DOMResult(result));
} catch (TransformerException e) {
    e.printStackTrace();
return result;
DocumentcloneDocument(final Document doc)
Clones a document object.
final Node rootNode = doc.getDocumentElement();
final TransformerFactory tfactory = TransformerFactory.newInstance();
final Transformer tx = tfactory.newTransformer();
final DOMSource source = new DOMSource(doc);
final DOMResult result = new DOMResult();
tx.transform(source, result);
final Document copy = (Document) result.getNode();
copy.removeChild(copy.getDocumentElement());
...
DocumentcloneDocument(final Document doc)
Creates an exact clone of a Document.
if (doc == null) {
    throw new IllegalArgumentException("The document source cannot be null.");
return (Document) doc.cloneNode(true);
DocumentcloneDOM(final Document src)
Create a clone of a DOM object
final Transformer transformer = sTransformerFactory.newTransformer();
final DOMSource srcDom = new DOMSource(src);
final DOMResult resultDom = new DOMResult();
transformer.transform(srcDom, resultDom);
return (Document) resultDom.getNode();
NodecloneDOM(Node node, Document document)
Recursively copy the DOM tree using the specified document as the root document factory.
Node clone = null;
if (node != null) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        clone = document.createElement(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        int nattrs = (attrs == null ? 0 : attrs.getLength());
        for (int a = 0; a < nattrs; a++) {
...
NodecloneNode(Document d, Node n)
clone Node
Node r = null;
switch (n.getNodeType()) {
case Node.TEXT_NODE:
    r = d.createTextNode(((Text) n).getData());
    break;
case Node.CDATA_SECTION_NODE:
    r = d.createCDATASection(((CDATASection) n).getData());
    break;
...
NodecloneNode(Document document, Node node, boolean deep)
clone Node
if (document == null || node == null) {
    return null;
int type = node.getNodeType();
if (node.getOwnerDocument() == document) {
    return node.cloneNode(deep);
Node clone;
...