Java XML Node Clone cloneNode(Node node, Document target, boolean deep)

Here you can find the source of cloneNode(Node node, Document target, boolean deep)

Description

Clone given Node into target Document.

License

Open Source License

Declaration

public static Node cloneNode(Node node, Document target, boolean deep)
        throws DOMException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Attr;

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

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    /**//from ww  w. j av a 2s . c  om
     * Clone given Node into target Document. If targe is null, same Document will be used.
     * If deep is specified, all children below will also be cloned.
     */
    public static Node cloneNode(Node node, Document target, boolean deep)
            throws DOMException {
        if (target == null || node.getOwnerDocument() == target)
            // same Document
            return node.cloneNode(deep);
        else {
            //DOM level 2 provides this in Document, so once xalan switches to that,
            //we can take out all the below and just call target.importNode(node, deep);
            //For now, we implement based on the javadocs for importNode
            Node newNode;
            int nodeType = node.getNodeType();

            switch (nodeType) {
            case Node.ATTRIBUTE_NODE:
                newNode = target.createAttribute(node.getNodeName());

                break;

            case Node.DOCUMENT_FRAGMENT_NODE:
                newNode = target.createDocumentFragment();

                break;

            case Node.ELEMENT_NODE:

                Element newElement = target.createElement(node
                        .getNodeName());
                NamedNodeMap nodeAttr = node.getAttributes();

                if (nodeAttr != null)
                    for (int i = 0; i < nodeAttr.getLength(); i++) {
                        Attr attr = (Attr) nodeAttr.item(i);

                        if (attr.getSpecified()) {
                            Attr newAttr = (Attr) cloneNode(attr, target,
                                    true);
                            newElement.setAttributeNode(newAttr);
                        }
                    }

                newNode = newElement;

                break;

            case Node.ENTITY_REFERENCE_NODE:
                newNode = target.createEntityReference(node.getNodeName());

                break;

            case Node.PROCESSING_INSTRUCTION_NODE:
                newNode = target.createProcessingInstruction(
                        node.getNodeName(), node.getNodeValue());

                break;

            case Node.TEXT_NODE:
                newNode = target.createTextNode(node.getNodeValue());

                break;

            case Node.CDATA_SECTION_NODE:
                newNode = target.createCDATASection(node.getNodeValue());

                break;

            case Node.COMMENT_NODE:
                newNode = target.createComment(node.getNodeValue());

                break;

            case Node.NOTATION_NODE:
            case Node.ENTITY_NODE:
            case Node.DOCUMENT_TYPE_NODE:
            case Node.DOCUMENT_NODE:
            default:
                throw new IllegalArgumentException("Importing of " + node
                        + " not supported yet");
            }

            if (deep)
                for (Node child = node.getFirstChild(); child != null; child = child
                        .getNextSibling())
                    newNode.appendChild(cloneNode(child, target, true));

            return newNode;
        }
    }
}

Related

  1. clone(final N node)
  2. cloneNode(Node node)
  3. cloneNode(Node node, Document doc)
  4. copyGraphicFiles(String baseFilename, String currentDirectory, String fileSeparator, NodeIterator graphicsElements)
  5. copyInto(Node src, Node dest)
  6. copyNode(Node source)
  7. copyNode(Node source, Node dest)