FoodForFutureGeeks

Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Sunday, 28 April 2013

SAX Parser In JAVA

The following code demonstrates the use of sax parser in JAVA :

main function wherein you initialise the parser and call for parsing the file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/****************************
 * author: Aniruddha SN
 *
 */
 
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import customHandler.anrSaxHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
 
public class XMLParser
{
    public static void main(String args[])
        {
                try
                {
                //create an input source for parsing
                //u can accept the file path as argument, harcoding for example
                File myfile= new File("c:\\temp\\sample1.xml");
                InputStream inputstream= new FileInputStream(myfile);
                InputStreamReader isr=new InputStreamReader(inputstream);
                InputSource inputsource=new InputSource(isr);
                
                //initialise the sax parser 
                SAXParserFactory saxfact=SAXParserFactory.newInstance();
                SAXParser saxparser=saxfact.newSAXParser();
                
                //call parse funstion  with a custom sax handler object as input
                saxparser.parse(inputsource,new anrSaxHandler());
                
                }
                catch(Exception e)
                {
                        System.out.println(e.getMessage());
                }
        }
}


custom SAX handler to override  the functionality:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*****************************
 * author: Aniruddha S N
 */
package customHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
public class anrSaxHandler extends DefaultHandler
{
    
        //function informs the sax event begining of a new element
        public void startElement(String uri, String localName,String qName, 
            Attributes attributes) throws SAXException {
                
                // u can write ur functionality here
                
                //sample functionality to print the element name and its attributes
                
                System.out.println("Reading element:"+qName+"\n");
                System.out.println("Attributes of the element and their values:\n");
                for(int i=0;i<attributes.getLength();i++)
                {
                        System.out.println(attributes.getLocalName(i)+": "+attributes.getValue(i)+"\n");
                }
                System.out.println("Data:");
        }
        
        //function informs the occurence of sax event end of an element
        public void endElement(String uri, String localName,
                        String qName) throws SAXException {
  
                //planned action on end of element can be added here
                
                //sample functionality forprinting the element name
                
                
                System.out.println("Finished Reading element:"+qName+"\n");
                
        }
        
   //function provides the data within a tag
        public void characters(char ch[], int start, int length) throws SAXException {
                
                
                //sample functionality to store data in a string and print it
                
                
                   String data=new String(ch,start, length);
                   System.out.println(""+data+"\n");
                
                
        }
}


sample file:



Results Of Parsing:

Reading element:catalog
 
Attributes of the element and their values:
 
Data:
 
   
 
Reading element:book
 
Attributes of the element and their values:
 
id: bk101
 
Data:
 
      
 
Reading element:author
 
Attributes of the element and their values:
 
Data:
Gambardella, Matthew
 
Finished Reading element:author
 
 
      
 
Reading element:title
 
Attributes of the element and their values:
 
Data:
XML Developer's Guide
 
Finished Reading element:title
 
 
      
 
Reading element:genre
 
Attributes of the element and their values:
 
Data:
Computer
 
Finished Reading element:genre
 
 
      
 
Reading element:price
 
Attributes of the element and their values:
 
Data:
44.95
 
Finished Reading element:price
 
 
      
 
Reading element:publish_date
 
Attributes of the element and their values:
 
Data:
2000-10-01
 
Finished Reading element:publish_date
 
 
      
 
Reading element:description
 
Attributes of the element and their values:
 
Data:
An in-depth look at creating applications 
      with XML.
 
Finished Reading element:description
 
 
   
 
Finished Reading element:book
 
 
 
 
Finished Reading element:catalog