Using JDOM to map an xml to a POJO bean

I am using the same xml I used earlier in the blog to map it to the same POJO class I used earlier but this time using a DOM parser. Refer to http://cuppajavamattiz.com/2009/03/19/using-a-sax-parser-to-map-an-xml-to-a-pojo-bean/ for pojo code and source xml.

JDOM has a good API that helps you do lot of stuff on XML files.

Download jdom from the web and remember to put the following jars on your classpath:
xml-apis.jar
xerces.jar
xalan.jar
saxpath.jar
jdom.jar
jaxen-jdom.jar
jaxen-core.jar
These jars are available in the jdom installation.

ElementMapper.java

package com.mattiz.dom.reader;
 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
 
public class ElementMapper {
    Document doc;
 
    SAXBuilder builder;
 
    public ElementMapper() throws Exception {
        try {
            builder = new SAXBuilder();
            // disabling xml validation
            builder.setValidation(false);
            builder.setIgnoringElementContentWhitespace(true);
            doc = builder.build(new File("mapper.xml"));
        } catch (JDOMException e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }
 
    private ArrayList getXMLData() throws Exception {
        ArrayList elementsVOArrList = new ArrayList();
        try {
            List ElementSetList = ((List) XPath
                    .selectNodes(doc, "//ElementSet"));
            for (int i = 0; i < ElementSetList.size(); i++) {
                Element elementNode = (Element) ElementSetList.get(i);
                // get children of this parent
                List elementList = (elementNode)
                        .getChildren("Element");
                // iterate over list of list
                ElementsVO elementsVO = null;
                for (Element element : elementList) {
                    elementsVO = new ElementsVO();
                    elementsVO.setGroup(elementNode.getAttributeValue("group"));
                    // get ChemicalSymbol child
                    Element ChemicalSymbolSibling = element
                            .getChild("ChemicalSymbol");
                    elementsVO.setLatinName(ChemicalSymbolSibling.getAttribute(
                            "latin").getValue());
                    // get value of ChemicalSymbol element
                    String chemicalSymbol = ((Element) ChemicalSymbolSibling)
                            .getValue();
                    // get ChemicalName child
                    Element ChemicalNameSibling = element
                            .getChild("ChemicalName");
                    // get value of ChemicalName element
                    String chemicalName = ((Element) ChemicalNameSibling)
                            .getValue();
                    // setting to valueobject
                    elementsVO.setChemicalSymbol(chemicalSymbol);
                    elementsVO.setChemicalName(chemicalName);
                    // add to arraylist
                    elementsVOArrList.add(elementsVO);
                }// for
            }// for
        } catch (Exception ex) {
            throw new Exception();
        }
        return elementsVOArrList;
    }
 
    public static void main(String args[]) {
        try {
            ElementMapper elmapper = new ElementMapper();
            List elementsList = elmapper.getXMLData();
            for (ElementsVO vo : elementsList) {
                System.out.print("  GROUP IS " + vo.getGroup());
                System.out.print("  CHEMICAL NAME IS " + vo.getChemicalName());
                System.out.print("  CHEMICAL SYMBOL IS "
                        + vo.getChemicalSymbol());
                System.out.println(" LATIN NAME IS " + vo.getLatinName());
                System.out.println("************");
            }
        } catch (Exception e) {
        }
    }
}

In both the parser examples the output is similar:

GROUP IS Reactive Elements  CHEMICAL NAME IS Sodium  CHEMICAL SYMBOL IS Na LATIN NAME IS Natrium
************
GROUP IS Reactive Elements  CHEMICAL NAME IS Potassium  CHEMICAL SYMBOL IS K LATIN NAME IS Kalium
************
GROUP IS Metals  CHEMICAL NAME IS Antimony  CHEMICAL SYMBOL IS Sb LATIN NAME IS Stibium
************
GROUP IS Metals  CHEMICAL NAME IS Copper  CHEMICAL SYMBOL IS Cu LATIN NAME IS Cuprum
************
GROUP IS Metals  CHEMICAL NAME IS Tin  CHEMICAL SYMBOL IS Sn LATIN NAME IS Stannum
************

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: