JDOM Example : Reading and Parsing XML with SAX parser in Java

XML parsing with JDOM parser
JDOM is an open source library which allow XML parsing and reading in Java program. JDOM is designed by using Java programming technique and customized for Java programmers, so that Java programmer with  very little knowledge of XML documents can use JDOM to read XML files. Unlike DOM Parser of Java API , which uses Factory design pattern to create instance of parser e.g DocumentBuilderFactory and DocumentBuilder, as seen in our last example of parsing XML documents in Java, JDOM uses new() operator to create its parser instances. In fact JDOM is very easy to understand and most of the time its self explanatory. While trying this Java program to parse XML file, you can explore JDOM API. JDOM API also allows to use XPATH expression to query XML documents with package org.jdom2.xpath. In next section we will see complete code example of How to read an XML file using JDOM parser in Java program.

How to read XML file using JDOM in Java

How to read XML file using JDOM parser in Java exampleIn this code example we will use following XML file to demonstrate How JDOM read this XML file and provide access to root, various elements and attributes.

Here is our sample XML file:
<?xml version="1.0" encoding="UTF-8"?>
        <book ISBN="1234">
                <title>Few words</title>
        <book ISBN="5678">
                <title>Around the World</title>

and here is a Java program which reads this XML document using JDOM library :

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

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

 * Java program to read XML file using JDOM library.
 * JDOM is an open source library which is close to Java style
 * and allows programmer to read and write XML files in JAva
 * @author

public class XMLReader {

        public static void main(String args[]){
                //creating JDOM SAX parser
                SAXBuilder builder = new SAXBuilder();
                //reading XML document
                Document xml = null;
                try {
                        xml = builder.build(new File("test.xml"));
                } catch (JDOMException e) {
                } catch (IOException e) {
                //getting root element from XML document
                Element root = xml.getRootElement();
                System.out.println("Root element of XML document is : " + root.getName());
                System.out.println("Number of books in this XML : " + root.getChildren().size());
                List<Element> books = root.getChildren();
                //Iterating over all childs in XML
                Iterator<Element> itr = books.iterator();
                        Element book = itr.next();
                        //reading attribute from Element using JDOM
                        System.out.println("ISBN : " + book.getAttributeValue("ISBN"));
                        //reading value of childern in XML using JDOM
                        System.out.println("author : " + book.getChildText("author"));
                        System.out.println("title : " + book.getChildText("title"));
                        System.out.println("pages : " + book.getChildText("pages"));
                        System.out.println("language : " + book.getChildText("language"));

Root element of XML document is : books
Number of books in this XML : 2
ISBN : 1234
author : James
title : Few words
pages : 120
language : English
ISBN : 5678
author : Peter
title : Around the World
pages : 200
language : English

If you look at this example of parsing XML document using JDOM parser, you will find it surprisingly simple. you just need to create SAXBuilder and then you got Document object, from that you can get Element and correspondingly value of child and attributes.

That's all on How to read XML file using JDOM open source library. JDOM is very easy to use library for Java programmer and great for quickly reading XML documents in Java.

Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services and REST API with Spring Boot
Java Web Fundamentals

Other Java Tutorials you may like

1 comment:

  1. Hi,
    I used this code but I have this error:
    org.jdom2.input.JDOMParseException: Error on line 1 of document file:/C:/Documentos/Docs_Cia/DesarrolloEclipse/Xml/test.xml: Reference is not allowed in prolog.

    Help me, please.

    Thank you