JAXB是否支持对ArrayList对象的ArrayList进行编组

时间:2022-10-29 19:38:10

In our java application I need to save multiple set of values provided by user. To handle this data I am using ArrayList < ArrayList < Object > >. When I send this data to JAXB marshaller it is writing my XMLElementWrapper and XMLElement names but its not writing the content of the each XMLElement. In this each XMLElement should be inner ArrayList. When I Unmarshall, I get the same size of ArrayList but the inner ArrayList contents are empty with size of zero.

在我们的java应用程序中,我需要保存用户提供的多组值。为了处理这些数据,我使用的是ArrayList >。当我将这些数据发送给JAXB marshaller时,它正在编写我的XMLElementWrapper和XMLElement名称,但它没有写出每个XMLElement的内容。在这个每个XMLElement应该是内部ArrayList。当我解组时,我得到相同大小的ArrayList,但内部ArrayList内容为空,大小为零。

We have another single ArrayList < Object > implementaion in the same class which is working as expected while marshaling and Unmarshalling.

我们在同一个类中有另一个单独的ArrayList 实现,它在编组和解组时按预期工作。

Where could I be going wrong in case when marshalling object of type ArrayList < ArrayList < Object > > ?

在编组ArrayList >>类型的对象时,我可能会出错?

2 个解决方案

#1


0  

I think if your main variable is a List that list could contain objects which are of type ArrayList themselves. So you may not need to define it as ArrayList, because you can define the same structure as ArrayList.

我想如果你的主变量是一个列表,列表可以包含ArrayList类型的对象。因此您可能不需要将其定义为ArrayList,因为您可以定义与ArrayList相同的结构。

Another option you may try is to make another class that basically contains a list. Then your main class could just be an ArrayList, then YourClass could actually contain it's own list.

您可以尝试的另一个选项是创建另一个基本上包含列表的类。然后你的主类可能只是一个ArrayList,那么YourClass实际上可以包含它自己的列表。

I would also recommend using List for this instead of ArrayList. When I first started Java I was using ArrayList also, but you want to use the more generic List interface. ArrayList and the other types implement the List interface, so you want to define your variables as List incase the type underneath ever changes.

我还建议使用List代替ArrayList。当我第一次启动Java时,我也使用了ArrayList,但是你想使用更通用的List接口。 ArrayList和其他类型实现List接口,因此您希望将变量定义为List incase下面的类型。

#2


0  

@XmlJavaTypeAdapter(IdAdapter.class)
private ArrayList<ArrayList<String>> _dataSet = null;

private static class IdAdapter extends XmlAdapter<RowList, ArrayList<ArrayList<String>>>
{

    @Override
    public ArrayList<ArrayList<String>> unmarshal(RowList v)
            throws Exception
    {
        ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
        for(ColumnList myList:v.rowList)
        {
            list.add(myList.columnList);
        }
        return list;
    }

    @Override
    public RowList marshal(ArrayList<ArrayList<String>> v)
            throws Exception
    {
        RowList rowList = new RowList();
        for(ArrayList<String> innerList:v)
        {
            ColumnList myList = new ColumnList();
            myList.columnList = innerList;
            rowList.rowList.add(myList);
        }
        return rowList;
    }
}

@XmlRootElement(name="RowList")
private static class RowList
{
    @XmlElement(name="RowValues")
    ArrayList<ColumnList> rowList = new ArrayList<SpoolDataSet.ColumnList>();
}

@XmlRootElement(name="ColumnList")
private static class ColumnList 
{
    @XmlElement(name="ColumnValues")
    ArrayList<String> columnList = new ArrayList<String>();
}

I had done this for ArrayList> you can change the string to object as well.

我已经为ArrayList做了这个>你也可以将字符串更改为对象。

#1


0  

I think if your main variable is a List that list could contain objects which are of type ArrayList themselves. So you may not need to define it as ArrayList, because you can define the same structure as ArrayList.

我想如果你的主变量是一个列表,列表可以包含ArrayList类型的对象。因此您可能不需要将其定义为ArrayList,因为您可以定义与ArrayList相同的结构。

Another option you may try is to make another class that basically contains a list. Then your main class could just be an ArrayList, then YourClass could actually contain it's own list.

您可以尝试的另一个选项是创建另一个基本上包含列表的类。然后你的主类可能只是一个ArrayList,那么YourClass实际上可以包含它自己的列表。

I would also recommend using List for this instead of ArrayList. When I first started Java I was using ArrayList also, but you want to use the more generic List interface. ArrayList and the other types implement the List interface, so you want to define your variables as List incase the type underneath ever changes.

我还建议使用List代替ArrayList。当我第一次启动Java时,我也使用了ArrayList,但是你想使用更通用的List接口。 ArrayList和其他类型实现List接口,因此您希望将变量定义为List incase下面的类型。

#2


0  

@XmlJavaTypeAdapter(IdAdapter.class)
private ArrayList<ArrayList<String>> _dataSet = null;

private static class IdAdapter extends XmlAdapter<RowList, ArrayList<ArrayList<String>>>
{

    @Override
    public ArrayList<ArrayList<String>> unmarshal(RowList v)
            throws Exception
    {
        ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
        for(ColumnList myList:v.rowList)
        {
            list.add(myList.columnList);
        }
        return list;
    }

    @Override
    public RowList marshal(ArrayList<ArrayList<String>> v)
            throws Exception
    {
        RowList rowList = new RowList();
        for(ArrayList<String> innerList:v)
        {
            ColumnList myList = new ColumnList();
            myList.columnList = innerList;
            rowList.rowList.add(myList);
        }
        return rowList;
    }
}

@XmlRootElement(name="RowList")
private static class RowList
{
    @XmlElement(name="RowValues")
    ArrayList<ColumnList> rowList = new ArrayList<SpoolDataSet.ColumnList>();
}

@XmlRootElement(name="ColumnList")
private static class ColumnList 
{
    @XmlElement(name="ColumnValues")
    ArrayList<String> columnList = new ArrayList<String>();
}

I had done this for ArrayList> you can change the string to object as well.

我已经为ArrayList做了这个>你也可以将字符串更改为对象。