Introduction to JDOM Parser in Java with example

Introduction to JDOM Parser in Java with example

JDOM is an open source Java API which provides classes and methods to represent an XML document in a more easy way to Java developers. JDOM is lightweight and fast, Java optimized and uses Java collections. It is an alternative to DOM and SAX parser, but it also works well with DOM and SAX APIs. Actually it combines the best of both the world and is nearly as fast as SAX parser. This post gives an Introduction to JDOM Parser in Java with example programs.

Important JDOM Classes

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

  • DOMBuilder – Builds a JDOM Document using DOM parser
  • SAXBuilder   This class builds a JDOM Document using a SAX parser
  • Document – Represents the entire 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. It has methods that allow the user to get and manipulate its child elements and content, directly access the element’s textual content, manipulate its attributes, and manage namespaces.
  • Attribute – Represents an attribute of an element. It has methods that allow the user to obtain the value of the attribute as well as namespace and type information.
  • Text – An XML character sequence. Represents the text of XML tag.
  • Comment – Represents the comments in a XML document.

 

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

Java Program to parse XML document using JDOM parser

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

 JDomParserExample.java

Below is the output of running the above program.

You can see that the above program is clearly Java developer friendly. The API provides a list of elements which we can iterate using enhanced for loop.

Create XML document using JDOM parser in Java

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

JDomParserCreateXml.java

Below is the output printed to the console.

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

jdom parser in java

Modify XML document using JDOM parser in Java

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

JDomParserModifyXml.java

Below is the output of above program.

Sorting XML elements using JDOM parser in Java

In this program we will sort elements in the below xml file based on the employee name.

JDomParserSortXml.java

Below is the output of above program. Note the sorting of employee elements based first and last name.

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

Further Reading:

Java/XML integration with JDOM

 

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.

Add Comment

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