java - 要映射的 JAXB 注释类
问题描述
我有这样的课
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;
}
}
推荐阅读
- momentjs - 使用 moment.js 验证时间
- python - 如何创建命令并将其添加到 python 存储库中的路径
- android - 我们可以单击自定义列表视图项目并在 App Widget 上执行不同的操作吗
- node.js - BadRequestError: checks.state 参数丢失
- c++ - 二叉树中的递归搜索
- python - Pygame在进行计算时没有响应
- linux - OWASP ZAP 错误:“现在有太多活动连接”[Docker Image]
- ios - Google Cloud Firebase:[[GoogleDataTransport][I-GDTCOR001006]] 将新计数器值保存到磁盘时出错
- java - 如果缓冲区未满,我有许多分析调用要发送如何缓冲它们并在一定时间内刷新
- python - 实现一维卡尔曼滤波器/平滑 Python