Saturday, October 27, 2012

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.


<?xml version="1.0" encoding="UTF-8"?>
<books>
        <book ISBN="1234">
                <author>James</author>
                <title>Few words</title>
                <pages>120</pages>
                <language>English</language>
        </book>
        <book ISBN="5678">
                <author>Peter</author>
                <title>Around the World</title>
                <pages>200</pages>
                <language>English</language>
        </book>
</books>

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) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
             
                //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();
                while(itr.hasNext()){
                        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"));
                }
        }
}

Output:
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.

Other Java Tutorials you may like

No comments:

Post a Comment

Java67 Headline Animator