JAXB注释的类要映射



我有一个类似这样的类

public class Item {
    @XmlElement(name = "my_id")
    private String id;
    @XmlElement(name = "my_type")
    private String type;
}

我想将此类转换为考虑JAXB注释字段的地图。

例如。结果是一张带有以下条目的地图:

键:my_id,值:" ID"

键:my_type,值:"类型"

我不确定您的XML外观如何,但假设它将是一个项目元素,在某些父母的情况下,您可以使用Adadapter(XmlJavaTypeAdapter)进行操作。示例代码看起来如下:

package test;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlAccessorType(XmlAccessType.FIELD)
public class Item {
    public Item() {
    }
    @XmlElement(name = "my_id")
    private String id;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    @XmlElement(name = "my_type")
    private String type;
    public String toString() {
        return "Item : id-" + getId() + ", type -" + getType();
    }
    public static void main(String[] args) throws Exception {
        String xmlString = "<items>n"
                + "    <item>n"
                + "        <my_id>someID</my_id>n"
                + "        <my_type>someType</my_type>n"
                + "    </item>n"
                + "</items>";
        //System.out.println("xmlString.." + xmlString);
        RootElement o = unmarshal(RootElement.class, xmlString);
        System.out.println("item Map : "+o.getItem());
    }
    private static <C> C unmarshal(Class<C> c, String sampleXML) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StringReader reader = new StringReader(sampleXML);
        //System.out.println("" + sampleXML);
        return (C) unmarshaller.unmarshal(reader);
    }
}
@XmlRootElement(name = "items")
@XmlAccessorType(XmlAccessType.FIELD)
class RootElement {
    public RootElement() {
        System.out.println("RootElement");
    }
    public Map<String, String> getItem() {
        return item;
    }
    public void setItem(Map<String, String> item) {
        this.item = item;
    }
    @XmlJavaTypeAdapter(ItemAdapter.class)
    @XmlElement()
    private Map<String, String> item;
}
class ItemAdapter extends XmlAdapter<Item, Map<String, String>> {
    @Override
    public Map<String, String> unmarshal(Item i) throws Exception {
        Map<String, String> r = new HashMap<String, String>();
        r.put("my_id", i.getId());
        r.put("my_type", i.getType());
        return r;
    }
    @Override
    public Item marshal(Map<String, String> v) throws Exception {
        Item i = new Item();
        i.setId(v.get("my_id"));
        i.setType(v.get("my_type"));
        return i;
    }
}

最新更新