Raj
Raj

Reputation: 141

JAXB @XmlJavaTypeAdapter usage

I had been struggling to have @XMLJavaTypeAdapter to make it working as per my need. I could be wrong with my understanding. Please help me to understand in better way.

Requirement: For example: I have a transport XML with list of mappings objects as below:

<transport>
    <mappings>
        <product>XXX</product>
        <eventName>XXX</eventName>
        <destination>XXX</destination>
        <destinationType>XXX</destinationType>
    </mappings>
</transport>

Using JAXB, I am able to read XML as a List<Mappings> objects.

I would like to have my in-memory representation of List<Mappings> as a Map<String,Mappings>, where key in map is product:eventName and Use in my code Map instead of List<Mapping>.

I am bit confused with usage of @XmlJavaTypeAdapter and not able to figure out How can i achieve this.

Any help in this regard would be highly appreciated.

Thanks Raj

Upvotes: 4

Views: 17169

Answers (1)

bdoughan
bdoughan

Reputation: 149007

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

You could leverage the @XmlPath extension in MOXy to do the following:

Transport

The Transport class makes use of the @XmlPath extension. Without @XmlPatha grouping element called mappings will be added to the document.

package forum8403623;

import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Transport {

    @XmlJavaTypeAdapter(MappingsAdapter.class)
    @XmlPath(".")
    private Map<String, Mapping> mappings = new HashMap<String, Mapping>();

}

Mapping

package forum8403623;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"product", "eventName", "destination", "destinationType"})
public class Mapping {

    String product;
    String eventName;
    String destination;
    String destinationType;

}

MappingsAdapter

The following class is responsible for converting to/from the Map<String, Mapping> to the representation we will be using for the XML mapping.

package forum8403623;

import java.util.*;
import java.util.Map.Entry;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MappingsAdapter extends XmlAdapter<MappingsAdapter.AdaptedMap, Map<String, Mapping>>{

    @Override
    public Map<String, Mapping> unmarshal(AdaptedMap v) throws Exception {
        Map<String, Mapping> mappings = new HashMap<String, Mapping>();
        for(Mapping mapping : v.mappings) {
            mappings.put(mapping.product, mapping);
        }
        return mappings;
    }

    @Override
    public AdaptedMap marshal(Map<String, Mapping> v) throws Exception {
        AdaptedMap adaptedMap = new AdaptedMap();
        for(Entry<String,Mapping> entry : v.entrySet()) {
            adaptedMap.mappings.add(entry.getValue());
        }
        return adaptedMap;
    }

    static class AdaptedMap {
        public List<Mapping> mappings = new ArrayList<Mapping>();
    }
}

Demo

package forum8403623;

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum8403623/input.xml");
        Transport transport = (Transport) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(transport, System.out);
    }

}

jaxb.properties

To use MOXy as your JAXB provider you need to add a file called jaxb.properties in the same package as your domain model with the following entry:

javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory

For More Information

Upvotes: 7

Related Questions