JDOM Element example, How to get root element of xml file.


 

JDOM Element example, How to get root element of xml file.

In this tutorial, you will see how to get root element of xml file.

In this tutorial, you will see how to get root element of xml file.

JDOM Element example, How to get root element of xml file.

In this tutorial, we will see how to get root element of xml file.

Code.

Student.xml

<?xml version="1.0"?>
<Student>
  <name id="1"  >
    <first id="one" >Bharat</first>
    <last>Singh</last>
    <age>24</age>
  </name>
  <name  id="2">
    <first id="two" >Amar</first>
    <last>Sagar</last>
    <age>25</age>
  </name>
  <name id="3" >
    <first id="three">Arun</first>
    <last>Bist</last>
    <age>26</age>
  </name>
</Student>

RootName.java

import java.io.File;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.DOMBuilder;
public class RootName {
  public static void main(String[] ar) {
    try {
      File XmlFile = new File("Student.xml");
      DOMBuilder db = new DOMBuilder(false);
      Document dc = db.build(XmlFile);
      Element e = dc.getRootElement();
      System.out.println("Root " + e);      
    catch (Exception e) {
      System.out.println("Exception : " + e.getMessage());
    }
  }
}

Output.

Root [Element: <Student/>]

Download this code

Ads