Share on Google+Share on Google+

Getting The XML Root Element

After reading this section, you will be able to retrieve a root element from the XML document.

Getting The XML Root Element


After reading this section, you will be able to retrieve a  root element from the XML document.  The JAXP (Java APIs for XML Processing) provides a common interface for creating and using xml files using the standard SAX, DOM and XSLTs. Here you will see the given example to use DOM interface. 

Description of program:

You need a XML document (file). Both Java and the XML file are kept in the same directory. This program takes a XML file as a String at the console . If the given file exists then it parses the document using parse() method . Before parsing the XML document you need a DocumentBuilder object. For creating this first of all you create a DocumentBuilderFactory. After parsing the XML document you get  the node element using getDocumentElement() method. To get the root element use the getNodeName() method.  

Here is the video insturction " How to get root node of XML in Java?":

Here is the XML File: Employee-Detail.xml

<?xml version = "1.0" ?>

<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>

<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>

<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>


Here is the Java File:

import org.w3c.dom.*;
import javax.xml.parsers.*;

public class GetRootNode{
  public static void main(String[] args) {
  BufferedReader bf = new BufferedReader(
new InputStreamReader(;
  System.out.print("Enter xml file name: ");
  String str = bf.readLine();
  File file = new File(str);
  if (file.exists()){
  DocumentBuilderFactory fact = 
  DocumentBuilder builder = fact.newDocumentBuilder();
  Document doc = builder.parse(str);
  Node node = doc.getDocumentElement();
  String root = node.getNodeName();
  System.out.println("Root Node: " + root);
  System.out.println("File not found!");
  catch(Exception e){}

Download this example.

Output of the program:


C:\vinod\xml>java GetRootNode
Enter xml file name: Employee-Detail.xml
Root Node: Employee-Detail


Download source code of the project in Eclipse Project format



Posted on: June 2, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.

Discuss: Getting The XML Root Element   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
May 29, 2011
Getting The XML Root Element - DOM

When I do this in a different class the IDE(netbeans) ask me to report a Exception, it require me a SAXException. How come this posible if we are using DOM and should do a DOMException instead. Here is the code:
June 1, 2012
software engineering

i am u give me these good help,but what i mean is how to retrieve specific column from xml file and display the data in a jlist .