Java JDOM2 Tutorial

1- Instroduction

 JDOM is an in-memory XML model that can be used to read, write, create and modify XML Documents. JDOM is similar to DOM in that they both provide an in-memory XML document model, but while DOM is designed to work the same in multiple languages (C, C++, ECMAScript, Java, JScript, Lingo, PHP, PLSQL, and Python []), JDOM is designed only for Java and uses the natural Java-specific features that the DOM model avoids. For this reason JDOM intentionally does not follow the w3c DOM standard. JDOM is not an XML parser but it can use a SAX, StAX or DOM parser to build the JDOM document. JDOM versions since JDOM 2.0.0 (JDOM2) all use the native language features of Java6 and later like Generics, Enums, var-args, co-variant return types, etc.

In the tutorials listed below we provide you with examples of all key functionalities of JDOM2. However before you start seeing the examples, it would be a good idea to understand the main packages that form part of JDOM2 and the functionalities that each of the packages provide. The first tutorial in the series provides an introduction to the package structure along with important classes in each package. The subsequent tutorials provide examples for each of the key functionalities.

2- Library

Unlike DOM available in JDK , JDOM you have to download the library to use:
You can download the JDOM2 library at homepage:
Or simply download from Maven Repository:

3- Begin Example

This is the image after completion of the Project:
<?xml version="1.0"?>



package org.o7planning.tutorial.jdom2;

import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

public class ReadXMLFile {
   public static void main(String[] args) {

       SAXBuilder builder = new SAXBuilder();
       File xmlFile = new File("department-datas.xml");
       System.out.println("Parsing FILE: "+ xmlFile.getAbsolutePath());

       try {

           Document document = (Document);
           Element rootNode = document.getRootElement();
           List<Element> list = rootNode.getChildren("employee");

           for (int i = 0; i < list.size(); i++) {

               Element node = (Element) list.get(i);
               System.out.println(" ------ ");
               System.out.println("Emp No : " + node.getChildText("empNo"));
               System.out.println("Emp Name : " + node.getChildText("empName"));
               System.out.println("Hire Date : " + node.getChildText("hireDate"));
               System.out.println("Salary : " + node.getChildText("salary"));


       } catch (IOException e) {
       } catch (JDOMException e) {
Results after running class ReadXMLFile:

4- Comparison of some difference between JDOM and DOM