- 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 Document getDescendants() Method
The Java JDOM getDescendants() method of Document class retrieves all the descendants of an XML document. We can filter the descendants to get only elements, only comments, comments or elements, elements with a specific name or prefix.
This method returns an IteratorIterable object which has the list of all descendants in the form of Content objects. We can iterate through the IteratorIterable object to access each Content object. When filtering is used on this method, it returns the iterator of the specified filter. For example, if Element filter is used, it returns the iterator of Element objects.
Syntax
Following is the syntax of the Java JDOM Document getDescendants() method −
Document.getDescendants() Document.getDescendants(filter)
Parameters
The Java JDOM Document getDescendants() method doesn't accept any parameter or accepts a single parameter.
filter − This represents a filter object.
Return Value
The Java getDescendants() method returns an iterator of all descendants in an XML document.
Example 1
Let us apply the Java JDOM Document getDescendants() method on the following collegeData.xml file −
<?xml version="1.0" encoding="UTF-8"?>
<!-- Information of xyz college (last updated 24-07-2024) -->
<college>
<!-- All the three departments are listed -->
<department id="101">
<!-- Here, name represents the name of the department -->
<name>Computer Science</name>
<staffCount>20</staffCount>
</department>
<department id="102">
<name>Electrical and Electronics</name>
<staffCount>23</staffCount>
</department>
<department id="103">
<name>Mechanical</name>
<staffCount>15</staffCount>
</department>
</college>
The following getDescendants.java program uses getDescendants() method of Document class on collegeData.xml document and retrieves all the descendants.
import java.io.File;
import java.util.Iterator;
import org.jdom2.Content;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
public class getDescendants {
public static void main(String args[]) {
try {
//Creating a SAXBuilder Object
SAXBuilder saxBuilder = new SAXBuilder();
//Reading the XML
File inputFile = new File("collegeData.xml");
//Parsing the XML Document
Document doc = saxBuilder.build(inputFile);
//Getting the descendants
Iterator<Content> list = doc.getDescendants();
//Iterating through the list
while(list.hasNext())
System.out.println(list.next());
} catch(Exception e) {
e.printStackTrace();
}
}
}
The output window displays all the descendants (Elements, text content and comments).
[Comment: <!-- Information of xyz college (last updated 24-07-2024) -->]
[Element: <college/>]
[Text:
]
[Comment: <!-- All the three departments are listed -->]
[Text:
]
[Element: <department/>]
[Text:
]
[Comment: <!-- Here, name represents the name of the department -->]
[Text:
]
[Element: <name/>]
[Text: Computer Science]
[Text:
]
[Element: <staffCount/>]
[Text: 20]
[Text:
]
[Text:
]
[Element: <department/>]
[Text:
]
[Element: <name/>]
[Text: Electrical and Electronics]
[Text:
]
[Element: <staffCount/>]
[Text: 23]
[Text:
]
[Text:
]
[Element: <department/>]
[Text:
]
[Element: <name/>]
[Text: Mechanical]
[Text:
]
[Element: <staffCount/>]
[Text: 15]
[Text:
]
[Text:
]
Example 2
The Filters.elements() method returns the data that matches with the Element. Using this method, an Element filter is obtained and passed as an argument to getDescendants(Filter<Element>) method. It filters only the Elements from the descendants and returns an iterator of Element objects.
In the following example, we parsed the collegeData.xml file and printed the elements by traversing through the iterator using a while loop.
import java.io.File;
import java.util.Iterator;
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 getDescElements {
public static void main(String args[]) {
try {
//Creating the document from XML file
SAXBuilder saxBuilder = new SAXBuilder();
File inputFile = new File("collegeData.xml");
Document doc = saxBuilder.build(inputFile);
//Getting the descendants of Element type only
Filter<Element> element_filter = Filters.element();
Iterator<Element> list = doc.getDescendants(element_filter);
//Iterating through the list
while(list.hasNext())
System.out.println(list.next());
} catch(Exception e) {
e.printStackTrace();
}
}
}
The output window displays all the element names of the XML document.
[Element: <college/>] [Element: <department/>] [Element: <name/>] [Element: <staffCount/>] [Element: <department/>] [Element: <name/>] [Element: <staffCount/>] [Element: <department/>] [Element: <name/>] [Element: <staffCount/>]
Example 3
The ElementFilter object is created by passing the name of the element as a String to its constructor. The obtained ElementFilter object is passed as an argument to the getDescendants(ElementFilter) method. It filters only the elements with the element name specified by the ElementFilter.
The following example uses the above methods to get the Elements with a specific name, 'staffCount'. The obtained Element iterator is iterated using a while loop and printed the elements.
import java.io.File;
import java.util.Iterator;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.filter.ElementFilter;
import org.jdom2.input.SAXBuilder;
public class getDescSpecificElements {
public static void main(String args[]) {
try {
//Creating the document from XML file
SAXBuilder saxBuilder = new SAXBuilder();
File inputFile = new File("collegeData.xml");
Document doc = saxBuilder.build(inputFile);
//Getting the descendants of Element with specific name
ElementFilter filter = new ElementFilter("staffCount");
Iterator<Element> list = doc.getDescendants(filter);
while(list.hasNext())
System.out.println(list.next());
} catch(Exception e) {
e.printStackTrace();
}
}
}
The output window displays all the elements with name, 'staffCount'.
[Element: <staffCount/>] [Element: <staffCount/>] [Element: <staffCount/>]
Example 4
The Filters.comments() method returns the data that matches with the Comment. Using this method, a Comment filter is obtained and passed as an argument to getDescendants(Filter<Comment>) method. It filters only the comments from the descendants and returns an iterator of Comment objects.
The above methods are used in the following program to obtain all the Comment objects as an Iterator. The obtained iterator is iterated using a while loop to print them on the console.
import java.io.File;
import java.util.Iterator;
import org.jdom2.Comment;
import org.jdom2.Document;
import org.jdom2.filter.Filter;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
public class getDescComments {
public static void main(String args[]) {
try {
//Creating the document from XML file
SAXBuilder saxBuilder = new SAXBuilder();
File inputFile = new File("collegeData.xml");
Document doc = saxBuilder.build(inputFile);
//Getting the descendants of Comment type only
Filter<Comment> comment_filter = Filters.comment();
Iterator<Comment> list = doc.getDescendants(comment_filter);
//Iterating through the list
while(list.hasNext())
System.out.println(list.next());
} catch(Exception e) {
e.printStackTrace();
}
}
}
The output window displays all the comments inside the document.
[Comment: <!-- Information of xyz college (last updated 24-07-2024) -->] [Comment: <!-- All the three departments are listed -->] [Comment: <!-- Here, name represents the name of the department -->]