Java XML Parsing Tutorial: SAX - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community



SAX is a stream-based processor. Only a tiny part is stored in memory at any time and the XML stream can be sniffed by implementing callback code for events like tagStarted() etc.
  • It is an event-based parser.
  • It parses the file as it reads it.
  • It does not require huge memory as the whole document is not stored at once.
  • It is read-only i.e. can't insert or delete any node.
  • It is preferred when memory content is large.
  • It reads the XML file from top to bottom and it does not support backward navigation.

Content of Employee.java
package com.t4b.parser.sax.test; public class Employee { private String name; private int age; private int id; private String type; public Employee() { } public Employee(String name, int id, int age, String type) { this.name = name; this.age = age; this.id = id; this.type = type; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String toString() { StringBuffer sb = new StringBuffer(); sb.append("Employee Details - "); sb.append("Name:" + getName()); sb.append(", "); sb.append("Type:" + getType()); sb.append(", "); sb.append("Id:" + getId()); sb.append(", "); sb.append("Age:" + getAge()); sb.append("."); return sb.toString(); } }

Content of SAXParserTest.java
package com.t4b.parser.sax.test; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.xml.parsers.ParserConfigurationException; 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 SAXParserTest extends DefaultHandler { List<Employee> myEmpls; private String tempVal; private Employee tempEmp; public SAXParserTest() { myEmpls = new ArrayList<Employee>(); } public void runExample() { parseDocument(); printData(); } private void parseDocument() { SAXParserFactory spf = SAXParserFactory.newInstance(); try { SAXParser sp = spf.newSAXParser(); sp.parse("employees.xml", this); } catch (SAXException se) { se.printStackTrace(); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (IOException ie) { ie.printStackTrace(); } } private void printData() { System.out.println("No of Employees '" + myEmpls.size() + "'."); Iterator<Employee> it = myEmpls.iterator(); while (it.hasNext()) { System.out.println(it.next().toString()); } } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println("start - " + qName + "\n"); tempVal = ""; if (qName.equalsIgnoreCase("Employee")) { tempEmp = new Employee(); tempEmp.setType(attributes.getValue("type")); } } public void characters(char[] ch, int start, int length) throws SAXException { tempVal = new String(ch, start, length); } public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("end - " + qName); if (qName.equalsIgnoreCase("Employee")) { myEmpls.add(tempEmp); } else if (qName.equalsIgnoreCase("Name")) { tempEmp.setName(tempVal); } else if (qName.equalsIgnoreCase("Id")) { tempEmp.setId(Integer.parseInt(tempVal)); } else if (qName.equalsIgnoreCase("Age")) { tempEmp.setAge(Integer.parseInt(tempVal)); } } public static void main(String[] args) { SAXParserTest spe = new SAXParserTest(); spe.runExample(); } }

Content of employees.xml document for parsing
<?xml version="1.0" encoding="UTF-8"?> <Personnel> <Employee type="permanent"> <Name>John</Name> <Id>1000</Id> <Age>34</Age> </Employee> <Employee type="contract"> <Name>Robin</Name> <Id>1001</Id> <Age>25</Age> </Employee> <Employee type="permanent"> <Name>Neil</Name> <Id>1002</Id> <Age>26</Age> </Employee> </Personnel>


Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.