{"id":1764,"date":"2011-03-22T16:38:51","date_gmt":"2011-03-22T11:08:51","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1764"},"modified":"2011-03-22T16:38:51","modified_gmt":"2011-03-22T11:08:51","slug":"read-xml-file-using-sax-parser-in-java","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/read-xml-file-using-sax-parser-in-java\/","title":{"rendered":"Read XML File using SAX Parser in JAVA"},"content":{"rendered":"<p>We have already discussed about the<a title=\"DOM Parsers\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/create-xml-file-using-jaxp-and-transformation-apis\/\"> DOM Parser of JAXP<\/a>. This article will focus on Reading XML file using SAX Parser:<\/p>\n<figure id=\"attachment_1765\" aria-describedby=\"caption-attachment-1765\" style=\"width: 439px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/SAX-Parser.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1765 \" title=\"SAX Parser\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/SAX-Parser.jpg?resize=439%2C313&#038;ssl=1\" alt=\"SAX Parser\" width=\"439\" height=\"313\" \/><\/a><figcaption id=\"caption-attachment-1765\" class=\"wp-caption-text\">SAX Parser<\/figcaption><\/figure>\n<p><!--more-->SAXParser object can obtained from the <strong>SAXParserFactory <\/strong>and then we can process the document using that parser.<br \/>\nThe most important interface in SAX parser class is <strong>ContentHandler<\/strong>. This interface requires a number of methods that the SAX parser invokes in response to various parsing events. The major event-handling methods are: <strong>startDocument, endDocument, startElement, and endElement.<\/strong><br \/>\nThe easiest way to implement this interface is to extend the <strong>DefaultHandler <\/strong>class, defined in the<strong> org.xml.sax.helpers<\/strong> package. That class provides do-nothing methods for all the ContentHandler events.<\/p>\n<p>The example program extends that class.<br \/>\n<strong>public class SAXLocalNameCount extends DefaultHandler {<\/strong><\/p>\n<p>Example :<\/p>\n<p><em>Locator to indicate the current position of SAX Parser.<\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.SAX;\n\nimport java.io.IOException;\n\nimport javax.xml.parsers.ParserConfigurationException;\nimport javax.xml.parsers.SAXParser;\nimport javax.xml.parsers.SAXParserFactory;\n\nimport org.xml.sax.Attributes;\nimport org.xml.sax.Locator;\nimport org.xml.sax.SAXException;\nimport org.xml.sax.helpers.DefaultHandler;\n\npublic class XMlParserLocator extends DefaultHandler {\n\tLocator locator;\n\n\t@Override\n\tpublic void startElement(String uri, String localName, String tagName, Attributes arg3) throws SAXException {\n\t\tString locationString = &quot;&quot;;\n\t\tif (locator != null) {\n\t\t\tlocationString += locator.getSystemId();\n\t\t\tlocationString += &quot;Line : &quot; + locator.getLineNumber();\n\t\t\tlocationString += &quot;Column : &quot; + locator.getColumnNumber();\n\t\t\tSystem.out.println(locationString);\n\n\t\t}\n\t}\n\n\tpublic static void main(String&#x5B;] args) {\n\n\t\ttry {\n\t\t\tSAXParserFactory saxFactory = SAXParserFactory.newInstance();\n\t\t\tSAXParser parser = saxFactory.newSAXParser();\n\t\t\tparser.parse(&quot;ShivaSoft.xml&quot;, new XMlParserLocator());\n\n\t\t} catch (ParserConfigurationException e) {\n\t\t\te.printStackTrace();\n\t\t} catch (SAXException e) {\n\t\t\te.printStackTrace();\n\t\t} catch (IOException e) {\n\t\t\te.printStackTrace();\n\t\t}\n\t}\n\n\t@Override\n\tpublic void setDocumentLocator(Locator arg0) {\n\t\tlocator = arg0;\n\t\tSystem.out.println(&quot;setDocumentLocator() Called&quot;);\n\t}\n\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Example of Reading XML File using SAX Parser in JAVA<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3],"tags":[227],"class_list":["post-1764","post","type-post","status-publish","format-standard","hentry","category-java","tag-xml"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1774,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/difference-between-sax-and-dom-parsers\/","url_meta":{"origin":1764,"position":0},"title":"Difference between SAX and DOM Parsers","author":"Jitendra","date":"March 22, 2011","format":false,"excerpt":"What is the difference between SAX And DOM Parsers","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1767,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/xml-tree-viewer-using-sax-parser-in-java-with-jtreejfilechooser-component-of-swing\/","url_meta":{"origin":1764,"position":1},"title":"XML Tree Viewer using SAX Parser in JAVA with Jtree,JFileChooser component of Swing","author":"Jitendra","date":"March 22, 2011","format":false,"excerpt":"XML Tree Viewer using SAX Parser in JAVA with Jtree,JFileChooser component of Swing, How to refresh Jtree when content changes , Expand or collapse JTree","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"XML Tree Viewer using SAX Parser in JAVA","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/XML-Tree-Viewer-using-SAX-Parser-in-JAVA.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2120,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/validate-xml-schema-using-dom-parser-and-sax-parser-in-java-using-swing\/","url_meta":{"origin":1764,"position":2},"title":"Validate XML Schema Using DOM Parser and SAX Parser in JAVA Using Swing","author":"Jitendra","date":"May 13, 2011","format":false,"excerpt":"Validate XML Schema Using DOM Parser and SAX Parser in JAVA Using Swing","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Input Screen - XML Schema Validation in JAVA Using DOM and SAX Parser","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/05\/Input-Screen-XML-Schema-Validation-in-JAVA-Using-DOM-and-SAX-Parser.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1742,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/introduction-to-jaxp\/","url_meta":{"origin":1764,"position":3},"title":"Introduction to JAXP and Difference between JAXP and JAXB","author":"Jitendra","date":"March 22, 2011","format":false,"excerpt":"Introduction to JAXP (JAVA API for XML Processing), SAX (Simple API for XML Parsing),Extensible Style sheet language transformation (XSLT)","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"XML Processing using DOM in JAXP","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/XML-Processing-using-DOM-in-JAXP.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1756,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/searching-xml-file-using-dom-parser-of-jaxp\/","url_meta":{"origin":1764,"position":4},"title":"Searching XML File using DOM Parser of JAXP","author":"Jitendra","date":"March 22, 2011","format":false,"excerpt":"Example of Searching XML File using DOM Parser of JAXP","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1753,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/traverse-xml-file-using-dom-parser-of-jaxp\/","url_meta":{"origin":1764,"position":5},"title":"Traverse XML file using DOM Parser of JAXP","author":"Jitendra","date":"March 22, 2011","format":false,"excerpt":"Example of Traverse XML file using DOM Parser of JAXP","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1764"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1764\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}