Java XML Document Clone copyNode(final Document newDoc, final Node node)

Here you can find the source of copyNode(final Document newDoc, final Node node)

Description

copy Node

License

Open Source License

Declaration

public static final Node copyNode(final Document newDoc, final Node node) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w.  j  ava2  s  . c  om*/
 * eXist Open Source Native XML Database
 * Copyright (C) 2001-2014 The eXist Project
 * http://exist-db.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 2
 * 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *  
 *  $Id$
 */

import org.w3c.dom.*;

public class Main {
    public static final Node copyNode(final Document newDoc, final Node node) {
        final Node newNode;
        switch (node.getNodeType()) {

        case Node.ELEMENT_NODE:
            newNode = newDoc.createElementNS(node.getNamespaceURI(), node.getNodeName());
            final NamedNodeMap attributes = node.getAttributes();
            for (int i = 0; i < attributes.getLength(); i++) {
                final Attr attr = (Attr) attributes.item(i);
                final Attr newAttr = newDoc.createAttributeNS(attr.getNamespaceURI(), attr.getNodeName());
                newAttr.setValue(((Attr) node).getValue());
                ((Element) newNode).setAttributeNode(newAttr);
            }
            copyChildren(newDoc, node, newNode);
            break;

        case Node.TEXT_NODE:
            newNode = newDoc.createTextNode(((Text) node).getData());
            break;

        default:
            // TODO : error ? -pb
            newNode = null;
        }
        return newNode;
    }

    public static final void copyChildren(final Document newDoc, final Node node, final Node newNode) {
        final NodeList children = node.getChildNodes();
        Node newChild;
        for (int i = 0; i < children.getLength(); i++) {
            final Node child = children.item(i);
            if (child == null) {
                continue;
            }
            switch (child.getNodeType()) {
            case Node.ELEMENT_NODE: {
                newChild = copyNode(newDoc, child);
                newNode.appendChild(newChild);
                break;
            }
            case Node.TEXT_NODE: {
                newChild = copyNode(newDoc, child);
                newNode.appendChild(newChild);
                break;
            }
            // TODO : error for any other one -pb
            }
        }
    }
}

Related

  1. copyDocument(Element from, Element to, String newNamespace)
  2. copyDocumentNode(Node source, Document target)
  3. copyDomDocument(Document originalDomDoc)
  4. copyElement(Element orig, Document newOwner, Set blankTextNodes)
  5. copyNode(Document new_doc, Node node)
  6. deepCloneDocument(Document doc, DOMImplementation impl)
  7. deepCopyDocument(Document ttml, File target)