Java JDOM Document removeContent() Method



The Java JDOM removeContent() method of Document class removes the document level content objects from an XML document. Using this method, we can remove comments, DocType declaration and root element from an XML document. This method throws indexOutOfBoundsException when we try to remove the content object with index that is not in the range of number of content objects inside the document.

The removeContent() method can be used to remove specific type of content objects, for example, removing all comments from the document. This method is not used to remove child level elements contained inside the root element. It removes the entire root element along with the content (child elements, comments) inside it.

To remove child elements inside the root element, we can use removeChild(), removeChildren() and removeContent() methods of Element class of JDOM.

Syntax

Following is the syntax of the Java JDOM Document removeContent() method −

Document.removeContent()
Document.removeContent(index)
Document.removeContent(child)
Document.removeContent(filter)

Parameters

The Java removeContent() method is a polymorphic method and has different parameters for each of them.

  • child − represents a Content object that we want to remove. This can be an Element, Comment, DocType, EntityRef, ProcessingInstruction and Text.

  • index − represents the index of the Content object we need to remove.

  • filter − represents the filter object (Element filter, comment filter etc.) of specific type of content.

Return Value

This method returns a list of removed Content objects.

Example 1

The following bookstore.xml file is used in all the examples to understand the functionality of Java JDOM removeContent() method.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bookstore[

<!ELEMENT bookstore (book+)>
<!ELEMENT book (#PCDATA)>
]>
<!-- Information of a Bookstore (last updated 05-08-2024) -->
<!-- All the books in the store are listed -->
<bookstore>
   <book>Treasure Island</book>
   <book>Oliver Twist</book>
</bookstore>

The removeContent() method, when no arguments are passed, removes the entire content from the XML document by just leaving only the processing instruction.

import java.io.File;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class RemoveContent {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
 		 SAXBuilder saxBuilder = new SAXBuilder();
 		 File inputFile = new File("bookstore.xml");
 		 Document doc = saxBuilder.build(inputFile);
 		 //Printing the document 
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     System.out.println("-------XML Document BEFORE removing the content-------\n");
         xmlOutput.output(doc, System.out);
         //Removing content
	     doc.removeContent();
	     System.out.println("\n-------XML Document AFTER removing the content-------\n");
         xmlOutput.output(doc, System.out);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

Content of XML document before and after removing the content is displayed.

-------XML Document BEFORE removing the content-------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bookstore>
<!-- Information of a Bookstore (last updated 05-08-2024) -->
<!-- All the books in the store are listed -->
<bookstore>
  <book>Treasure Island</book>
  <book>Oliver Twist</book>
</bookstore>

-------XML Document AFTER removing the content-------

<?xml version="1.0" encoding="UTF-8"?>

Example 2

To remove Content object at a specified index, we can pass the index as an argument to removeContent() method as follows −

import java.io.File;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;

public class RemoveContent {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
 		 SAXBuilder saxBuilder = new SAXBuilder();
 		 File inputFile = new File("bookstore.xml");
 		 Document doc = saxBuilder.build(inputFile);		 
 		 //Removing Content object
	     doc.removeContent(2);
	     System.out.println("Content object at index 2 is removed.");
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays a simple print statement that the content is removed.

Content object at index 2 is removed.

Example 3

We can remove a specific content object from the XML document by passing the content object as an argument to the removeContent() method.

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

public class RemoveContent {
   public static void main(String args[]) {
      try {	
    	 //Create a new Document and add root
	     Document doc = new Document();
	     Element root = new Element("company").setText("xyz");
	     Comment comment = new Comment("This is the company data.");
	     doc.addContent(comment);
	     doc.addContent(root);
	     //Print document
	     XMLOutputter xmlOutput = new XMLOutputter();
	     xmlOutput.setFormat(Format.getPrettyFormat());
	     System.out.println("-------XML Document after adding content-------\n");
         xmlOutput.output(doc, System.out);
         //remove comment
         doc.removeContent(comment);
         System.out.println("\n-------XML Document after removing the comment-------\n");
         xmlOutput.output(doc, System.out);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the XML document before and after removing the Comment object.

-------XML Document after adding content-------

<?xml version="1.0" encoding="UTF-8"?>
<!--This is the company data.-->
<company>xyz</company>

-------XML Document after removing the comment-------

<?xml version="1.0" encoding="UTF-8"?>
<company>xyz</company>

Example 4

The comment() method of Filters class returns the filter of Comment objects that can be passed as an argument to the removeContent() method to remove only the comments from the document. Similarly, Element filter can be used to remove root element.

import org.jdom2.Comment;
import org.jdom2.filter.Filter;
import org.jdom2.filter.Filters;
import java.io.File;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;

public class RemoveComments {
   public static void main(String args[]) {
      try {	
    	 //Reading the document
 		 SAXBuilder saxBuilder = new SAXBuilder();
 		 File inputFile = new File("bookstore.xml");
 		 Document doc = saxBuilder.build(inputFile);		 
 		 //Removing comments
 		 Filter<Comment> comment_filter = Filters.comment();
	     List<Comment> commentList = doc.removeContent(comment_filter);
	     //Printing the removed comments
	     System.out.println("Removed Comments: \n");
	     for(Comment c : commentList) {
	    	System.out.println(c);
	     }
	     XMLOutputter xmlOutput = new XMLOutputter();
	     System.out.println("\n-------XML Document after removing comments-------\n");
         xmlOutput.output(doc, System.out);
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The output window displays the XML document before and after removing the comments.

Removed Comments: 

[Comment: <!-- Information of a Bookstore (last updated 05-08-2024) -->]
[Comment: <!-- All the books in the store are listed -->]

-------XML Document after removing comments-------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bookstore><bookstore>
   <book>Treasure Island</book>
   <book>Oliver Twist</book>
</bookstore>
Advertisements