Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

JAXB marshal/unmarshal java.util.Map entries to XML attributes

I'm trying to accomplish marshaling/unmarshaling Map.Entry to XML element attributes. And can't find anything suited to my case on stackoverflow.

Here is my root element:

@JsonInclude(JsonInclude.Include.NON_NULL)
@XmlRootElement(name = "METADATA")
@XmlAccessorType(XmlAccessType.FIELD)
public class ExcelResponse {
    @JsonIgnore
    @XmlTransient
    private long startRow;

    @JsonIgnore
    @XmlTransient
    private long lastRow;

    @JsonIgnore
    @XmlTransient
    private long totalRow;

    @JsonProperty("fields")
    @XmlElementWrapper(name = "FIELDS")
    @XmlElement(name = "FIELD")
    private List<Field> fields;

    @JsonProperty("countRows")
    private int countRows;

    @JsonProperty("rows")
    @XmlElementWrapper(name = "ROWS")
    @XmlElement(name = "ROW")
    private List<Row> rows;

    ...
}

Sofar my Row class looks like:

@XmlAccessorType(XmlAccessType.FIELD)
public class Row {

    /**
     * Мапа с данными
     */
    @XmlJavaTypeAdapter(MapAdapter.class)
    private HashMap<String, String> data;

    ...getters/setters

    static class MapElements {

        @XmlAttribute
        public String key;

        @XmlAttribute
        public String value;

        private MapElements() {}

        public MapElements(String key, String value) {
            this.key = key;
            this.value = value;
        }
    }

    public static class MapAdapter extends XmlAdapter<MapElements[], Map<String, String>> {
        public MapAdapter() {
        }

        public MapElements[] marshal(Map<String, String> map) throws Exception {
            MapElements[] mapElements = new MapElements[map.size()];
            int i = 0;
            for (Map.Entry<String, String> entry : map.entrySet())
                mapElements[i++] = new MapElements(entry.getKey(), entry.getValue());

            return mapElements;
        }

        public Map<String, String> unmarshal(MapElements[] map) throws Exception {
            Map<String, String> r = new HashMap<String, String>();
            for (MapElements element : map)
                r.put(element.key, element.value);
            return r;
        }
    }
}

And what I recive in responce is:

<METADATA>
    <FIELDS>...</FIELDS>
    <countRows>0</countRows>
    <ROWS>
        <ROW>
            <data>
                <item key="F10" value="20180613T15:13:24000"/>
                <item key="F12" value="3"/>
                <item key="F13"/>
                ...
            </data>
        </ROW>
        ... more rows
     </ROWS>
</METADATA>

My goal is to marshal/unmarshal Map.Enty Set elements to a single Document attributes and recive document with structure like this:

<METADATA>
    <FIELDS>...</FIELDS>
    <countRows>0</countRows>
    <ROWS>
        <ROW F10="20180613T15:13:24000" F12="3" "F13"=""/>
        ... more rows
     </ROWS>
</METADATA>

Is there any way to accomplish this?

Comments