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

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 XML Elements

                         

In this section you will learn to develop a simple java program to get the names of all elements contained in the XML document .

Description of the program:

The following program teaches to parse the elements and retrieve  their names, from a  XML document.                                                                              

Create a java file to retrieve the starting elements. When you run this program it asks for a XML document. This is used as a command line argument. If  you don't enter any file, the program throws an exception displaying "java.lang.ArrayIndexOutOfBoundsException: 0 at XMLElements.main( XML Elements.java:17)". 

The parse() method of SAXParser class reads the contents. The SAXParserFactory  is a factory API that enables applications to configure and obtain a SAX  parser to parse XML documents. The startElement() method retrieves all starting elements and prints them on the console. Whenever an error  occurs  it throws the SAXException.  

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: XMLElements.java

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

public class XMLElements{
  public static void main(String[] args) {
    try{
      SAXParserFactory parserFact = SAXParserFactory.newInstance();
      SAXParser parser = parserFact.newSAXParser();
      System.out.println("XML Elements: ");
      DefaultHandler handler = new DefaultHandler(){
        public void startElement(String uri, String lName, 
            String ele, Attributes attributes
)throws SAXException{
          //print elements of xml
          System.out.println(ele);
        }
      };
      parser.parse(args[0], handler);
    }
    catch (Exception e){
      e.printStackTrace();
    }
  }
}

Download this example 

Output of program:

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

C:\vinod\xml\comXML>java XMLElements Employee-Detail.xml
XML Elements:
Employee-Detail
Employee
Emp_Id
Emp_Name
Emp_E-mail
Employee
Emp_Id
Emp_Name
Emp_E-mail
Employee
Emp_Id
Emp_Name
Emp_E-mail

                         

Ask programming questions?

 

 

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:

GET XML ELEMENTS code is wrong.

the code should be like this.

package com;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class DemoParser{
public static void main(String[] args)throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter XML file name:");
String xmlFile = bf.readLine();
try{
SAXParserFactory parserFact = SAXParserFactory.newInstance();
SAXParser parser = parserFact.newSAXParser();
System.out.println("XML Elements: ");
DefaultHandler handler = new DefaultHandler(){
public void startElement(String uri, String lName,
String ele, Attributes attributes)throws SAXException{
System.out.println(ele);
}
};
parser.parse(xmlFile, handler);
}
catch (Exception e){
e.printStackTrace();
}
}
}

Posted by sanjay kumar gupta on Monday, 10.8.07 @ 21:52pm | #32519

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
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.

[an error occurred while processing this directive]