XML parsing using SAX parser in java
In java we have multiple ways to parse XML. We will see steps for XML parsing using SAX parser in java in this post.
What are SAX parsers?
SAX parsers are event based parsers. SAX parsers parse the XML line by line, hence do not create any document in the memory.
When to use SAX parsers?
1. When you want to process the XML line by line
2. When you have a large XML. SAX parse do not store the XML in memory, hence they can be faster than DOM parser’s for large XML’s.
3. When you only want to traverse the XML in forward manner, as SAX parsers do not allow backward traversing as supported by DOM parser.
Steps for XML parsing using SAX parser in java
SAX parser use callback function (org.xml.sax.helpers.DefaultHandler) to inform about the XML document structure that was parsed.
below are some of the important methods that need to be used while parsing
1 2 3 4 5 6 7 8 | public void startDocument() throws SAXException public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException public void endElement(String uri, String localName, String qName) throws SAXException public void endDocument() throws SAXException public void characters(char ch[], int start, int length) throws SAXException |
Example
1. Complete XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <employeeDetails> <employee id="0001"> <name>John Doe</name> <department>Sales</department> <salary>$5000</salary> </employee> <employee id="0002"> <name>Scott Tiger</name> <department>HR</department> <salary>$4000</salary> </employee> <employee id="0003"> <name>David Smith</name> <department>Security</department> <salary>$4500</salary> </employee> </employeeDetails> |
2. Add a XML Handler that extends the DefaultHandler provided and use the above methods 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 | package com.kscodes.sampleproject; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class XmlSaxParser extends DefaultHandler { boolean name = false; boolean department = false; boolean salary = false; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("employee")) { String id = attributes.getValue("id"); System.out.println("Employee Details Start--------"); System.out.println("Employee ID : " + id); } else if (qName.equalsIgnoreCase("name")) { name = true; } else if (qName.equalsIgnoreCase("department")) { department = true; } else if (qName.equalsIgnoreCase("salary")) { salary = true; } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("employee")) { System.out.println("Employee Details Finish-------"); } } @Override public void characters(char ch[], int start, int length) throws SAXException { if (name) { System.out.println("Name: " + new String(ch, start, length)); name = false; } else if (department) { System.out.println("Department: " + new String(ch, start, length)); department = false; } else if (salary) { System.out.println("Salary: " + new String(ch, start, length)); salary = false; } } } |
3. Create a main class that will call the XML handler and display the output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.kscodes.sampleproject; import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; public class XmlSaxParserDemo { public static void main(String[] args) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); XmlSaxParser xmlParser = new XmlSaxParser(); saxParser.parse(new File("C:\\kscodes\\employee.xml"), xmlParser); } catch (Exception e) { System.out.println("Error while parsing file :: " + e); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Employee Details Start-------- Employee ID : 0001 Name: John Doe Department: Sales Salary: $5000 Employee Details Finish------- Employee Details Start-------- Employee ID : 0002 Name: Scott Tiger Department: HR Salary: $4000 Employee Details Finish------- Employee Details Start-------- Employee ID : 0003 Name: David Smith Department: Security Salary: $4500 Employee Details Finish------- |