Here you can find the source of copyNode(Node source, Node dest)
Parameter | Description |
---|---|
source | source Node |
dest | destination Node |
public static synchronized Node copyNode(Node source, Node dest)
//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; } }