How to read XML file in Java Using SAX Parser

In this article, we will learn how to read XML file in java using SAX Parser.

SAX parser, or Simple API for XML parser uses callback function (org.xml.sax.helpers.DefaultHandler) to inform clients about the XML document structure.

SAX Parser is faster and uses less memory than DOM parser.

The callback methods are as below:-

  • startDocument() – This method called at the start of the XML document.
  • endDocument() – This method called at the end of the XML document.
  • startElement() – This method called at the start of a document element.
  • endElement() – This method called at the end of a document element.
  • characters() – This method called with the text contents in between the start and end tags of an XML document element.

Note: In Program, consider changing saxParser.parse(filepath,handler) with your system’s file path.

Java Program of SAX XML Parser

START Element :contactbook START Element :employee START Element :name Name :- Devil Donald END Element :name START Element :email Email :- donald.devil@abcd.com END Element :email START Element :phone Phone :- +91 9090909090 END Element :phone END Element :employee START Element :employee START Element :name Name :- Nevil Ray END Element :name START Element :email Email :- ray.nevil@abcd.com END Element :email START Element :phone Phone :- +1 919191919 END Element :phone END Element :employee END Element :contactbook
It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *