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!

Getting Attributes And its Value

                         

In this section, you will learn to retrieve the attributes and their value from a XML document using the SAX APIs. 

Description of program:

This program helps you in retrieving attributes and their values.  The parse() method is  invoked by the SAXParser object. Override startElement() method in  SAXHandler class to provide functionality to get the attribute and its value from a xml file. 

Here is the XML File: sample.xml

<Employee-detail >
    <Employee name="Amit" >
        <address>My address</address>
     </Employee>
     <issued-items> 
        <item code="111" type="CD" label=" music" />
        <item code="222" type="DVD" label=" video"/>
     </issued-items>
</Employee-detail>

Here is the Java File: ReadAttributes.java

import java.io.*;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.util.* ;
import org.apache.xerces.impl.*;
import org.apache.xerces.parsers.*;
import org.apache.xerces.jaxp.*;

public class ReadAttributes {
  public static void main(String[] args) {
    try {
      BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter a xml file name: ");
      String xmlFile = bf.readLine();
      File file = new File(xmlFile);
      if (file.exists()){
        //SAX-implementation:
        SAXParserFactory factory = SAXParserFactory.newInstance();
        // Create SAX-parser
        SAXParser parser = factory.newSAXParser();
        System.out.println("Name:\t" "Value:");
        //Define a handler
        SaxHandler handler = new SaxHandler();
        // Parse the xml document
        parser.parse(xmlFile, handler);
      }
      else{
        System.out.println("File not found!");
      }
    
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  private static class SaxHandler extends DefaultHandler {
    public void startElement(String uri, String localName, String qName, 
                    Attributes attrs
throws SAXParseException,SAXException {
      int length = attrs.getLength();
      //Each attribute
      for (int i=0; i<length; i++) {
        // Get names and values to each attribute
        String name = attrs.getQName(i);
        System.out.print(name);
                String value = attrs.getValue(i);
        System.out.println("\t"+value);
      }
    }
  }
}

Download this example.

Output of this program:

C:\vinod\xml\sax1>javac ReadAttributes.java

C:\vinod\xml\sax1>java ReadAttributes
Enter a xml file name: sample.xml
Name:    Value:
name      Amit
code      111
type       CD
label       music
code      222
type       DVD
label      video

                         

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 
  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.