Saturday, July 26, 2014

JDOM (Java based Document Object Model)



In previous post, you have learnt about SAX and DOM. JDOM is related to DOM, but JDOM is very efficient in reading, manipulating and writing than DOM. This is very simple to implement.


What is JDOM ?



JDOM is simplistic method to parse XML files than DOM. This is specially designed for Java (Java based DOM), so it has many features that can be used with Java. You have learnt something called node in DOM. JDOM is not talking about node. They are referenced in their own types. In JDOM,

  • XML element is an instance of element
  • XML attribute is an instance of attribute.
  • XML document itself is an instance of document.

Advantages of JDOM


  • Lightweight and fast.
  • More efficient than SAX or DOM.
  • Implementation is easy.
  • Can be integrated with SAX or DOM.


How to add JDOM Library ?


JDOM is not provided in JDK like SAX or DOM. You need to download the latest JDOM library from www.jdom.org


Right click on Libraries and click Add Library


Provide a name for JDOM library

Click Add JAR/Folder


Browse jdom.jar file and add it


Finally you have done adding JDOM library


How to use JDOM ?

Students.xml


<?xml version="1.0" encoding="UTF-8"?>
<school>   
  <student id="1">
    <firstname>John</firstname>
    <lastname>Lodne</lastname>
    <age>20</age>
    <city>A</city>
  </student>
  <student id="2">
    <firstname>Ann</firstname>
    <lastname>Linda</lastname>
    <age>23</age>
    <city>B</city>
  </student>
</school>



JDOMTest.java


package JD;

import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
 
public class JDOMTest {
  public static void main(String[] args) {
    Scanner scn = new Scanner(System.in); //Create Scanner object to get file path as a String
    SAXBuilder builder = new SAXBuilder();//Create SAX builder object

    System.out.print("Enter the path of xml file: ");
    String fileName = scn.nextLine();

    try {
      Document documentObj = (Document) builder.build(fileName);//Convert file to document object
      Element rootNode = documentObj.getRootElement();//Get the root element
      List list = rootNode.getChildren("student");//List down all xml elements

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

        System.out.println(" ");
        System.out.println("First name : " + node.getChildText("firstname"));
        System.out.println("Last name : " + node.getChildText("lastname"));
        System.out.println("Age : " + node.getChildText("age"));
        System.out.println("City : " + node.getChildText("city"));
        System.out.println("---------------------");
      }
    }catch (IOException io) {
       System.out.println(io.getMessage());
    }catch (JDOMException jdomex) {
       System.out.println(jdomex.getMessage());
    }
  }
}



Display


There is a link to download the NetBeans project file.