[英]how to unmarshal unwrapped collections to map with jaxb
我有一個像這樣的xml文件
<info>
<item key=1>value1</item>
<item key=2>value2</item>
</info>
我想得到一個像這樣的綁定類
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...
}
它與包裹的領域很有趣
<info>
<map>
<item key=1>value1</item>
<item key=2>value2</item>
</map>
</info>
當我擺脫<map>
,它失敗了沒有錯誤。 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始終為null。
我該怎么辦這個xml?
您的Info
是Map
的裝飾者。 在您的示例中,它不提供地圖上的任何值。 我看到兩種選擇:
刪除Info
,移動map
以替換info
使用。
為Info
而不是地圖編寫@XmlJavaTypeAdapter
。 讓它整理/解組內部map
- 你已經在做什么,只需將它向上移動一級。
我的解決方案是
完整的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
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);
}
}
版畫
<?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
標識地圖成員。 地圖成員上的@XmlValue注釋可能有效。
注意:我是EclipseLink JAXB(MOXy)的負責人,也是JAXB(JSR-222)專家組的成員。
您可以利用MOXy的@XmlPath
擴展來支持您的用例。
信息
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
要將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
演示
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中/輸出
<?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.