Share on Google+Share on Google+

Get Data From the XML File

Here you will learn to retrieve data from XML file using SAX parser. We use the JAXP APIs to retrieve data from XML document .

Get Data From the XML File


Here you will learn to retrieve data from XML file using SAX parser. We use the JAXP APIs to retrieve data from XML document .

Description of program:

In this example you need a well-formed XML file that has some data (Emp_Id, Emp_Name and Emp_E-mail in our case). Create a java program ( that retrieves data from it. When you run the program it asks for a file with a message "Enter XML file name:" at the command line and checks its existence through exists() method. If the given file exits, the instance of SAXParser class parses the file  using the parse() method.   Till  the startElement() method returns 'true', the characters() method prints data . If  the file doesn't exist it will display a message "File not found!". 

Characters(char[] ch, int start, int len)  method retrieves identification of character data. The Parser calls this method and to report every character data encountered . If any error occurs it throws the SAXException. This method takes the following parameters: 

    ch: This is the characters of XML document.
This is staring position in an array.
This is the number of characters to read from an array.  

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 javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class EmployeeDetails{
  public static void main(String[] argsthrows IOException{
  BufferedReader bf = new BufferedReader(new InputStreamReader(;
  System.out.print("Enter XML file name:");
  String xmlFile = bf.readLine();
  EmployeeDetails detail = new EmployeeDetails(xmlFile);
  public EmployeeDetails(String str){
  File file = new File(str);
  if (file.exists()){
  SAXParserFactory parserFact = SAXParserFactory.newInstance();
  SAXParser parser = parserFact.newSAXParser();
  System.out.println("XML Data: ");
  DefaultHandler dHandler = new DefaultHandler(){
  boolean id;
  boolean name;
  boolean mail;
  public void startElement(String uri, String localName, 
   String element_name, Attributes attributes
)throws SAXException{
  if (element_name.equals("Emp_Id")){
  id = true;
  if (element_name.equals("Emp_Name")){
  name = true;
  if (element_name.equals("Emp_E-mail")){
  mail = true;
  public void characters(char[] ch, int start, int len
throws SAXException{
  String str = new String (ch, start, len);
  if (id){
  System.out.println("Emp_Id: "+str);
  id = false;
  if (name){
  System.out.println("Name: "+str);
  name = false;
  if (mail){
  System.out.println("E-mail: "+str);
  mail = false;
  parser.parse(str, dHandler);
  System.out.println("File not found!");
  catch (Exception e){
  System.out.println("XML File hasn't any elements");

Download this example

Output of program:


C:\vinod\xml\comXML>java EmployeeDetails
Enter XML file name:Employee-Detail.xml
XML Data:
Emp_Id: E-001
Name: Vinod
E-mail: [email protected]
Emp_Id: E-002
Name: Amit
E-mail: [email protected]
Emp_Id: E-003
Name: Deepak
E-mail: [email protected]



Posted on: May 25, 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: Get Data From the XML File   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
August 17, 2011
hard code

coded hard.. you can do better. thanks.
July 26, 2012
How to display data from xml file.

When i run this program,it is not showing the details in xml file.So,why this is happening?Can somebody please help me???
October 23, 2012
xml file parsing in java

how can i get only the first value extracted?