Java XML Document Clone copyChildren(Document new_doc, Node node, Node new_node)

Here you can find the source of copyChildren(Document new_doc, Node node, Node new_node)

Description

copy Children

License

Open Source License

Declaration

public final static void copyChildren(Document new_doc, Node node, Node new_node) 

Method Source Code


//package com.java2s;
/*/*  w ww. j av  a  2s . c  o m*/
 * eXist Open Source Native XML Database
 * Copyright (C) 2001-2007 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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class Main {
    public final static void copyChildren(Document new_doc, Node node, Node new_node) {
        final NodeList children = node.getChildNodes();
        Node new_child;
        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: {
                new_child = copyNode(new_doc, child);
                new_node.appendChild(new_child);
                break;
            }
            case Node.ATTRIBUTE_NODE: {
                new_child = copyNode(new_doc, child);
                ((Element) new_node).setAttributeNode((Attr) new_child);
                break;
            }
            case Node.TEXT_NODE: {
                new_child = copyNode(new_doc, child);
                new_node.appendChild(new_child);
                break;
            }
            // TODO : error for any other one -pb
            }
        }
    }

    public final static Node copyNode(Document new_doc, Node node) {
        Node new_node;
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            new_node = new_doc.createElementNS(node.getNamespaceURI(), node.getNodeName());
            copyChildren(new_doc, node, new_node);
            return new_node;
        case Node.TEXT_NODE:
            new_node = new_doc.createTextNode(((Text) node).getData());
            return new_node;
        case Node.ATTRIBUTE_NODE:
            new_node = new_doc.createAttributeNS(node.getNamespaceURI(), node.getNodeName());
            ((Attr) new_node).setValue(((Attr) node).getValue());
            return new_node;
        default:
            // TODO : error ? -pb
            return null;
        }
    }
}

Related

  1. cloneDOM(final Document src)
  2. cloneDOM(Node node, Document document)
  3. cloneNode(Document d, Node n)
  4. cloneNode(Document document, Node node, boolean deep)
  5. copy(Document from, Document to)
  6. copyChildren(Element from, Element to, Document doc)
  7. copyChildren(final Document newDoc, final Node node, final Node newNode)
  8. copyDocument(Document document)
  9. copyDocument(Document from, Document to)