Copies the source tree into the specified place in a destination tree. : Node « XML « Java Tutorial






import org.w3c.dom.Attr;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import com.sun.org.apache.xerces.internal.dom.AttrImpl;
import com.sun.org.apache.xerces.internal.dom.DocumentImpl;

/*
 * 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.
 */

public class Main {
  
  /**
   * Copies the source tree into the specified place in a destination
   * tree. The source node and its children are appended as children
   * of the destination node.
   * <p>
   * <em>Note:</em> This is an iterative implementation.
   */
  public static void copyInto(Node src, Node dest) throws DOMException {
      
      // get node factory
      Document factory = dest.getOwnerDocument();
      boolean domimpl = factory instanceof DocumentImpl;
      
      // placement variables
      Node start  = src;
      Node parent = src;
      Node place  = src;
      
      // traverse source tree
      while (place != null) {
          
          // copy this node
          Node node = null;
          int  type = place.getNodeType();
          switch (type) {
          case Node.CDATA_SECTION_NODE: {
              node = factory.createCDATASection(place.getNodeValue());
              break;
          }
          case Node.COMMENT_NODE: {
              node = factory.createComment(place.getNodeValue());
              break;
          }
          case Node.ELEMENT_NODE: {
              Element element = factory.createElement(place.getNodeName());
              node = element;
              NamedNodeMap attrs  = place.getAttributes();
              int attrCount = attrs.getLength();
              for (int i = 0; i < attrCount; i++) {
                  Attr attr = (Attr)attrs.item(i);
                  String attrName = attr.getNodeName();
                  String attrValue = attr.getNodeValue();
                  element.setAttribute(attrName, attrValue);
                  if (domimpl && !attr.getSpecified()) {
                      ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                  }
              }
              break;
          }
          case Node.ENTITY_REFERENCE_NODE: {
              node = factory.createEntityReference(place.getNodeName());
              break;
          }
          case Node.PROCESSING_INSTRUCTION_NODE: {
              node = factory.createProcessingInstruction(place.getNodeName(),
                      place.getNodeValue());
              break;
          }
          case Node.TEXT_NODE: {
              node = factory.createTextNode(place.getNodeValue());
              break;
          }
          default: {
              throw new IllegalArgumentException("can't copy node type, "+
                      type+" ("+
                      node.getNodeName()+')');
          }
          }
          dest.appendChild(node);
          
          // iterate over children
          if (place.hasChildNodes()) {
              parent = place;
              place  = place.getFirstChild();
              dest   = node;
          }
          
          // advance
          else {
              place = place.getNextSibling();
              while (place == null && parent != start) {
                  place  = parent.getNextSibling();
                  parent = parent.getParentNode();
                  dest   = dest.getParentNode();
              }
          }
          
      }
      
  } // copyInto(Node,Node)
}








33.29.Node
33.29.1.Add a text node to the element
33.29.2.Add a text node to the beginning of the element
33.29.3.Add a text node before the last child of the element
33.29.4.Add a text node in front of the new item element
33.29.5.Adding a Text Node to a DOM Document
33.29.6.Removing a Node from a DOM Document
33.29.7.Remove All nodes
33.29.8.Get the text node
33.29.9.Change a particular node in XML
33.29.10.Add another element after the first child of the root element
33.29.11.Create a new element and move the middle text node to it
33.29.12.Move all children of the element in front of the element
33.29.13.Split the node at the beginning of the word
33.29.14.Compare two DOM Nodes from JBoss
33.29.15.Convert NodeList To Node Array
33.29.16.Convert node element To String
33.29.17.Search our next siblings for a given node
33.29.18.Search earlier siblings for a given node
33.29.19.Returns a first child DOM Node of type ELEMENT_NODE for the specified Node
33.29.20.Search up the tree for a given node
33.29.21.Get the first text node associated with this element
33.29.22.Simplified implementation of a Node from a Document Object Model (DOM)
33.29.23.Returns a list of value for the given node
33.29.24.Returns the value of the child node with the given name
33.29.25.Remove this node from its parent.
33.29.26.Find the first text descendent node of an element
33.29.27.Copies the source tree into the specified place in a destination tree.
33.29.28.DOM helper for root element
33.29.29.Compare two DOM Nodes