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

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

Description

clone Node

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/**/*w  w  w.j a  v a2  s .com*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the 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 {
    public static Node cloneNode(Document document, Node node, boolean deep) throws DOMException {
        if (document == null || node == null) {
            return null;
        }
        int type = node.getNodeType();

        if (node.getOwnerDocument() == document) {
            return node.cloneNode(deep);
        }
        Node clone;
        switch (type) {
        case Node.CDATA_SECTION_NODE:
            clone = document.createCDATASection(node.getNodeValue());
            break;
        case Node.COMMENT_NODE:
            clone = document.createComment(node.getNodeValue());
            break;
        case Node.ENTITY_REFERENCE_NODE:
            clone = document.createEntityReference(node.getNodeName());
            break;
        case Node.ELEMENT_NODE:
            clone = document.createElementNS(node.getNamespaceURI(), node.getNodeName());
            NamedNodeMap attributes = node.getAttributes();
            for (int i = 0; i < attributes.getLength(); i++) {
                Attr attr = (Attr) attributes.item(i);
                Attr attrnew = ((Element) clone).getOwnerDocument().createAttributeNS(attr.getNamespaceURI(),
                        attr.getNodeName());
                attrnew.setValue(attr.getNodeValue());
                ((Element) clone).setAttributeNodeNS(attrnew);
            }
            break;

        case Node.TEXT_NODE:
            clone = document.createTextNode(node.getNodeValue());
            break;
        default:
            return null;
        }
        if (deep && type == Node.ELEMENT_NODE) {
            Node child = node.getFirstChild();
            while (child != null) {
                clone.appendChild(cloneNode(document, child, true));
                child = child.getNextSibling();
            }
        }
        return clone;
    }
}

Related

  1. cloneDocument(final Document doc)
  2. cloneDocument(final Document doc)
  3. cloneDOM(final Document src)
  4. cloneDOM(Node node, Document document)
  5. cloneNode(Document d, Node n)
  6. copy(Document from, Document to)
  7. copyChildren(Document new_doc, Node node, Node new_node)
  8. copyChildren(Element from, Element to, Document doc)
  9. copyChildren(final Document newDoc, final Node node, final Node newNode)