Header javaperspective.com
JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.10. Duplicating nodes

3.10. Duplicating nodes
Last updated: 25 January 2013.

This tutorial explains how to duplicate nodes in a JDOM document.

To duplicate a node, just call one of the following methods provided by the class Element: For example, suppose that you want to duplicate the first ROOM node contained in the file hotel.xml shown below:

   <?xml version="1.0"?>
   <HOTEL>
            <ROOM>
                     <NUMBER>17</NUMBER>
                     <CATEGORY>STANDARD</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <FLOOR>5</FLOOR>
                     <LIVING_ROOM/>
                     <KITCHENETTE/>
                     <AIR_CONDITIONING>
                              <TABLE_FANS>2</TABLE_FANS>
                     </AIR_CONDITIONING>
            </ROOM>
            <ROOM>
                     <NUMBER>33</NUMBER>
                     <CATEGORY>SUITE</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <BED>KING SIZE</BED>
                     <FLOOR>9</FLOOR>
                     <LIVING_ROOM>17 FEET * 13 FEET</LIVING_ROOM>
                     <KITCHENETTE>10 FEET * 8 FEET</KITCHENETTE>
                     <JACUZZI>3 SEATS</JACUZZI>
                     <AIR_CONDITIONING>
                              <AIR_CONDITIONERS>5</AIR_CONDITIONERS>
                     </AIR_CONDITIONING>
            </ROOM>
   </HOTEL>

Here is the code that duplicates the first ROOM node:

import java.io.File;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;


public final class JDOMDemo {


   
private void duplicateElement(){
         
try {
               
// Read the XML file and get a JDOM document
               
SAXBuilder builder = new SAXBuilder();
                Document document = builder.build
(new File("hotel.xml"));

               
// Get the root element
               
Element rootElement = document.getRootElement();

               
// Get the first ROOM element
               
Element firstRoomElement = rootElement.getChildren().get(0);

               
// Duplicate the first ROOM element and add it to the root element
               
rootElement.addContent(firstRoomElement.clone());

               
// Output the JDOM document to the standard output
               
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
                outputter.output
(document, System.out);
         
}
         
catch (Exception e) {
               
e.printStackTrace();
         
}
    }


   
public static void main(String[] args){
         
new JDOMDemo().duplicateElement();
   
}

}

The output is:

   <?xml version="1.0"?>
   <HOTEL>
            <ROOM>
                     <NUMBER>17</NUMBER>
                     <CATEGORY>STANDARD</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <FLOOR>5</FLOOR>
                     <LIVING_ROOM/>
                     <KITCHENETTE/>
                     <AIR_CONDITIONING>
                              <TABLE_FANS>2</TABLE_FANS>
                     </AIR_CONDITIONING>
                     <JACUZZI>3 SEATS</JACUZZI>
            </ROOM>
            <ROOM>
                     <NUMBER>33</NUMBER>
                     <CATEGORY>SUITE</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <BED>KING SIZE</BED>
                     <FLOOR>9</FLOOR>
                     <LIVING_ROOM>17 FEET * 13 FEET</LIVING_ROOM>
                     <KITCHENETTE>10 FEET * 8 FEET</KITCHENETTE>
                     <AIR_CONDITIONING>
                              <AIR_CONDITIONERS>5</AIR_CONDITIONERS>
                     </AIR_CONDITIONING>
            </ROOM>
            <ROOM>
                     <NUMBER>17</NUMBER>
                     <CATEGORY>STANDARD</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <FLOOR>5</FLOOR>
                     <LIVING_ROOM/>
                     <KITCHENETTE/>
                     <AIR_CONDITIONING>
                              <TABLE_FANS>2</TABLE_FANS>
                     </AIR_CONDITIONING>
                     <JACUZZI>3 SEATS</JACUZZI>
            </ROOM>
   </HOTEL>


You are here :  JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.10. Duplicating nodes
Next tutorial :  JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.11. Working with attributes

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7