Search code examples
javaxmljaxbmappingdozer

Unmarshalling Dozer mapping files to provide a mapping library


I'm trying to unmarshall some dozer mapping files in order to provide a mapping availability library to a number of applications. But i cant get the JaxB annotations to work correctly. Either the list of mappings us unmarshalled as null or empty

From the mapping file, all i'm interested in is.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<mappings>
    <mapping>
        <class-a>package.MySourceClass</class-a>
        <class-b>other.package.DestinationClass</class-b>
    </mapping>
</mappings>

I have a mappings class

@XmlRootElement(name="mappings")
@XmlAccessorType(XmlAccessType.FIELD)
public class Mappings {

    @XmlElementWrapper(name="mappings")
    private List<Mapping> mappingEntries = null;

//Getters and setters omitted

and A mapping class

@XmlRootElement(name="mapping")
@XmlAccessorType(XmlAccessType.FIELD)
public class Mapping {


    @XmlElement(name ="class-a")
    private String classA;

    @XmlElement(name = "class-b")
    private String classB;

I've tried numerous combinations of the annotations and I cant figure out what i'm doing wrong.

Can someone point me in the right direction.


Solution

  • You could do the following:

    Mappings

    package forum11193953;
    
    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name="mappings") // Match the root element "mappings"
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Mappings {
    
        @XmlElement(name="mapping") // There will be a "mapping" element for each item.
        private List<Mapping> mappingEntries = null;
    
    }
    

    Mapping

    package forum11193953;
    
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Mapping {
    
    
        @XmlElement(name ="class-a")
        private String classA;
    
        @XmlElement(name = "class-b")
        private String classB;
    
    }
    

    Demo

    package forum11193953;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Mappings.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml= new File("src/forum11193953/input.xml");
            Mappings mappings = (Mappings) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(mappings, System.out);
        }
    
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <mappings>
        <mapping>
            <class-a>package.MySourceClass</class-a>
            <class-b>other.package.DestinationClass</class-b>
        </mapping>
    </mappings>