Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

Features

Technology
  Tech. Home
  GPS
  OSI
  WiMAX
  VoIP
  WiFi
  HSDPA
  LBS
  Vehicle Tracking
  SCADA
  Tech. What is?
Jobs At RoseIndia.net!
 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

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 (EmployeeDetails.java) 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.
        start:
This is staring position in an array.
        len:
This is the number of characters to read from an array.  

Here is the XML File: Employee-Detail.xml

<?xml version = "1.0" ?>
<Employee-Detail>

<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> Amit2@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> Deepak3@yahoo.com </Emp_E-mail>
</Employee>

</Employee-Detail>

Here is the Java File: EmployeeDetails.java

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;

public class EmployeeDetails{
  public static void main(String[] argsthrows IOException{
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter XML file name:");
    String xmlFile = bf.readLine();
    EmployeeDetails detail = new EmployeeDetails(xmlFile);
  }
  public EmployeeDetails(String str){
    try{
      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 lenthrows 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);
      }
      else{
        System.out.println("File not found!");
      }
    }
    catch (Exception e){
      System.out.println("XML File hasn't any elements");
      e.printStackTrace();
    }
  }
}

Download this example

Output of program:

C:\vinod\xml\comXML>javac EmployeeDetails.java

C:\vinod\xml\comXML>java EmployeeDetails
Enter XML file name:Employee-Detail.xml
XML Data:
Emp_Id: E-001
Name: Vinod
E-mail: Vinod1@yahoo.com
Emp_Id: E-002
Name: Amit
E-mail: Amit2@yahoo.com
Emp_Id: E-003
Name: Deepak
E-mail: Deepak3@yahoo.com

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

i just want to see the example

Posted by umesh on Friday, 02.22.08 @ 12:44pm | #49398

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.