Java provides two ways of parsing XML. Namely SAXParser and DOM Parser. With DOM Parser complete XML document object is held in emory and uses the DOM approach with DOM elements. This approach is used when you need to get hold of elements from XML in any order. But using DOM parser is costly in term of memory. When you have a XML document and want to read it once and create in memory java object (POJO), go for SAXParser approach. SAXParser parses the document(XML) from top to bottom, tag by tag.
Using Sax parser is simple. All we need to do is create a
Handler class which extends
DefaultHandler class of
org.xml.sax.helpers package (part of JDK). In this Handler class, we define how tags are processed and stored into in memory object.
Once the Handler is developed, Create a
Parser class which uses
SAXParser abstract class from
javax.xml.parsers package (also part of JDK). Use this Parser class and parse the document with help of the Handler class which we created. Current example demonstrate simple XML, for parsing nested XML with hierarchy, read a another article as
how to parse nested XML.
Below is a snippet of simple XML file which needs to be parsed.