Java XML Node Tree moveSubTree(Node from, Node to, Node context)

Here you can find the source of moveSubTree(Node from, Node to, Node context)

Description

Copies all child nodes from the first Element tree into the second Element tree inserted before the given context node.

License

Apache License

Parameter

Parameter Description
from The sub-tree to copy Nodes from.
to The sub-tree to copy Nodes to.
context The Node before which the children will be inserted

Return

The same Node as the second parameter (the new sub-tree).

Declaration

public static Node moveSubTree(Node from, Node to, Node context) 

Method Source Code

//package com.java2s;
/* /*from  ww  w. ja  va2s.  c  o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.Document;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * 
     * Copies all child nodes from the first Element tree into the second 
     * Element tree. Because a Node cannot have two parents, all of the 
     * children will be removed from the first tree before being appended 
     * to the second one.
     *
     * @param from
     *        The sub-tree to copy Nodes from.
     * 
     * @param to
     *        The sub-tree to copy Nodes to.
     * 
     * @return The same Node as the second parameter (the new sub-tree).
     *
     */
    public static Node moveSubTree(Node from, Node to) {
        return moveSubTree(from, to, null);
    }

    /**
     * 
     * Copies all child nodes from the first Element tree into the second 
     * Element tree inserted before the given context node. 
     * Because a Node cannot have two parents, all of the 
     * children will be removed from the first tree before being appended 
     * to the second one. If the context node is null, the new nodes will 
     * simply be appended to the list of children of the second Element tree. 
     *
     * @param from
     *        The sub-tree to copy Nodes from.
     * 
     * @param to
     *        The sub-tree to copy Nodes to.
     *        
     * @param context
     *         The Node before which the children will be inserted
     * 
     * @return The same Node as the second parameter (the new sub-tree).
     *
     */
    public static Node moveSubTree(Node from, Node to, Node context) {
        NodeList children = from.getChildNodes();
        Node[] asArray = convertToArray(children);

        Document fromDoc = from.getOwnerDocument();
        Document toDoc = to.getOwnerDocument();

        for (int n = 0; n < asArray.length; ++n) {
            from.removeChild(asArray[n]);

            if (fromDoc != toDoc)
                asArray[n] = toDoc.importNode(asArray[n], true);

            if (context == null)
                to.appendChild(asArray[n]);

            else
                to.insertBefore(asArray[n], context);
        }

        return to;
    }

    /**
     * 
     * @param list
     *        The list of Nodes to copy into a Node array.
     * 
     * @return An array holding a copy of the Nodes in the original NodeList.
     *
     */
    public static Node[] convertToArray(NodeList list) {
        int length = list.getLength();
        Node[] copy = new Node[length];

        for (int n = 0; n < length; ++n)
            copy[n] = list.item(n);

        return copy;
    }
}

Related

  1. dump(final Node node)
  2. dump(Node node)
  3. dumpTree(Node node)