
- Java XML Home
- Java XML Overview
- Java XML Parsers
- Java DOM Parser
- Java DOM Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java SAX Parser
- Java SAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- JDOM XML Parser
- JDOM XML Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java StAX Parser
- Java StAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XPath Parser
- Java XPath Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java DOM4J Parser
- Java DOM4J Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XML Useful Resources
- Java XML - Questions and Answers
- Java XML - Quick Guide
- Java XML - Useful Resources
- Java XML - Discussion
Java JDOM Element getContent() Method
The Java JDOM getContent() method of Element class is used to get the content objects inside the scope of an XML Element. Content objects include Element, Text, Comment, ProcessingInstruction, CDATA and EntityRef.
The getContent() method can also be used to retrieve only specific type of content such as only elements, only comments etc. This method can be used to get the content object at a specified index. It returns a live list and hence any modifications made to content objects affect the original content list.
Syntax
Following is the syntax of the Java JDOM Element getContent() method −
Element.getContent(); Element.getContent(index); Element.getContent(filter);
Parameters
The Java getContent() method is a polymorphic method and it accepts the following parameters −
- index − represents the index of the content object.
- filter − represents the filter object to filter elements, comments etc.
Return Value
The Java getContent() method returns the list of content objects.
Example 1
We need to parse the following library.xml file −
<library> Readers choice <!--Book information is available here --> <book>War and peace</book> <book>Random thoughts</book> </library>
Here is the basic example of using the Java JDOM Element getContent() method −
import java.io.File; import java.util.List; import org.jdom2.Content; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; public class GetContent { public static void main(String args[]) { try { //Reading the document and get the root SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("library.xml"); Document doc = saxBuilder.build(inputFile); Element root = doc.getRootElement(); //Get content List<Content> contentList = root.getContent(); for(Content con : contentList) { System.out.println(con); } } catch(Exception e) { e.printStackTrace(); } } }
The list of content objects of the root element is displayed.
[Text: Readers choice ] [Comment: <!--Book information is available here -->] [Text: ] [Element: <book/>] [Text: ] [Element: <book/>] [Text: ]
Example 2
The getContent() method retrieves content object at a specified index by passing the index as a parameter to it.
import java.io.File; import org.jdom2.Content; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; public class GetContent { public static void main(String args[]) { try { //Reading the document and get the root SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("library.xml"); Document doc = saxBuilder.build(inputFile); Element root = doc.getRootElement(); //Get content at index 1 Content content = root.getContent(1); System.out.println(content); } catch(Exception e) { e.printStackTrace(); } } }
Content object at index 1 is displayed.
[Comment: <!--Book information is available here -->]
Example 3
The Filters.element() function filters only the Element type of content objects. This Element filter is passed as an argument inside the getContent() method to get 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 and get the root SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("library.xml"); Document doc = saxBuilder.build(inputFile); Element root = doc.getRootElement(); //Get only elements Filter<Element> element_filter = Filters.element(); List<Element> elementList = root.getContent(element_filter); for(Content con : elementList) { System.out.println(con); } } catch(Exception e) { e.printStackTrace(); } } }
Only the elements inside the current element are displayed.
[Element: <book/>] [Element: <book/>]