
- 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 isAncestor() Method
The Java JDOM isAncestor() method of Element class is used to check if the current XML element is an ancestor of another XML element. If it is an ancestor, it returns true, else it returns false.
Syntax
Following is the syntax of the Java JDOM Element isAncestor() method −
Element.isAncestor(element);
Parameters
The Java Element.isAncestor() method accepts a single parameter −
element − represents an element object to check against the current Element.
Return Value
The Java isAncestor() method returns a boolean value; true if it is ancestor, false otherwise.
Example 1
Here is the basic example of using the Java JDOM Element isAncestor() method −
import org.jdom2.Element; public class CheckAncestor { public static void main(String args[]) { try { Element root = new Element("root"); Element child = new Element("child"); Element grandChild = new Element("grandChild"); child.addContent(grandChild); root.addContent(child); //check if the root is ancestor System.out.println(root.isAncestor(grandChild)); } catch (Exception e) { e.printStackTrace(); } } }
The output window displays the boolean value.
true
Example 2
We need to parse the following vehicle.xml file −
<vehicles> <twoWheeler> <bike>Pulsar</bike> <scooty>Activa</scooty> </twoWheeler> <threeWheeler></threeWheeler> <fourWheeler></fourWheeler> </vehicles>
In the following example, a child element of twoWheeler is passed as an argument to isAncestor() method to check against fourWheeler element.
import java.io.File; import java.util.List; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; public class CheckAncestor { public static void main(String args[]) { try { //Reading the document SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("vehicle.xml"); Document doc = saxBuilder.build(inputFile); //Get the root element Element root = doc.getRootElement(); List<Element> elementList = root.getChildren("twoWheeler"); Element ele = elementList.get(0); Element fourWheeler = root.getChild("fourWheeler"); //check if fourWheeler is ancestor System.out.println(fourWheeler.isAncestor(ele)); } catch (Exception e) { e.printStackTrace(); } } }
The output window displays the boolean value.
false