繁体   English   中英

映射的JAXB注释类

[英]JAXB Annotated Class to Map

我有这样的课

public class Item {

    @XmlElement(name = "my_id")
    private String id;

    @XmlElement(name = "my_type")
    private String type;
}

我想将此类转换为考虑了jaxb注释字段的Map。

例如,结果是具有以下条目的地图:

关键字:my_id,值:“ id”

键:my_type,值:“类型”

我不确定您的xml看起来如何,但是假设它将是一个item元素,在某些父项下,您可以使用adapter( 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;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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