A Technology Blog About Code Development, Architecture, Operating System, Hardware, Tips and Tutorials for Developers.

Wednesday, November 14, 2012

XML Parsing in java - SAX Parser

11:51:00 PM Posted by Satish Kumar , No comments
SAX parser is work differently than DOM parser, it neither load any XML document into memory nor create any object representation of the XML document. SAX provides an Event-Driven XML Processing following the Push-Parsing Model. What this model means is that in SAX, Applications will register Listeners in the form of Handlers to the Parser and will get notified through Call-back methods. Here the SAX Parser takes the control over Application thread by Pushing Events to the Application. SAX Parser is faster and uses less memory than DOM parser.
The DOM interface is the easiest XML parser to understand, and use. It parses entire XML document and loads it into memory; then models it with Object for easy traversal or manipulation. DOM Parser is slow and will consume a lot of memory when it loads an XML document which contains a lot of data. Please consider SAX parser as solution for it, SAX is faster than DOM and use less memory.

 JAVA AND XML
                   XML Parsing using Java
                         1. DOM XML Parser
                         2. SAX XML Parser
                         3. StaX XML Parser
                         4. JAXB XML Parser

See following SAX callback methods :
  • startDocument() and endDocument() – Method called at the start and end of an XML document.
  • startElement() and endElement() – Method called at the start and end of a document element.
  • characters() – Method called with the text contents in between the start and end tags of an XML document element.
I have used the following things for this tutorial.

1. JDK 7
2. Maven2

Let's create a java project using maven 

1
mvn archetype:generate -DgroupId=com.techiekernel -DartifactId=ParserDemo -Dpackagename=com.techiekernel

Now create a sample xml document to parse

 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
<?xml version="1.0" encoding="UTF-8"?>
<products>
 <product>
  <name>R15</name>
  <make>Yamaha</make>
  <engine-cc>150</engine-cc>
  <type>sports</type>
 </product>
 <product>
  <name>Duke</name>
  <make>KTM</make>
  <engine-cc>200</engine-cc>
  <type>Street</type>
 </product>
 <product>
  <name>GS650GS Sertao</name>
  <make>BMW</make>
  <engine-cc>650</engine-cc>
  <type>Enduro</type>
 </product>
 <product>
  <name>Multistada</name>
  <make>Ducati</make>
  <engine-cc>1210</engine-cc>
  <type>Touring</type>
 </product>
</products>

The program to parse the xml

 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.techiekernel.parser.sax;

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 SaxReader {
 public static void main(String argv[]) {
   
     try {
  
  SAXParserFactory factory = SAXParserFactory.newInstance();
  SAXParser saxParser = factory.newSAXParser();
  
  DefaultHandler handler = new DefaultHandler() {
  
  boolean name = false;
  boolean make = false;
  boolean engineCC = false;
  boolean type = false;
  
  public void startElement(String uri, String localName,String qName, 
                 Attributes attributes) throws SAXException {
  
   //System.out.println("Start Element :" + qName);
  
   if (qName.equalsIgnoreCase("name")) {
    name = true;
   }
  
   if (qName.equalsIgnoreCase("make")) {
    make = true;
   }
  
   if (qName.equalsIgnoreCase("engine-cc")) {
    engineCC = true;
   }
  
   if (qName.equalsIgnoreCase("type")) {
    type = true;
   }
  
  }
  
  public void endElement(String uri, String localName,
   String qName) throws SAXException {
  
   //System.out.println("End Element :" + qName);
  
  }
  
  public void characters(char ch[], int start, int length) throws SAXException {
  
   if (name) {
    System.out.println("Name : " + new String(ch, start, length));
    name = false;
   }
  
   if (make) {
    System.out.println("Make : " + new String(ch, start, length));
    make = false;
   }
  
   if (engineCC) {
    System.out.println("Engine : " + new String(ch, start, length));
    engineCC = false;
   }
  
   if (type) {
    System.out.println("Type : " + new String(ch, start, length));
    type = false;
   }
  
  }
  
      };
  
        saxParser.parse("product.xml", handler);
  
      } catch (Exception e) {
        e.printStackTrace();
      }
  
    }
}

Output:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Name : R15
Make : Yamaha
Engine : 150
Type : sports
Name : Duke
Make : KTM
Engine : 200
Type : Street
Name : GS650GS Sertao
Make : BMW
Engine : 650
Type : Enduro
Name : Multistada
Make : Ducati
Engine : 1210
Type : Touring


Source Code:

You can pull the code from GitHub.

0 comments:

Post a Comment