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

 
 

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

Advertisement

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


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 18, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com