如何将 HashMap JAXB 输出优化为 JSF 语法
How to optimize HashMap JAXB output as JSF syntax
有了这个 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>
如何删除事件标签?
@XmlPath(".")
删除 "events" 地图包装器
@XmlPath(".")
@XmlJavaTypeAdapter(value=EventAdapter.class)
public Map<Event, String> getEvents() {
return events;
}
有了这个 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>
如何删除事件标签?
@XmlPath(".")
删除 "events" 地图包装器
@XmlPath(".")
@XmlJavaTypeAdapter(value=EventAdapter.class)
public Map<Event, String> getEvents() {
return events;
}