Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Monday, January 1, 2024

XML to Java Class Example

Have you got a task to convert XML data to Java objects? Worry no more. Java 1.8 has got you covered.

For this example, we are going to use a subset of XML data from dictionary of medicines and devices. Making this as close as possible to real world use. The XML Schema Definition describes the structure of an XML document. The XML document is the data we are going to convert into Java objects. The files are:

  • f_vtm2_3301123.xml
  • vtm_v2_3.xsd

New Java Project

I'm using Spring Tool Suite 4 and Java 1.8 for this project. You can use whatever IDE you like. Create a new project like so:

XSD to Java Types

I've placed the above mentioned XML and XSD files under the data folder. I'm on Windows 10 so on the Command Prompt I go to the data folder. I have JDK configured on my PATH which makes me do the xjc command. For more information, run xjc -help. Below I have specified the -p option which specifies the target package.


D:\xml-to-java\data>xjc -p com.blogspot.jpllosa vtm_v2_3.xsd
parsing a schema...
compiling a schema...
com\blogspot\jpllosa\ObjectFactory.java
com\blogspot\jpllosa\VIRTUALTHERAPEUTICMOIETIES.java

A million thanks JDK for the xjc command. We now have Java classes created. Let's move the package to our src folder.

XML to Java

Let's create Main.java to drive the XML to Java object conversion. You should have something like below:


package com.blogspot.jpllosa;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class Main {
    public static void main(String[] args) throws JAXBException {
        System.out.println("Starting...");
        File file = new File("data/f_vtm2_3301123.xml");
        JAXBContext jcVtm = JAXBContext.newInstance(VIRTUALTHERAPEUTICMOIETIES.class);
        Unmarshaller unmarshaller = jcVtm.createUnmarshaller();
        VIRTUALTHERAPEUTICMOIETIES vtms = (VIRTUALTHERAPEUTICMOIETIES) unmarshaller.unmarshal(file);
        
        int size = vtms.getVTM().size();
        if (size > 0) {
            System.out.printf("Virtual Therapeutic Moieties: %,d \r\n", size);
            for (int i = 0; i < 5; i++) {
                System.out.println(vtms.getVTM().get(i).getNM());
            }
        }
    }
}

Pretty cool. In a few minutes we are able to read the XML document and map it to Java objects. As Java objects, we can now do whatever we like with it. The super simple steps are open the file, create a JAXB context then the unmarshaller does all the heavy lifting. For this example we just show the first 5 VTMs out of the 3,000 or so Virtual Therapeutic Moieties. Right click on the file then Run As Java Application. The output looks like below:


Starting...
Virtual Therapeutic Moieties: 3,117 
Acebutolol
Paracetamol
Acetazolamide
Abciximab
Acarbose

There you have it. A super quick example of converting XML data to Java objects. The complete project can be cloned from github.com/jpllosa/xml-to-java.