有了这个JAXB Moxy定义,我想优化HashMap输出
@XmlRootElement
public class ViewElement {
private Map<StlType, ElementStyle> styles;
@XmlElementWrapper <---- must be removed thanks to @lexicore
@XmlVariableNode("type")
public Map<StlType, ElementStyle> getStyles() {
return styles;
}
private List<ViewElement> elements;
@XmlElementWrapper <---- must be removed thanks to @lexicore
@XmlElementRefs({
.....
})
public List<ViewElement> getElements(){
return elements;
}
}
我目前的输出是:
<tab>
<styles>
<html width="100px" />
<print width="100px" />
</styles>
<elements>
<field id="code"/>
<field id="code2"/>
<listing>
....
</listing>
</elements>
</tab>
我可以拥有此优化输出吗? (即使没有“e:”和“s:”)
<e:tab>
<s:html width="100px" />
<s:print width="100px" />
<e:field id="code"/>
<e:field id="code2"/>
<e:listing>
....
</e:listing>
</e:tab>
现在好了
@XmlJavaTypeAdapter(value=EventAdapter.class)
public Map<Event, String> getEvents() {
return events;
}
带
public class EventAdapter extends XmlAdapter<EventAdapter.AdaptedMap, Map<Event, String>> {
public static class AdaptedMap {
@XmlVariableNode("key")
List<AdaptedEntry> entries = new ArrayList<AdaptedEntry>();
}
public static class AdaptedEntry {
@XmlTransient
public String key;
@XmlValue
public String value;
}
@Override
public AdaptedMap marshal(Map<Event, String> map) throws Exception {
if (map==null||map.size()==0)
return null;
AdaptedMap adaptedMap = new AdaptedMap();
for(Entry<Event, String> entry : map.entrySet()) {
AdaptedEntry adaptedEntry = new AdaptedEntry();
adaptedEntry.key = entry.getKey().toString();
adaptedEntry.value = entry.getValue();
adaptedMap.entries.add(adaptedEntry);
}
return adaptedMap;
}
@Override
public Map<Event, String> unmarshal(AdaptedMap adaptedMap) throws Exception {
if (adaptedMap==null)
return null;
List<AdaptedEntry> adaptedEntries = adaptedMap.entries;
Map<Event, String> map = new HashMap<Event, String>(adaptedEntries.size());
for(AdaptedEntry adaptedEntry : adaptedEntries) {
map.put( Event.valueOf(adaptedEntry.key), adaptedEntry.value );
}
return map;
}
}
输出也是
<tab>
<html/>
<print/>
<events>
<onCellEnter>aa</onCellEnter>
</events>
</tab>
如何删除事件标记?
答案 0 :(得分:0)
@XmlPath(".")
删除“events”地图包装
@XmlPath(".")
@XmlJavaTypeAdapter(value=EventAdapter.class)
public Map<Event, String> getEvents() {
return events;
}