0

I have a Java Webservice that is using Jersey. Now i want to send a Map of Maps (Rows and Columns) to the Client.

Everything works fine when I leave it as it is. But then the XMLOutput looks like this...

data>
    <entry>
        <key>0</key>
        <value>
            <data>
                <entry>
                    <key>id</key>
                    <value>20</value>
                </entry>
                <entry>
                    <key>status</key>
                    <value>test</value>
                </entry>
                <entry>
                    <key>number</key>
                    <value>20</value>
                </entry>
                <entry>
                    <key>cars</key>
                    <value>3</value>
                </entry>

            </data>
        </value>
    </entry>
</data>

But I would like to have something like this...

 <data>
    <entry row=0>
        <column name=id>20</column>
        <column name=status>OK</column>
        <column name=number>1</column>
        <column name=cars>1</column>
    </entry>
    <entry row=1>
        <column name=id>21</column>
        <column name=status>OK</column>
        <column name=number>1</column>
        <column name=cars>2</column>
    </entry>
    <entry row=2>
        <column name=id>22</column>
        <column name=status>OK</column>
        <column name=number>1</column>
        <column name=cars>3</column>
    </entry>
</data>

To control the XML Output I want to use XmlAdapters but I don't get it to work.

Class that contains the rows:

public class CoDataList{

   public LinkedHashMap<Integer,  CoDataMap> data = new LinkedHashMap<Integer,CoDataMap>();

}

Class that contains the columns:

public class CoDataMap {

   public LinkedHashMap<String, String> data = new LinkedHashMap<String, String>();

}

How would I write the XmlAdapter and Types to marshal the objects properly?

I did something like this... but where would I add the next XmlAdapter?

MainClass that contains the rows and columns

@XmlRootElement
public class Response implements Cloneable{

    @XmlJavaTypeAdapter(CoDataListAdapter.class)
    public CoDataList data = new CoDataList();

}

Now I wrote the Adapters for the CoDataList and CoDataMap:

public class CoDataListAdapter extends XmlAdapter<CoDataListType, CoDataList>{

@Override
public CoDataListType marshal(CoDataList v) throws Exception {
    CoDataListType rows = new CoDataListType();
            for (int currentRow : v.data.keySet()){
        CoDataListEntryType row= new CoDataListEntryType();
            row.key = currentRow;
        row.map = v.data.get(currentRow);
        rows.entry.add(row);
    }
    return rows;
}

@Override
public CoDataList unmarshal(CoDataListType v) throws Exception {
    // TODO Auto-generated method stub
    return null;
}

}

public class CoDataMapAdapter extends XmlAdapter<CoDataMapType, CoDataMap> {

@Override
public CoDataMapType marshal(CoDataMap v) throws Exception {
    CoDataMapType columns = new CoDataMapType();
    for (String curCol : v.data.keySet()){
                    CoDataMapEntryType column= new CoDataMapEntryType();
        column.key = curCol;
        column.value = v.data.get(curCol);
        columns.entry.add(column);
    }
    // TODO Auto-generated method stub
    return columns;
}

@Override
public CoDataMap unmarshal(CoDataMapType v) throws Exception {
    //...
}
}

public class CoDataListType {
    public List<CoDataListEntryType> entry = new ArrayList<CoDataListEntryType>();
}

public class CoDataListEntryType {

@XmlAttribute(name="row")
    public Integer key; 

    @XmlValue
    @XmlJavaTypeAdapter(CoDataMapAdapter.class)
    public CoDataMap map;
}

At this point I am getting an exception:

@XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.xmladapter


public class CoDataMapType {
    public List<CoDataMapEntryType> entry = new ArrayList<CoDataMapEntryType>();
}

public class CoDataMapEntryType {
    @XmlAttribute(name="column")
    public String key; 

    @XmlValue
    public String value;
}

I would really appreciate your help, I don't know how to get it to work. Thank you

Pavel Bucek
  • 5,304
  • 27
  • 44
Chris
  • 566
  • 1
  • 8
  • 20

1 Answers1

0

I worked this out now:

The following classes need to be changed:

public class CoDataListAdapter extends XmlAdapter<CoDataListType, CoDataList<CoDataMap<String, String>>>{

@Override
public CoDataListType marshal(CoDataList<CoDataMap<String, String>> v) throws Exception {


    CoDataListType rows = new CoDataListType();

    for (int currentRow : v.keySet()){

        CoDataListEntryType row= new CoDataListEntryType();

        row.key = currentRow;

        for (String key : v.get(currentRow).keySet()){
            CoDataMapEntryType column = new CoDataMapEntryType();

            column.name = key;
            column.value = v.get(currentRow).get(key);

            row.column.add(column);
        }


        rows.row.add(row);


    }


    return rows;
}

@Override
public CoDataList<CoDataMap<String, String>> unmarshal(CoDataListType v) throws Exception {


    CoDataList<CoDataMap<String,String>> rows =  new CoDataList<CoDataMap<String,String>>();

    for (CoDataListEntryType row : v.row){


        CoDataMap<String, String> columns = new CoDataMap<String, String>();
        for (CoDataMapEntryType column : row.column){
            columns.put(column.name, column.value);
        }
        rows.put(row.key, columns);

    }




    // TODO Auto-generated method stub
    return rows;
}

}

public class CoDataListEntryType {

 @XmlAttribute(name="id")
 public Integer key; 

 @XmlElement
 public List<CoDataMapEntryType> column = new ArrayList<CoDataMapEntryType>();
}

Now I need to have only the one Adapter. The classes CoDataMapAdapter and CoDataMapType are unnecessary.

Chris
  • 566
  • 1
  • 8
  • 20