Here you can find the source of copyNode(Node source)
Parameter | Description |
---|---|
source | a source node |
public static Document copyNode(Node source)
//package com.java2s; /*//w w w . j a v a 2s . co m * #%L * Xacml4J Core Engine Implementation * %% * Copyright (C) 2009 - 2014 Xacml4J.org * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. * * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * <http://www.gnu.org/licenses/lgpl-3.0.html>. * #L% */ import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; import org.w3c.dom.Node; import com.google.common.base.Preconditions; public class Main { /** * Copies node and all its children * to the new document as root element * in the new document * * @param source a source node * @return {@link Document} a new DOM * document with copy of the node as * root element */ public static Document copyNode(Node source) { if (source == null) { return null; } Document sourceDoc = (source.getNodeType() == Node.DOCUMENT_NODE) ? (Document) source : source.getOwnerDocument(); Node rootNode = (source.getNodeType() == Node.DOCUMENT_NODE) ? sourceDoc .getDocumentElement() : source; Preconditions.checkState(sourceDoc != null); DOMImplementation domImpl = sourceDoc.getImplementation(); Document doc = domImpl.createDocument(sourceDoc.getNamespaceURI(), null, sourceDoc.getDoctype()); Node copy = doc.importNode(rootNode, true); doc.appendChild(copy); return doc; } }