Java JDOM Document getContent() Method



The Java JDOM getContent() method of Document class is used to get the list of all the content of an XML document in the form of Content objects. This list includes comments, root element and DTD declaration.

To filter the content objects to obtain only specific type of content(only elements, only comments etc.), content specific filter is created using Filter class methods and these filters can be passed as arguments to the getContent() method.

Syntax

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

Document.getContent();
Document.getContent(index);
Document.getContent(filter);

Parameters

The getContent() method is a polymorphic method and has the following parameters −

  • index

    represents the index of the content object we want to retrieve.
  • filter

    represents the filter object of Filter class.

Return Value

The Java getContent() method returns a List of Content objects.

Example 1

Here is the bookstore.xml file we need to parse −

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

<!ELEMENT bookstore (book+)>
<!ELEMENT book (name,author,price)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>
<!-- Information of a Bookstore (last updated 05-08-2024) -->
<!-- All the books in the store are listed -->
<bookstore>
   <book>
      <name>Treasure Island</name>
      <author>Robert Louis</author>
      <price>1400</price>
   </book>
   <book>
      <name>Oliver Twist</name>
      <author>Charles Dickens</author>
      <price>2000</price>
   </book>
   <book>
      <name>War and Peace</name>
      <author>Leo Tolstoy</author>
      <price>1500</price>
   </book>
</bookstore>

Here is the basic java program that uses Java JDOM Document getContent() method to get all the content from the above bookstore.xml file.

import java.io.File;
import java.util.List;
import org.jdom2.Content;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;

public class GetContent {
   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);		 
 		 //Getting the content
	     List<Content> contentList = doc.getContent();
	     //Printing the list items
	     for(Content c : contentList) {
	    	System.out.println(c);
	     } 	           
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

All the content objects of the XML document are displayed.

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

Example 2

To get the content object at a particular index, we pass an integer index to the getContent() method. If the index is not in the range of content size, it throws IndexOutOfBoundsException.

The following example retrieves the content at 0th index −

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

public class GetContent {
   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);		 
 		 //Getting the content
	     Content content = doc.getContent(0);
	     System.out.println(content); 	           
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The DTD declaration is the first content object of the document; hence it is printed.

[DocType: <!DOCTYPE bookstore>]

Example 3

The element() method of Filters class returns a Filter list of Element objects. This Element filter is passed as an argument to getContent() method to filter only elements from the content inside the document.

In the following program, we filtered only the Element objects.

import java.io.File;
import java.util.List;
import org.jdom2.Content;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.filter.Filter;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;

public class GetContent {
   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);
 		 Filter<Element> element_filter = Filters.element();
 		 //Getting the content
	     List<Element> contentList = doc.getContent(element_filter);
	     //Printing the list items
	     for(Content c : contentList) {
	    	System.out.println(c);
	     } 	           
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

There is only one root element; hence it is printed.

[Element: <bookstore/>]

Example 4

The comment() method of Filters object returns a Filter list if Comment objects. This Comment Filter is passed as an argument to the getContent() method to obtain only the Comment objects.

Here is the Java program that uses getContent() method to filter only the comments −

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

public class GetContent {
   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);
 		 Filter<Comment> comment_filter = Filters.comment();
 		 //Getting the content
	     List<Comment> contentList = doc.getContent(comment_filter);
	     //Printing the list items
	     for(Content c : contentList) {
	    	System.out.println(c);
	     } 	           
      } catch (Exception e) {
    	 e.printStackTrace();
      }
   }
}

The two comments of the bookstore.xml file are printed on the console.

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