Java XML Document Clone copyDocument(Document oldDocument, Document newDocument)

Here you can find the source of copyDocument(Document oldDocument, Document newDocument)

Description

Copies a document's content to another document (i.e.

License

Open Source License

Parameter

Parameter Description
oldDocument a parameter
newDocument a parameter

Return

the new containing document

Declaration

public static Document copyDocument(Document oldDocument, Document newDocument) 

Method Source Code

//package com.java2s;
/*//from   www. ja  v a  2  s. c o m
 * (J)ava (M)iscellaneous (U)tilities (L)ibrary
 *
 * JMUL is a central repository for utilities which are used in my
 * other public and private repositories.
 *
 * Copyright (C) 2013  Kristian Kutin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * e-mail: kristian.kutin@arcor.de
 */

import org.w3c.dom.Document;

import org.w3c.dom.Node;

public class Main {
    /**
     * Copies a document's content to another document (i.e. the root node is
     * adopted by the new document and inserted there). The new document should
     * be empty.
     *
     * @param oldDocument
     * @param newDocument
     *
     * @return the new containing document
     */
    public static Document copyDocument(Document oldDocument, Document newDocument) {

        Node root = oldDocument.getDocumentElement();

        Node adoptedRoot = newDocument.adoptNode(root);

        newDocument.appendChild(adoptedRoot);

        return newDocument;
    }
}

Related

  1. copyChildren(Document new_doc, Node node, Node new_node)
  2. copyChildren(Element from, Element to, Document doc)
  3. copyChildren(final Document newDoc, final Node node, final Node newNode)
  4. copyDocument(Document document)
  5. copyDocument(Document from, Document to)
  6. copyDocument(Document source)
  7. copyDocument(Element from, Element to, String newNamespace)
  8. copyDocumentNode(Node source, Document target)
  9. copyDomDocument(Document originalDomDoc)