{"id":33,"date":"2005-11-08T22:40:27","date_gmt":"2005-11-09T03:40:27","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/?p=33"},"modified":"2005-11-08T22:40:27","modified_gmt":"2005-11-09T03:40:27","slug":"using-dom4j-reading-an-xml-file","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2005\/11\/08\/using-dom4j-reading-an-xml-file\/","title":{"rendered":"Using Dom4J: Reading An XML File"},"content":{"rendered":"<p>For almost every project I have worked on I have had to work with XML files.  I&#8217;ve used SAX and DOM parsers and have even written my own XML writers.  But now for most of my XML needs I use Dom4J.  Dom4J will get you started quickly.  These four little lines will read in an XML file:<\/p>\n<pre>\nFile xml = new File(&quot;simple.xml&quot;);\nSAXReader reader = new SAXReader();\nDocument doc = reader.read(xml);\nElement root = doc.getRootElement();\n<\/pre>\n<p>Of course, you will need to import the Dom4J SAXReader, Document and Element classes from the correct package.  The SAXReader read method is heavily overloaded and you can read from a String, URL, InputStream, etc.  Once you have an Element object you can get the name, attributes, and child elements.  The following code will iterate through the child elements:<\/p>\n<pre>\nfor(Iterator i = root.elements().iterator(); i.hasNext();)\n   Element elem = (Element)i.next();\n<\/pre>\n<p>This is all the code you need to start reading in an XML document.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For almost every project I have worked on I have had to work with XML files. I&#8217;ve used SAX and DOM parsers and have even written my own XML writers. But now for most of my XML needs I use Dom4J. Dom4J will get you started quickly. These four little lines will read in an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[13,15,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-x","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/33"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}