Mapping XML Entities to Java Objects

By | December 6, 2017
Questions:

I am quite sure, this is one of the many duplicated questions around XML to Java Object conversions.
But I started this thread since I couldn’t find simpler or looking for simpler solution.

I have an xsd [Infact I am designing it] and xml.
I would like to auto-map the xml data to Java beans as per mapping

<tns:SummaryCart xmlns:tns="SummaryCart" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="SummaryCart.xsd">
    <SummaryElement type="test">
        <order>1</order>
        <id>A</id>
        <displayName>A</displayName>
        <subElements>
            <order>1</order>
            <id>Preactivation</id>
            <displayName>Preactivation</displayName>
        </subElements>
        <maxlines>1</maxlines>
    </SummaryElement>
</tns:SummaryCart>

Now my Java classes will be

public class SummaryCart{
    private List<SummaryElement> summaryElementList;
}
public class SummaryElement {
    private int order;
    private String id;
    private String displayName;
    private String property;
    private List<SummaryElement> subElements;
    private int maxlines;
    private String type;
}

Is there any simple tool/framework which can auto-map the data from XML to Java beans [MUST support attributes/element mapping]. Tutorial will be good.

Btw, I am using Spring framework, if spring-oxm advantage is taken, its welcome.

Answers:

Below is how you could map your object to XML using JAXB (JSR-222). An implementation is included in the JDK/JRE starting with Java SE 6. JAXB is supported by Spring (see section 8.5: http://static.springsource.org/spring-ws/site/reference/html/oxm.html)

SummaryCart

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="SummaryCart", namespace="SummaryCart")
@XmlAccessorType(XmlAccessType.FIELD)
public class SummaryCart{

    @XmlElement(name="SummaryElement")
    private List<SummaryElement> summaryElementList;

}

SummaryElement

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class SummaryElement {

    private int order;
    private String id;
    private String displayName;
    private String property;
    private List<SummaryElement> subElements;
    private int maxlines;

    @XmlAttribute
    private String type;

}

Demo

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(SummaryCart.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum15881876/input.xml");
        SummaryCart sc = (SummaryCart) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "SummaryCart.xsd");
        marshaller.marshal(sc, System.out);
    }

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:SummaryCart xmlns:ns2="SummaryCart" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="SummaryCart.xsd">
    <SummaryElement type="test">
        <order>1</order>
        <id>A</id>
        <displayName>A</displayName>
        <subElements>
            <order>1</order>
            <id>Preactivation</id>
            <displayName>Preactivation</displayName>
            <maxlines>0</maxlines>
        </subElements>
        <maxlines>1</maxlines>
    </SummaryElement>
</ns2:SummaryCart>

Questions:
Answers:

Basically you want to unmarshal your XML. Here’s a detailed tutorial that describes how to use the JAXB xjc command to generate a Java class from XML Schema. A maven xjc plugin is also available for your convenience.

Leave a Reply

Your email address will not be published. Required fields are marked *