Home Tutorial Xml Jdom JDOM Element example, How to get root element of xml file.

 
 

JDOM Element example, How to get root element of xml file.
Posted on: August 18, 2010 at 12:00 AM
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

Related Tags for JDOM Element example, How to get root element of xml file.:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.