[英]how to unmarshal unwrapped collections to map with jaxb
i have an xml file like this 我有一个像这样的xml文件
<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. 当我摆脱
<map>
,它失败了没有错误。 MapAdapter did not worked. MapAdapter没有用。
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. myMapType始终为null。
what can i do with this xml ? 我该怎么办这个xml?
Your Info
is a decorator for Map
. 您的
Info
是Map
的装饰者。 In your example it offers no value over a map. 在您的示例中,它不提供地图上的任何值。 I see two alternatives:
我看到两种选择:
Remove Info
, move your map
up to replace uses of info
. 删除
Info
,移动map
以替换info
使用。
Write your @XmlJavaTypeAdapter
for Info
instead of the map. 为
Info
而不是地图编写@XmlJavaTypeAdapter
。 Have it marshal/unmarshal the internal map
- what you're doing already, just move it up a level. 让它整理/解组内部
map
- 你已经在做什么,只需将它向上移动一级。
My solution is that 我的解决方案是
Full mavenized project is here http://code.google.com/p/jinahya/source/browse/trunk/com.googlecode.jinahya/stackoverflow 完整的mavenized项目在这里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.
地图成员上的@XmlValue注释可能有效。
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group. 注意:我是EclipseLink JAXB(MOXy)的负责人,也是JAXB(JSR-222)专家组的成员。
You could leverage MOXy's @XmlPath
extension to support your use case. 您可以利用MOXy的
@XmlPath
扩展来支持您的用例。
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 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 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 : 要将MOXy指定为JAXB提供程序,您需要在与域模型相同的包中包含一个名为
jaxb.properties
文件的文件,并带有以下条目(请参阅: 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 input.xml中/输出
<?xml version="1.0" encoding="UTF-8"?>
<info>
<item key="1">value1</item>
<item key="2">value2</item>
</info>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.