0

Having this JAXB Moxy definition, i want to optimize the HashMap output

@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;
    }
}

My current output is :

<tab>
    <styles>
        <html width="100px" />
        <print width="100px" />
    </styles>
    <elements>
        <field id="code"/>
        <field id="code2"/>
        <listing>
             ....
        </listing>
    </elements>
</tab>

Could i have this optimized output? (even without the "e:" and "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>

OK now i have

@XmlJavaTypeAdapter(value=EventAdapter.class)   
public Map<Event, String> getEvents() {     
    return events;
}

with

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;
    }       
}

the output is also

<tab>
    <html/>
    <print/>  
    <events>
         <onCellEnter>aa</onCellEnter>
    </events>
</tab>

how to remove the events tag?

Nassim MOUALEK
  • 4,702
  • 4
  • 25
  • 44

1 Answers1

0

@XmlPath(".") remove "events" map wrapper

@XmlPath(".")
@XmlJavaTypeAdapter(value=EventAdapter.class)   
public Map<Event, String> getEvents() { 
    return events;
}
Nassim MOUALEK
  • 4,702
  • 4
  • 25
  • 44