In my previous example, I have explained how to parse simple XML with on and multiple elements or objects. This is beyond that. Have you ever encountered and XML which have nested elements and the nested elements again have same nested objects and the tree grows. I happen to encounter such huge XML of size in few MBs. Sometimes collections like java.util.Stack or java.util.Map are helpful and very useful in this case. While parsing, you have to add and object as a child to another object, but it becomes difficult to determine to which parent object the child has to be added. The image below shows a basic tree structure of the XML file in this demo example.
Monday, August 23, 2010
Friday, August 20, 2010
XML parsing with SAX Parser Demo
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.
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.
Subscribe to:
Posts (Atom)