Java XML Node Clone copyNode(Node source, Node dest)

Here you can find the source of copyNode(Node source, Node dest)

Description

Copy one node to another node.

License

Apache License

Parameter

Parameter Description
source source Node
dest destination Node

Return

destination Node

Declaration

public static synchronized Node copyNode(Node source, Node dest) 

Method Source Code

//package com.java2s;
/*/*from   w  ww. j av  a  2s  .c om*/
 * Copyright 2008-2015 Jose Luis Martin.
 *
 * Licensed 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.CDATASection;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class Main {
    /**
     * Copy one node to another node.
     * @param source source Node
     * @param dest destination Node
     * @return destination Node
     */
    public static synchronized Node copyNode(Node source, Node dest) {
        if (source.getNodeType() == Node.TEXT_NODE) {
            Text tn = dest.getOwnerDocument().createTextNode(source.getNodeValue());
            return tn;
        }

        Node attr = null;
        NamedNodeMap attrs = source.getAttributes();

        if (attrs != null) {
            for (int i = 0; i < attrs.getLength(); i++) {
                attr = attrs.item(i);
                ((Element) dest).setAttribute(attr.getNodeName(), attr.getNodeValue());
            }
        }

        Node child = null;
        NodeList list = source.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            child = list.item(i);
            if (!(child instanceof Text)) {
                Element en = dest.getOwnerDocument().createElementNS(child.getNamespaceURI(), child.getNodeName());

                if (child.getNodeValue() != null) {
                    en.setNodeValue(child.getNodeValue());
                }

                Node n = copyNode(child, en);
                dest.appendChild(n);
            } else if (child instanceof CDATASection) {
                CDATASection cd = dest.getOwnerDocument().createCDATASection(child.getNodeValue());
                dest.appendChild(cd);
            } else {
                Text tn = dest.getOwnerDocument().createTextNode(child.getNodeValue());
                dest.appendChild(tn);
            }
        }
        return dest;
    }
}

Related

  1. cloneNode(Node node, Document doc)
  2. cloneNode(Node node, Document target, boolean deep)
  3. copyGraphicFiles(String baseFilename, String currentDirectory, String fileSeparator, NodeIterator graphicsElements)
  4. copyInto(Node src, Node dest)
  5. copyNode(Node source)