{"id":1767,"date":"2011-03-22T16:46:00","date_gmt":"2011-03-22T11:16:00","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1767"},"modified":"2011-03-22T16:46:00","modified_gmt":"2011-03-22T11:16:00","slug":"xml-tree-viewer-using-sax-parser-in-java-with-jtreejfilechooser-component-of-swing","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/xml-tree-viewer-using-sax-parser-in-java-with-jtreejfilechooser-component-of-swing\/","title":{"rendered":"XML Tree Viewer using SAX Parser in JAVA with Jtree,JFileChooser component of Swing"},"content":{"rendered":"<p>I am going to create a utility application in JAVA for reading the XML document using SAX parser. The application will look like:<\/p>\n<figure id=\"attachment_1769\" aria-describedby=\"caption-attachment-1769\" style=\"width: 351px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/XML-Tree-Viewer-using-SAX-Parser-in-JAVA.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1769 \" title=\"XML Tree Viewer using SAX Parser in JAVA\" alt=\"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=351%2C491&#038;ssl=1\" width=\"351\" height=\"491\" \/><\/a><figcaption id=\"caption-attachment-1769\" class=\"wp-caption-text\">XML Tree Viewer using SAX Parser in JAVA<\/figcaption><\/figure>\n<p><!--more--><\/p>\n<p>Input File :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;\n&lt;Movies&gt;\n\t&lt;Movie Actor=&quot;Aamir Khan&quot; Name=&quot;Lagaan&quot; Type=&quot;BollyWood&quot;&gt;\n\t\tThis is node description for Movie Lagaan\n\t&lt;\/Movie&gt;\n\t&lt;Movie Actor=&quot;Aamir Khan&quot; Name=&quot;Andaaz Apana Apna&quot; Type=&quot;BollyWood&quot; \/&gt;\n\t&lt;Movie Actor=&quot;Salman Khan&quot; Name=&quot;Dabang&quot; Type=&quot;BollyWood&quot; \/&gt;\n\t&lt;Movie Actor=&quot;Salman Khan&quot; Name=&quot;Wanted&quot; Type=&quot;BollyWood&quot; \/&gt;\n\t&lt;Movie Actor=&quot;AKshay Kumar&quot; Name=&quot;Mujhse Shadi karoge&quot; Type=&quot;BollyWood&quot; \/&gt;\n\t&lt;Movies2011&gt;\n\t\t&lt;jan&gt; Yamla Pagala Deewana &lt;\/jan&gt;\n\t&lt;\/Movies2011&gt;\n&lt;\/Movies&gt;\n<\/pre>\n<p>Code:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">package com.G2.SAX;\n\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\nimport java.io.File;\n\nimport javax.swing.JButton;\nimport javax.swing.JFileChooser;\nimport javax.swing.JFrame;\nimport javax.swing.JLabel;\nimport javax.swing.JPanel;\nimport javax.swing.JScrollPane;\nimport javax.swing.JTextField;\nimport javax.swing.JTree;\nimport javax.swing.filechooser.FileFilter;\nimport javax.swing.filechooser.FileNameExtensionFilter;\nimport javax.swing.tree.DefaultMutableTreeNode;\nimport javax.swing.tree.DefaultTreeModel;\nimport javax.xml.parsers.SAXParser;\nimport javax.xml.parsers.SAXParserFactory;\n\nimport org.xml.sax.Attributes;\nimport org.xml.sax.SAXException;\nimport org.xml.sax.helpers.DefaultHandler;\n\npublic class XMLTreeViewer extends DefaultHandler {\n\n\tprivate JTree xmlJTree;\n\tDefaultTreeModel treeModel;\n\tint lineCounter;\n\tDefaultMutableTreeNode base = new DefaultMutableTreeNode(&quot;XML Viewer&quot;);\n\tstatic XMLTreeViewer treeViewer = null;\n\tJTextField txtFile = null;\n\n\t@Override\n\tpublic void startElement(String uri, String localName, String tagName, Attributes attr) throws SAXException {\n\n\t\tDefaultMutableTreeNode current = new DefaultMutableTreeNode(tagName);\n\n\t\tbase.add(current);\n\t\tbase = current;\n\n\t\tfor (int i = 0; i &lt; attr.getLength(); i++) {\n\t\t\tDefaultMutableTreeNode currentAtt = new DefaultMutableTreeNode(attr.getLocalName(i) + &quot; = &quot;\n\t\t\t\t\t+ attr.getValue(i));\n\t\t\tbase.add(currentAtt);\n\t\t}\n\t}\n\n\tpublic void skippedEntity(String name) throws SAXException {\n\t\tSystem.out.println(&quot;Skipped Entity: '&quot; + name + &quot;'&quot;);\n\t}\n\n\t@Override\n\tpublic void startDocument() throws SAXException {\n\t\tsuper.startDocument();\n\t\t base = new DefaultMutableTreeNode(&quot;XML Viewer&quot;);\n\t\t((DefaultTreeModel) xmlJTree.getModel()).setRoot(base);\n\t}\n\n\tpublic void characters(char&#x5B;] ch, int start, int length) throws SAXException {\n\n\t\tString s = new String(ch, start, length).trim();\n\t\tif (!s.equals(&quot;&quot;)) {\n\t\t\tDefaultMutableTreeNode current = new DefaultMutableTreeNode(&quot;Descrioption : &quot; + s);\n\t\t\tbase.add(current);\n\n\t\t}\n\t}\n\n\tpublic void endElement(String namespaceURI, String localName, String qName) throws SAXException {\n\n\t\tbase = (DefaultMutableTreeNode) base.getParent();\n\t}\n\n\tpublic static void main(String&#x5B;] args) {\n\t\ttreeViewer = new XMLTreeViewer();\n\t\t\/\/ treeViewer.xmlSetUp();\n\t\ttreeViewer.createUI();\n\n\t}\n\n\t@Override\n\tpublic void endDocument() throws SAXException {\n\t\t\/\/ Refresh JTree\n\t\t((DefaultTreeModel) xmlJTree.getModel()).reload();\n\t\texpandAll(xmlJTree);\n\t}\n\n\tpublic void expandAll(JTree tree) {\n\t\tint row = 0;\n\t\twhile (row &lt; tree.getRowCount()) {\n\t\t\ttree.expandRow(row);\n\t\t\trow++;\n\t\t}\n\t}\n\n\tpublic void xmlSetUp(File xmlFile) {\n\t\ttry {\n\t\t\tSAXParserFactory fact = SAXParserFactory.newInstance();\n\t\t\tSAXParser parser = fact.newSAXParser();\n\t\t\tparser.parse(xmlFile, this);\n\n\t\t} catch (Exception e) {\n\n\t\t}\n\t}\n\n\tpublic void createUI() {\n\n\t\ttreeModel = new DefaultTreeModel(base);\n\t\txmlJTree = new JTree(treeModel);\n\t\tJScrollPane scrollPane = new JScrollPane(xmlJTree);\n\n\t\tJFrame windows = new JFrame();\n\n\t\twindows.setTitle(&quot;XML Tree Viewer using SAX Parser in JAVA&quot;);\n\n\t\tJPanel pnl = new JPanel();\n\t\tpnl.setLayout(null);\n\t\tJLabel lbl = new JLabel(&quot;File :&quot;);\n\t\ttxtFile = new JTextField(&quot;Selected File Name Here&quot;);\n\t\tJButton btn = new JButton(&quot;Select File&quot;);\n\n\t\tbtn.addActionListener(new ActionListener() {\n\t\t\t@Override\n\t\t\tpublic void actionPerformed(ActionEvent evt) {\n\t\t\t\tJFileChooser fileopen = new JFileChooser();\n\t\t\t\tFileFilter filter = new FileNameExtensionFilter(&quot;xml files&quot;, &quot;xml&quot;);\n\t\t\t\tfileopen.addChoosableFileFilter(filter);\n\n\t\t\t\tint ret = fileopen.showDialog(null, &quot;Open file&quot;);\n\n\t\t\t\tif (ret == JFileChooser.APPROVE_OPTION) {\n\t\t\t\t\tFile file = fileopen.getSelectedFile();\n\t\t\t\t\ttxtFile.setText(file.getPath() + File.separator + file.getName());\n\t\t\t\t\txmlSetUp(file);\n\t\t\t\t}\n\n\t\t\t}\n\t\t});\n\t\tlbl.setBounds(0, 0, 100, 30);\n\t\ttxtFile.setBounds(110, 0, 250, 30);\n\t\tbtn.setBounds(360, 0, 100, 30);\n\t\tscrollPane.setBounds(0, 50, 500, 600);\n\n\t\tpnl.add(lbl);\n\n\t\tpnl.add(txtFile);\n\t\tpnl.add(btn);\n\n\t\tpnl.add(scrollPane);\n\n\t\twindows.add(pnl);\n\t\twindows.setSize(500, 700);\n\t\twindows.setVisible(true);\n\t\twindows.setDefaultCloseOperation( windows.EXIT_ON_CLOSE);\n\t}\n\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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<\/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":[192,227],"class_list":["post-1767","post","type-post","status-publish","format-standard","hentry","category-java","tag-swing","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":1767,"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":1756,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/searching-xml-file-using-dom-parser-of-jaxp\/","url_meta":{"origin":1767,"position":1},"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":1767,"position":2},"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":[]},{"id":1750,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/create-xml-file-using-jaxp-and-transformation-apis\/","url_meta":{"origin":1767,"position":3},"title":"Create XML File using DOM Parser of JAXP and Transformation APIs","author":"Jitendra","date":"March 22, 2011","format":false,"excerpt":"Example of Creating XML File using JAXP and Transformation APIs","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":2120,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/validate-xml-schema-using-dom-parser-and-sax-parser-in-java-using-swing\/","url_meta":{"origin":1767,"position":4},"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":1764,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/read-xml-file-using-sax-parser-in-java\/","url_meta":{"origin":1767,"position":5},"title":"Read XML File using SAX Parser in JAVA","author":"Jitendra","date":"March 22, 2011","format":false,"excerpt":"Example of Reading XML File using SAX Parser in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"SAX Parser","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/SAX-Parser.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1767","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=1767"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1767\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}