Copying a Subtree of Nodes in a DOM Document - Java XML

Java examples for XML:DOM

Description

Copying a Subtree of Nodes in a DOM Document

Demo Code


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Main {
  public static void main(String[] args) {
    Document doc = null;/*www . j a v  a  2 s.  c o  m*/
    NodeList list = doc.getElementsByTagName("entry");
    Element element = (Element) list.item(0);

    // Make a copy of the element, including any child nodes
    Element dup = (Element) element.cloneNode(true);

    // Insert the copy immediately after the cloned element
    element.getParentNode().insertBefore(dup, element.getNextSibling());
  }
}

Related Tutorials