简体   繁体   中英

how to unmarshal unwrapped collections to map with jaxb

i have an xml file like this

<info>
  <item key=1>value1</item>
  <item key=2>value2</item>
</info>

and i wanna get a binded class like this

class Info {
    @XmlJavaTypeAdapter(MapAdapter.class)
    private Map<Integer,Item> map;

    public setMap...
    public getMap...
}

class Item{
    @XmlAttribute
    private Integer key;

    @XmlValue
    private String value;

    //get,set method...
}

it works fun with wrapped field

<info>
  <map>
    <item key=1>value1</item>
    <item key=2>value2</item>
  </map>
</info>

when i get rid of <map> , it failed with no error. MapAdapter did not worked.

public Map<Integer, Item> unmarshal(MapType myMapType) throws Exception {
    HashMap<Integer, Item> hashMap = new HashMap<Integer, Item>();
    for (Item myEntryType : myMapType.getEntry()) {
        hashMap.put(myEntryType.getKey(), myEntryType);
    }
    return hashMap;
}

myMapType always get null.

what can i do with this xml ?

Your Info is a decorator for Map . In your example it offers no value over a map. I see two alternatives:

  1. Remove Info , move your map up to replace uses of info .

  2. Write your @XmlJavaTypeAdapter for Info instead of the map. Have it marshal/unmarshal the internal map - what you're doing already, just move it up a level.

My solution is that

  1. Leave the map as @XmlTransient
  2. Create an another property

Full mavenized project is here http://code.google.com/p/jinahya/source/browse/trunk/com.googlecode.jinahya/stackoverflow

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Info {

    @XmlElement(name = "item")
    private List<Item> getItems() {
        return new ArrayList<Item>(getMap().values());
    }

    private void setItems(final List<Item> items) {
        getMap().clear();
        for (Item item : items) {
            getMap().put(item.getKey(), item);
        }
    }

    public Map<Integer, Item> getMap() {
        if (map == null) {
            map = new HashMap<Integer, Item>();
        }
        return map;
    }

    private Map<Integer, Item> map;
}

test

@Test
public void testXml() throws JAXBException {

    final JAXBContext context = JAXBContext.newInstance(Info.class);

    final Info marshall = new Info();
    marshall.getMap().put(1, Item.newInstance(1, "value1"));
    marshall.getMap().put(2, Item.newInstance(2, "value2"));

    final Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    marshaller.marshal(marshall, baos);
    System.out.println(new String(baos.toByteArray()));

    final Unmarshaller unmarshaller = context.createUnmarshaller();

    final Info unmarshal = (Info) unmarshaller.unmarshal(
        new ByteArrayInputStream(baos.toByteArray()));

    for (Item item : unmarshal.getMap().values()) {
        System.out.println(item);
    }
}

prints

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<info>
    <item key="1">value1</item>
    <item key="2">value2</item>
</info>

key=1&value=value1
key=2&value=value2

identifies the map member. @XmlValue annotation on the map member might work.

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

You could leverage MOXy's @XmlPath extension to support your use case.

Info

package forum11956071;

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

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class Info {
    @XmlJavaTypeAdapter(MapAdapter.class)
    @XmlPath(".")
    private Map<Integer,String> map;

}

MapAdapter

package forum11956071;

import java.util.*;
import java.util.Map.Entry;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MapAdapter extends XmlAdapter<MapAdapter.AdaptedMap, Map<Integer, String>>{

    public static class AdaptedMap {
        public List<Item> item = new ArrayList<Item>();
    }

    public static class Item {
        @XmlAttribute Integer key;
        @XmlValue String value;
    }
    @Override
    public AdaptedMap marshal(Map<Integer, String> map) throws Exception {
        AdaptedMap adaptedMap = new AdaptedMap();
        for(Entry<Integer, String> entry : map.entrySet()) {
            Item item = new Item();
            item.key = entry.getKey();
            item.value = entry.getValue();
            adaptedMap.item.add(item);
        }
        return adaptedMap;
    }

    @Override
    public Map<Integer, String> unmarshal(AdaptedMap adaptedMap) throws Exception {
        Map<Integer, String> map = new HashMap<Integer, String>();
        for(Item item : adaptedMap.item) {
            map.put(item.key, item.value);
        }
        return map;
    }

}

jaxb.properties

To specify MOXy as your JAXB provider you need to have a file called jaxb.properties file in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html :

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

Demo

package forum11956071;

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11956071/input.xml");
        Info info = (Info) unmarshaller.unmarshal(xml);

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

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8"?>
<info>
   <item key="1">value1</item>
   <item key="2">value2</item>
</info>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM