{"id":1395,"date":"2016-11-07T16:10:47","date_gmt":"2016-11-07T16:10:47","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1395"},"modified":"2024-12-14T04:11:43","modified_gmt":"2024-12-14T04:11:43","slug":"how-to-parse-an-xml-file-with-jdom","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-parse-an-xml-file-with-jdom\/","title":{"rendered":"How to Parse an XML file with JDOM?"},"content":{"rendered":"<h1>How to Parse an XML file with JDOM?<\/h1>\n<p>Let us write a sample JDOM exercise Java program the reads the XML file and lists all the environment names in the XML file. Included are the sample\u00a0 XML file and the Java program.<\/p>\n<p>In order to use JDOM library in our program, we need to place the below maven dependency in your project <strong>pom<\/strong> file.<\/p>\n<h2>JDOM Maven dependency<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;dependency&gt;\r\n&lt;groupId&gt;org.jdom&lt;\/groupId&gt;\r\n&lt;artifactId&gt;jdom2&lt;\/artifactId&gt;\r\n&lt;version&gt;2.0.5&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p>&nbsp;<\/p>\n<h2>Sample XML File<\/h2>\n<p>Let us consider the below sample xml file that has details about testing environments. The file name is \u201csample.xml\u201d<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;environments&gt;\r\n   &lt;environment&gt;\r\n   &lt;name&gt;QA&lt;\/name&gt;\r\n   &lt;username&gt;qa&lt;\/username&gt;\r\n   &lt;password&gt;qa1234&lt;\/password&gt;\r\n      &lt;browsercontext&gt;\r\n         &lt;Browser&gt;Firefox&lt;\/Browser&gt;\r\n         &lt;OS&gt;Linux&lt;\/OS&gt;\r\n      &lt;\/browsercontext&gt;\r\n&lt;\/environment&gt;\r\n&lt;environment&gt;\r\n    &lt;name&gt;STAGING&lt;\/name&gt;\r\n    &lt;username&gt;stage&lt;\/username&gt;\r\n    &lt;password&gt;stage1234&lt;\/password&gt;\r\n        &lt;browsercontext&gt;\r\n           &lt;Browser&gt;Chrome&lt;\/Browser&gt;\r\n           &lt;OS&gt;Windows&lt;\/OS&gt;\r\n        &lt;\/browsercontext&gt;\r\n&lt;\/environment&gt;\r\n&lt;environment&gt;\r\n      &lt;name&gt;PRODUCTION&lt;\/name&gt;\r\n      &lt;username&gt;prod&lt;\/username&gt;\r\n      &lt;password&gt;prod1234&lt;\/password&gt;\r\n         &lt;browsercontext&gt;\r\n            &lt;Browser&gt;Chrome&lt;\/Browser&gt;\r\n            &lt;OS&gt;Windows&lt;\/OS&gt;\r\n         &lt;\/browsercontext&gt;\r\n&lt;\/environment&gt;\r\n&lt;\/environments&gt;<\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>Sample Java Program<\/strong><\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class JDOMExercise {\r\n\r\npublic static void main(String[] args) throws IOException, JDOMException {\r\nString filename = \"sample.xml\";\r\nPrintStream out = System.out;\r\n\r\nSAXBuilder builder = new SAXBuilder();\r\nDocument doc = builder.build(new File(filename));\r\nElement root = doc.getRootElement();\r\n\r\nList&lt;Element&gt; environments = root.getChildren(\"environment\");\r\nout.println(\"The xml file has \"+ environments.size() + \" environments\");\r\nIterator&lt;Element&gt; i = environments.iterator();\r\nwhile (i.hasNext()) {\r\nElement environment = (Element) i.next();\r\nout.println( \"Environment:\" + environment.getChildText(\"name\"));\r\n\r\n}\r\n\r\n}\r\n}\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>Program Output<\/strong><\/h2>\n<p>The xml file has 3 environments<br \/>\nEnvironment:QA<br \/>\nEnvironment:STAGING<br \/>\nEnvironment:PRODUCTION<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-605\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/XML-file-parsing-JDOM.jpeg\" alt=\"XML file parsing JDOM\" width=\"1365\" height=\"724\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p><strong>SAXBuilde<\/strong>r builds a JDOM Document using a SAX parser.<\/p>\n<p><strong>Document<\/strong> class represents an XML document. It has methods that allow access to the root element as well as the and other document-level information.<\/p>\n<p>The<strong> Element<\/strong> class represents an XML element. It has methods that allow us to get and manipulate its child elements and content. We can directly access the element\u2019s content, manipulate its attributes etc.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Parse an XML file with JDOM? Let us write a sample JDOM exercise Java program the reads the XML file and lists all the environment names in the XML file. Included are the sample\u00a0 XML file and the Java program. In order to use JDOM library in our program, we need to place [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1395","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1395"}],"version-history":[{"count":11,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1395\/revisions"}],"predecessor-version":[{"id":26386,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1395\/revisions\/26386"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}