SAXParser

Read XML File in Java using SAX parser example

In this tutorial we are going to see how you can parse an XML File using a SAX parser. SAX parsers are very popular and much more frequently used than DOM parsers. The reason is that they are much more memory efficient (and this is important for large XML Files) and much faster. It’s all down to the way the SAX parsers work. They don’t create an object with tree structure and store it in the memory while parsing the file. The idea of the SAX parsing is much simpler and it lies in callback methods. Basically there are 3 groups of callback methods:

  • startDocument() and endDocument(), called at the start and end of an XML document.
  • startElement() and endElement() called at the start and end of an element tag.
  • characters() called when the text contents between the start and end tags of an elements are parsed.

These are the functions that inform the clients about the structure of the XML File. In order to implement these functions you have to create a class that extends org.xml.sax.helpers.DefaultHandler class as you can see in the code snippets that follow.

Here is the XML File we are going to use for the demo:

testFile.xml:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8" standalone="no"?><company>
 
    <employee id="10">
        <firstname>Jeremy</firstname>
        <lastname>Harley</lastname>
        <email>james@example.org</email>
        <department>Human Resources</department>
        <salary>2000000</salary>
        <address>34 Stanley St.</address>
 
    </employee>
 
    <employee id="2">
        <firstname>John</firstname>
        <lastname>May</lastname>
        <email>john@example.org</email>
        <department>Logistics</department>
        <salary>400</salary>
        <address>123 Stanley St.</address>
    </employee>
 
</company>

Now we just have to create the MyHandler class that extends DefaultHandler and that will implement the callback functions we need.

MyHandler.java:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
package com.javacodegeeks.java.core;
 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
public class MyHandler extends DefaultHandler {
 
    boolean tagFname = false;
    boolean tagLname = false;
    boolean tagEmail = false;
    boolean tagDep = false;
    boolean tagSalary = false;
    boolean tagAddress = false;
 
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
 
        if (attributes.getLength() > 0) {
 
            String tag = "<" + qName;
            for (int i = 0; i < attributes.getLength(); i++) {
 
                tag += " " + attributes.getLocalName(i) + "="
                        + attributes.getValue(i);
            }
 
            tag += ">";
            System.out.println(tag);
 
        } else {
 
            System.out.println("<" + qName + ">");
        }
 
        if (qName.equalsIgnoreCase("firstname")) {
            tagFname = true;
        }
 
        if (qName.equalsIgnoreCase("lastname")) {
            tagLname = true;
        }
 
        if (qName.equalsIgnoreCase("email")) {
            tagEmail = true;
        }
 
        if (qName.equalsIgnoreCase("department")) {
            tagDep = true;
        }
 
        if (qName.equalsIgnoreCase("salary")) {
            tagSalary = true;
        }
 
        if (qName.equalsIgnoreCase("address")) {
            tagAddress = true;
        }
 
    }
 
    public void characters(char ch[], int start, int length)
            throws SAXException {
 
        if (tagFname) {
            System.out.println(new String(ch, start, length));
            tagFname = false;
        }
 
        if (tagLname) {
            System.out.println(new String(ch, start, length));
            tagLname = false;
        }
 
        if (tagEmail) {
            System.out.println(new String(ch, start, length));
            tagEmail = false;
        }
 
        if (tagDep) {
            System.out.println(new String(ch, start, length));
            tagDep = false;
        }
 
        if (tagSalary) {
            System.out.println(new String(ch, start, length));
            tagSalary = false;
        }
 
        if (tagAddress) {
            System.out.println(new String(ch, start, length));
            tagAddress = false;
        }
 
    }
 
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
 
        System.out.println("</" + qName + ">");
 
    }
 
}

ParseXMLFileWithSAX.java:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.javacodegeeks.java.core;
 
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
 
public class ParseXMLFileWithSAX {
 
    private static final String xmlFilePath = "C:\\Users\\nikos7\\Desktop\\filesForExamples\\testFile.xml";
 
    public static void main(String argv[]) {
 
        try {
 
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
 
            saxParser.parse(xmlFilePath, new MyHandler());
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
}

Output:

<company>
<employee id=10>
<firstname>
Jeremy
</firstname>
<lastname>
Harley
</lastname>
<email>
james@example.org
</email>
<department>
Human Resources
</department>
<salary>
2000000
</salary>
<address>
34 Stanley St.
</address>
</employee>
<employee id=2>
<firstname>
John
</firstname>
<lastname>
May
</lastname>
<email>
john@example.org
</email>
<department>
Logistics
</department>
<salary>
400
</salary>
<address>
123 Stanley St.
</address>
</employee>
</company>

 
This was an example on how to read XML File in Java using SAX parser example

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Back to top button