Introduction to DOM4J Parser in Java with example programs

Introduction to DOM4J Parser in Java with example

DOM4J is an open source Java API that provides a powerful set of features to process, manipulate or navigate XML documents using the Java Collections Framework and work with XPath and XSLT. It integrates well with SAX, JAXP and DOM APIs. It is easy-to-use and highly efficient. This post gives an Introduction to DOM4J Parser in Java with example programs.

Important DOM4J Classes

The DOM4J library defines several Java classes. Here are the most important classes

  • DOMReader – DOMReader navigates a W3C DOM tree and creates a DOM4J tree from it. The DOMReader class allows you to convert an existing DOM tree into a dom4j tree.
  • SAXReader   SAXReader creates a DOM4J tree from SAX parsing events.
  • Document – Defines an XML document. A Document object is often referred to as a DOM tree. It provides methods to get the root element as well as the DocType and other document-level information
  • Element – This class represents an XML element. An element can have declared namespaces, attributes, child nodes and textual content
  • Attribute – Represents an attribute of an element. An attribute may have a name, an optional namespace and a value.
  • Text – Defines an XML Text node. Represents the text between XML tag.
  • Comment – Defines the behavior of an XML comment. Represents the comments in a XML document.
  • Node  Defines the polymorphic behavior for all XML nodes in a dom4j tree. A node can be an Element, Attribute etc. A node can be output as its XML format, can be detached from its position in a document and can have XPath expressions evaluated on itself.

 

dom4j also offers classes for integration with the XML processing APIs – SAX and DOM. The org.dom4j.SAXContentHandler class implements several SAX interfaces directly (such as ContentHandler) so that you can embed dom4j directly inside any SAX application. You can also use this class to implement your own specific SAX-based Reader class if you need to.

DOM4J is not part of standard JDK. You can download the DOM4J library from this link and add all the jar files to your classpath.

Java Program to parse XML document using DOM4J parser

Below is the input xml file we will be parsing using DOM4J parser.

DOM4JParserExample.java

Below is the output of running the above program.

Create XML document using DOM4J parser in Java

Here is the input xml file we will be creating using DOM4J parser.

Dom4jParserCreateXml.java

Below is the output printed to the console.

and here is the screenshot of the file created in D drive.

dom4j parser in java

Modify XML document using DOM4J parser in Java

We will use the below XML file to modify using DOM4J parser.

Dom4jParserModifyXml.java

Below is the output of above program.

Using XPath

XPath is is one of the most useful features of dom4j. You can use it to retrieve nodes from any location as well as evaluating complex expressions.  Below is an example Java program which uses XPath expression to select nodes in an XML document.

We will parse the below XML file.

Dom4jXPathExample.java

Below is the output of running the above program.

Hope you liked the Introduction to DOM4J Parser in Java with example programs. If you have any comments, post it in the comments section.

Additional Resources:

DOM4J Cookbook

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.
One comment

Add Comment

Required fields are marked *. Your email address will not be published.