Skip to main content

Parsing an XML document using SAX Parser in Java

This is again a "getting started tutorial" of how to start using XML parsing concepts in java. For further details of using XML parsing please refer tutorials at : http://docs.oracle.com/javase/tutorial/jaxp/sax/index.html.

The program presented here is a simple demonstration of how to parse an XML document using SAX-XML parser for java. For demonstration purpose a sample .xml file is also included.

temp.xml:

<?xml version="1.0"?>
<company>
    <staff department="information security">
        <firstname>FIRST NAME-1</firstname>
        <lastname>LAST NAME-1</lastname>
        <nickname>STUDENT-1</nickname>
        <salary>100000</salary>
    </staff>
    <staff>
        <firstname>FIRST NAME-2</firstname>
        <lastname>LAST NAME-2</lastname>
        <nickname>STUDENT-2</nickname>
        <salary>200000</salary>
    </staff>
</company>


Temp.java :

import java.util.Hashtable;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class Temp extends DefaultHandler{   
    private Hashtable tags;
    String tempVal=null;
   
    public static void main(String a[])throws Exception
    {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser saxParser=spf.newSAXParser();
        XMLReader reader=saxParser.getXMLReader();
        reader.setContentHandler(new Temp());
        reader.parse("temp.xml");       
    }
    public void startDocument()throws SAXException
    {
        tags=new Hashtable();
        System.out.println("------startDocument()--------");
    }
   
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
    {
        System.out.println("------startElement() -------");
        System.out.println("namespaceURI="+namespaceURI);
        System.out.println("localname="+localName);
        System.out.println("qName="+qName);
        int attributes=atts.getLength();
        //Iterator iterator=atts.
        for(int i=0; i<attributes; i++)
        {
            System.out.println("attribute name="+atts.getLocalName(i));
            System.out.println("attribute value="+atts.getValue(i));
           
        }
    }
   
    public void characters(char[] ch, int start, int length) throws SAXException {
        tempVal = new String(ch,start,length);
        System.out.println("characters()/ Tag content value="+tempVal);
    }
   
    public void endElement(String uri, String localName,
            String qName) throws SAXException {
        System.out.println("---------endELement()-------");
        System.out.println("uri="+uri);
        System.out.println("localName="+localName);
        System.out.println("qName="+qName);
    }
   

}

Comments

Popular posts from this blog

Uploading a binary file to RESTful web service in java using Jersey

Hi, Recently I had a situation where I was supposed to upload a binary file to a server side RESTful web service created in Jersey. I faced a lot of trouble in doing so because always one or the other part of the hood was not working properly. The following post gives a step by step process as how to create a RESTful service that accepts uploaded binary files. Pre-requisites: 1. A running RESTful service based on Jersey. 2. An HTML page with file upload component. 3. Tomcat 7 as web server. 4. Eclipse indigo. Note: Let us suppose that tomcat is running on same machine on which the HTML client is present. So we will be using the base domain URL as ' http://localhost:8080/ '. If your service and client are located on different instances of tomcat then replace the localhost part of the URL with the IP address of the machine on which the REST service is running. Step 1 (Eclipse setup): a. Create a 'Dynamic Web Project' in eclipse indigo. Let us say that thi

Using R Programming Language inside Java

Hi, This tutorial walks you through using R programming language inside a java program. R is a powerful programming language for Machine Learning and Data Mining. It contains implementation of various Machine Learning algorithms. Recently i had a situation where i was supposed to use these machine learning algorithms inside my java program. I searched the web but the stuff i found was not of much help. Thus i thought of putting the same in the form of this tutorial. This tutorial is not intended to teach you R language. It is only to help you integrate R to Java and then to use R functions inside a Java program. Pre-requisite: Following things are needed to be pre-configured on your system to use R in Java program: 1. R workbench:  R has got a console known as RGui where R commands/programs could be executed. To install RGui simply go to  http://cran.r-project.org/bin/windows/base/  and download the ' Download R 3.0.2 for Windows   (52 megabytes, 32/64 bit) '. Simply

Creating a JAX-WS Web Service and Client using Eclipse Indigo

Creating a JAX-WS Web Service and corresponding client is a very trivial task. However I faced various problems while doing the same mainly while creating a client for the web service. This was mainly because the contents I found on the Internet were in a distributed manner. So I thought of creating a tutorial which binds all the concepts at one place. So this tutorial does not teaches indepth concepts of web services. Instead it guides as how to create a Web Service and a seperate client. Software Requirements: 1. Eclipse Indigo. Note: To download Eclipse Indigo follow the link http://www.eclipse.org/downloads/. Then select 'Eclipse IDE for Java EE Developers'. Process: We will proceed with the creation of web service and client in following two steps: 1. Creating Web Service 2. Creating client to consume web service. 1. Creating Web Service: Creating of a JAX-WS web service is not a very tough task. We simply create a class(normal Java class) and define certai