Copying a Subtree of Nodes in a DOM Document

// Obtain an element; the following method is implemented in // The Quintessential Program to Create a DOM Document from an XML File Document doc = parseXmlFile("infilename.xml", false); 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());
This is the sample input for the example:
<root> <entry attr="value"> a<i>b</i>c </entry> </root>
This is the resulting XML:
<?xml version="1.0" encoding="UTF-8"?> <root> <entry attr="value"> a<i>b</i>c </entry><entry attr="value"> a<i>b</i>c </entry> </root>

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.