Header javaperspective.com
JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.12. Working with comments

3.12. Working with comments
Last updated: 25 January 2013.

This tutorial will show you how to use comments with the JDOM library.

So far, there were no comments in the XML examples that you have seen in the previous tutorials but XML documents may contain comments. A comment starts with <!-- and ends with -->. In the JDOM API, a comment is an instance of the class Comment. To add a comment to an element, just call the method addContent provided by the class Element. Here is an example:

import org.jdom2.Comment;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;


public final class JDOMDemo {


   
private void createXmlWithComment(){
         
try {
               
// Create the root element
               
Element rootElement = new Element("HOTEL");

               
// Create a new ROOM element
               
Element room = new Element("ROOM");

               
//################################################
                // Create a comment and add it to ROOM
               
Comment comment = new Comment("This is a comment");
                room.addContent
(comment);
               
//################################################

                // Create a new NUMBER element and add it to ROOM
               
Element number = new Element("NUMBER");
                number.addContent
("17");
                room.addContent
(number);

               
// Create a CATEGORY element and add it to ROOM
               
Element category = new Element("CATEGORY");
                category.addContent
("STANDARD");
                room.addContent
(category);

               
// Create a BED element and add it to ROOM
               
Element bed = new Element("BED");
                bed.addContent
("DOUBLE SIZE");
                room.addContent
(bed);

               
// Create a FLOOR element and add it to ROOM
               
Element floor = new Element("FLOOR");
                floor.addContent
("5");
                room.addContent
(floor);

               
// Create an AIR_CONDITIONING element and add it to ROOM
               
Element airConditioning = new Element("AIR_CONDITIONING");
                airConditioning.addContent
(new Element("TABLE_FANS").addContent("2"));
                room.addContent
(airConditioning);

               
// Add the newly created ROOM element to the root element
               
rootElement.addContent(room);

               
// Create the document with the root element
               
Document document = new Document(rootElement);

               
// 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().createXmlWithComment();
   
}

}

The output is the following:

   <?xml version="1.0" encoding="UTF-8"?>
   <HOTEL>
            <ROOM>
                     <!--This is a comment-->
                     <NUMBER>17</NUMBER>
                     <CATEGORY>STANDARD</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <FLOOR>5</FLOOR>
                     <AIR_CONDITIONING>
                              <TABLE_FANS>2</TABLE_FANS>
                     </AIR_CONDITIONING>
            </ROOM>
   </HOTEL>

Instead of adding comments to elements within the XML document, you may want to add a comment at the beginning of the document (between the prolog and the root element) or at the end of the document (after the root element's closing tag). To add a comment at the beginning or end, you need to call the method getContent provided by the class Document. That call returns a list of Content objects which represents all the content of the document. Because changes in that list affect the document, all you need to do is add a Comment object at the beginning or end of that list. The code below creates an XML document and adds a comment at the beginning and a comment at the end:

import java.util.List;

import org.jdom2.Comment;
import org.jdom2.Content;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;


public final class JDOMDemo {


   
private void createXmlWithCommentsAtTheBeginningAndAtTheEnd(){
         
try {
               
// Create the root element
               
Element rootElement = new Element("HOTEL");

               
// Create a new ROOM element
               
Element room = new Element("ROOM");

               
// Create a new NUMBER element and add it to ROOM
               
Element number = new Element("NUMBER");
                number.addContent
("17");
                room.addContent
(number);

               
// Create a CATEGORY element and add it to ROOM
               
Element category = new Element("CATEGORY");
                category.addContent
("STANDARD");
                room.addContent
(category);

               
// Create a BED element and add it to ROOM
               
Element bed = new Element("BED");
                bed.addContent
("DOUBLE SIZE");
                room.addContent
(bed);

               
// Create a FLOOR element and add it to ROOM
               
Element floor = new Element("FLOOR");
                floor.addContent
("5");
                room.addContent
(floor);

               
// Create an AIR_CONDITIONING element and add it to ROOM
               
Element airConditioning = new Element("AIR_CONDITIONING");
                airConditioning.addContent
(new Element("TABLE_FANS").addContent("2"));
                room.addContent
(airConditioning);

               
// Add the newly created ROOM element to the root element
               
rootElement.addContent(room);

               
// Create the document with the root element
               
Document document = new Document(rootElement);

               
//###############################################################
                // Get the document's content list
               
List<Content> content = document.getContent();

               
// Add a comment at the beginning of the document
               
content.add(0, new Comment("This is the comment at the beginning"));

               
// Add a comment at the end of the document
               
content.add(new Comment("This is the comment at the end"));
               
//###############################################################

                // 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().createXmlWithCommentsAtTheBeginningAndAtTheEnd();
   
}

}

Here is the output:

   <?xml version="1.0" encoding="UTF-8"?>
   <!--This is the comment at the beginning-->
   <HOTEL>
            <ROOM>
                     <NUMBER>17</NUMBER>
                     <CATEGORY>STANDARD</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <FLOOR>5</FLOOR>
                     <AIR_CONDITIONING>
                              <TABLE_FANS>2</TABLE_FANS>
                     </AIR_CONDITIONING>
            </ROOM>
   </HOTEL>
   <!--This is the comment at the end-->


You are here :  JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.12. Working with comments
Next tutorial :  JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.13. Working with DTDs

Copyright © 2013. 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