{"id":1753,"date":"2011-03-22T12:09:37","date_gmt":"2011-03-22T06:39:37","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1753"},"modified":"2011-03-22T12:09:37","modified_gmt":"2011-03-22T06:39:37","slug":"traverse-xml-file-using-dom-parser-of-jaxp","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/traverse-xml-file-using-dom-parser-of-jaxp\/","title":{"rendered":"Traverse XML file using DOM Parser of JAXP"},"content":{"rendered":"<p>Check this post to get the idea about how to <a title=\"Create XML document\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/create-xml-file-using-jaxp-and-transformation-apis\/\" target=\"_blank\">create XML document using DOM Parser and XSLT API<\/a>.<\/p>\n<p>In DOM Parser of JAXP, <strong>textContent() <\/strong>method of node does not give the text for that node. We <strong>must typecast <\/strong>the node to Element and then try this method.<\/p>\n<p>See below code for implementation:<!--more--><\/p>\n<p><strong>Input File:<\/strong><\/p>\n<pre class=\"brush: java; 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\n&lt;\/Movies&gt;\n<\/pre>\n<p><strong>Java Code:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.DOM;\n\nimport java.io.IOException;\n\nimport javax.xml.parsers.DocumentBuilder;\nimport javax.xml.parsers.DocumentBuilderFactory;\nimport javax.xml.parsers.ParserConfigurationException;\n\nimport org.w3c.dom.Document;\nimport org.w3c.dom.Element;\nimport org.w3c.dom.NamedNodeMap;\nimport org.w3c.dom.Node;\nimport org.w3c.dom.NodeList;\nimport org.xml.sax.SAXException;\n\npublic class ReadXML {\n\tpublic static void main(String&#x5B;] args) {\n\t\tDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\n\t\tDocumentBuilder builder;\n\t\ttry {\n\t\t\tbuilder = factory.newDocumentBuilder();\n\t\t\tDocument doc = builder.parse(&quot;ShivaSoft.xml&quot;);\n\n\t\t\t\/\/Get Root Node and its child\n\t\t\tNode root = doc.getDocumentElement();\n\n\t\t\tNodeList childNodes = root.getChildNodes();\n\t\t\tfor(int i=0;i&lt;childNodes.getLength();i++)\n\t\t\t{\n\t\t\t\tprintNodeInfo(childNodes.item(i));\n\t\t\t}\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\n\t}\n\tstatic void printNodeInfo(Node node)\n\t{\n\t\tSystem.out.println(&quot; Name : &quot; + node.getNodeName());\n\t\tSystem.out.println(&quot; Type : &quot;+ nodeType(node.getNodeType()));\n\t\tif(node.getNodeType() == Node.ELEMENT_NODE)\n\t\t{\n\t\t\tElement e = (Element)node;\n\t\t\tSystem.out.println(e.getTextContent().trim());\n\t\t}\n\t\tif(node.hasAttributes())\n\t\t{\n\t\t\tNamedNodeMap rootAtt = node.getAttributes();\n\t\t\tfor(int i=0;i&lt;rootAtt.getLength();i++)\n\t\t\t{\n\t\t\t\tSystem.out.println(&quot; Name : &quot;+rootAtt.item(i).getNodeName());\n\t\t\t\tSystem.out.println(&quot; Value : &quot;+rootAtt.item(i).getNodeValue());\n\t\t\t\tSystem.out.println(&quot; Type : &quot;+nodeType(rootAtt.item(i).getNodeType()));\n\t\t\t\tSystem.out.println(&quot;--------------------------&quot;);\n\t\t\t}\n\t\t}\n\t}\n\tstatic String nodeType(short type) {\n\t    switch(type) {\n\t      case Node.ELEMENT_NODE:                return &quot;Element&quot;;\n\t      case Node.DOCUMENT_TYPE_NODE:          return &quot;Document type&quot;;\n\t      case Node.ENTITY_NODE:                 return &quot;Entity&quot;;\n\t      case Node.ENTITY_REFERENCE_NODE:       return &quot;Entity reference&quot;;\n\t      case Node.NOTATION_NODE:               return &quot;Notation&quot;;\n\t      case Node.TEXT_NODE:                   return &quot;Text&quot;;\n\t      case Node.COMMENT_NODE:                return &quot;Comment&quot;;\n\t      case Node.CDATA_SECTION_NODE:          return &quot;CDATA Section&quot;;\n\t      case Node.ATTRIBUTE_NODE:              return &quot;Attribute&quot;;\n\t      case Node.PROCESSING_INSTRUCTION_NODE: return &quot;Attribute&quot;;\n\t    }\n\t    return &quot;Unidentified&quot;;\n\t  }\n}\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Example of Traverse XML file using DOM Parser of JAXP<\/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-1753","post","type-post","status-publish","format-standard","hentry","category-java","tag-xml"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1756,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/searching-xml-file-using-dom-parser-of-jaxp\/","url_meta":{"origin":1753,"position":0},"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":1750,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/create-xml-file-using-jaxp-and-transformation-apis\/","url_meta":{"origin":1753,"position":1},"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":1767,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/xml-tree-viewer-using-sax-parser-in-java-with-jtreejfilechooser-component-of-swing\/","url_meta":{"origin":1753,"position":2},"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":1774,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/difference-between-sax-and-dom-parsers\/","url_meta":{"origin":1753,"position":3},"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":1742,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/introduction-to-jaxp\/","url_meta":{"origin":1753,"position":4},"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":1737,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/dom-document-object-module\/","url_meta":{"origin":1753,"position":5},"title":"DOM \u2013 Document Object Module","author":"Jitendra","date":"March 22, 2011","format":false,"excerpt":"What is DOM in JAVA. DOM stands for Document Object Module","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Diagramatical representation of DOM","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Diagramatical-representation-of-DOM.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\/1753","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=1753"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1753\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}