2011-10-17 69 views
2

我有一個類世界包含人類地圖。如果我編組世界我得到以下輸出:在Jaxb中編組地圖的問題

<world> 
    <humans> 
     <human key="2"> 
      <value> 
       <name>Tom</name> 
      </value> 
     </human> 
     <human key="1"> 
      <value> 
       <name>Max</name> 
      </value> 
     </human> 
    </humans> 
</world> 

但我不想顯示「價值」 - 標籤。它應該看起來像:

<world> 
    <humans> 
     <human key="2"> 
      <name>Tom</name> 
     </human> 
     <human key="1"> 
      <name>Max</name> 
     </human> 
    </humans> 
</world> 

這可能嗎?

這裏是一流的世界和人類的代碼:

@XmlRootElement 
public class Human { 

    @XmlElement 
    private String name; 

    public Human(){} 
} 

@XmlRootElement 
public class World { 

    private Map<String, Human> humans = new HashMap<String, Human>(); 

    public World(){} 

    @XmlElementWrapper(name = "humans") 
    @XmlElement(name = "human") 
    public HumanEntry[] getMap() { 
     List<HumanEntry> list = new ArrayList<HumanEntry>(); 
     for (Entry<String, Human> entry : humans.entrySet()) { 
      HumanEntry mapEntry =new HumanEntry(); 
      mapEntry.key = entry.getKey(); 
      mapEntry.value = entry.getValue(); 
      list.add(mapEntry); 
     } 
     return list.toArray(new HumanEntry[list.size()]); 
    } 

    public void setMap(HumanEntry[] arr) { 
     for(HumanEntry entry : arr) { 
      this.humans.put(entry.key, entry.value); 
     } 
    } 

    public static class HumanEntry { 
     @XmlAttribute 
     public String key; 

     @XmlElement 
     public Human value; 
    } 

    public void addHuman(String key, Human human){ 
     this.humans.put(key, human); 
    } 
} 

回答

1
+0

不幸@XmlValue需要引用映射在XML文本一個Java類型。所以我不能使用它,特別是如果人類變得更復雜 –

+0

你是對的「如果字段或屬性的類型不是集合類型,那麼類型必須映射到XML模式簡單類型 」,那麼我猜您唯一的選擇是創建一個類型適配器http://jaxb.java.net/tutorial/section_6_2_9-Type-Adapters-XmlJavaTypeAdapter.html#Type%20Adapters:%20XmlJavaTypeAdapter – ilcavero

+2

在HumanEntry上使用@XmlJavaTypeAdapter(HumanAdapter.class)並創建一個適配器,將人類進入人體,反之亦然,從HumanEntry中刪除所有其他JaxB註釋 – ilcavero

2

我的解決方案

我添加鍵屬性到人類,然後我轉換地圖以數組:

@XmlRootElement 
public class World { 

    @XmlJavaTypeAdapter(HumanAdapter.class) 
    private Map<String, Human> humans = new HashMap<String, Human>(); 

    public World(){} 
} 

public class Human { 

    @XmlElement 
    private String name; 

    @XmlAttribute(name="key") 
    private String key; 

    public Human(){} 
} 

public class HumanAdapter extends XmlAdapter<HumanJaxbCrutch, Map<String, Human>> { 

    @Override 
    public HumanJaxbCrutch marshal(Map<String, Human> humans) throws Exception { 
     List<Human> list = new ArrayList<Human>(); 
     for (Entry<String, Human> entry : humans.entrySet()) { 
      list.add(entry.getValue()); 
     } 

     HumanJaxbCrutch humanJaxbCrutch = new HumanJaxbCrutch(); 
     humanJaxbCrutch.setCourses(list.toArray(new Human[list.size()])); 

     return humanJaxbCrutch; 
    } 

    @Override 
    public Map<String, Human> unmarshal(HumanJaxbCrutch humans) throws Exception { 
     Map<String, Human> map = new HashMap<String, Human>(); 

     for(Human human : humans.getCourses()){ 
      map.put(human.getKey(), human); 
     } 

     return map; 
    } 
} 

class HumanJaxbCrutch { 

    private Human[] courses; 

    @XmlElement(name = "human") 
    public Human[] getCourses() { 
     return courses; 
    } 

    public void setCourses(Human[] courses) { 
     this.courses = courses; 
    } 
}