1

I have this classes structure to serialize TreeGrid (www.treegrid.com) object:

DataGrid.java

@XmlRootElement(name = "Grid")
public class DataGrid implements Serializable {

    private static final long serialVersionUID = 337286974296229101L;

    @XmlElement(name = "Body")
    public DataGridData data = new DataGridData();

    @XmlElement(name = "IO")
    public XmlAttributeHolder io = new XmlAttributeHolder();

    public DataGrid() {

    }
}

and

DataGridData.java

public class DataGridData {
    @XmlElement(name="B")
    public DataGridCurrentPage currentPage = new DataGridCurrentPage();

    @XmlElement(name="B")
    public List<XmlAttributeHolder> pageList = new ArrayList<XmlAttributeHolder>();
}

These classes will be processed to return an XML structure as follow:

<Grid>
    <Body>
        <B />
    </Body>
 </Grid>

but the information encapsuled in B can be different (so exist two properties in DataGridData class mapped by the same XmlElement).

If I run my project under Java 7 that's all OK but with Java 8 is raise an exception about conflict two properties can't use the same XmlElement map.

A possible solution is: Encapsule two properties in two different classes as follow:

DataGridData.java

public class DataGridData {
    private DataGridDataCP dataGridDataCP;
    private DataGridDataPL dataGridDataPL;

    public DataGridData() {
        this.dataGridDataCP = new DataGridDataCP();
        this.dataGridDataPL = new DataGridDataPL();
    }

    public DataGridDataCP getDataGridDataCP() {
        return dataGridDataCP;
    }

    public void setDataGridDataCP(DataGridDataCP dataGridDataCP) {
        this.dataGridDataCP = dataGridDataCP;
    }

    public DataGridDataPL getDataGridDataPL() {
        return dataGridDataPL;
    }

    public void setDataGridDataPL(DataGridDataPL dataGridDataPL) {
        this.dataGridDataPL = dataGridDataPL;
    }
}

DataGridDataCP.java

public class DataGridDataCP {

    private DataGridCurrentPage currentPage;

    public DataGridDataCP() {
        this.currentPage = new DataGridCurrentPage();
    }

    @XmlElement(name="B")
    public DataGridCurrentPage getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(DataGridCurrentPage currentPage) {
        this.currentPage = currentPage;
    }
}

DataGridDataPL.java

public class DataGridDataPL {

    private List<XmlAttributeHolder> pageList;

    public DataGridDataPL() {
        this.pageList = new ArrayList<XmlAttributeHolder>();
    }

    @XmlElement(name="B")
    public List<XmlAttributeHolder> getPageList() {
        return pageList;
    }

    public void setPageList(List<XmlAttributeHolder> pageList) {
        this.pageList = pageList;
    }
}

But in this way when DataGridData class has been serialized, add a tag <dataGridDataCP> (or <dataGridDataPL>) but I don't want to show this intermediate tag.

I've tried with XmlAccessorType annotation to exclude object DataGridDataCP / DataGridDataPL but this annotation exclude the complete object (with its encapsuled properties and no only the property in DataGridData class)

Joe Taras
  • 15,166
  • 7
  • 42
  • 55

1 Answers1

0

You can try something, but you will be able only to serialize properly. Deserialization will not work, and I will explain why. The code would look something like this:

@XmlAccessorType(XmlAccessType.FIELD)
public class DataGridData {

    @XmlElements({
            @XmlElement(name = "B", type = DataGridCurrentPage.class),
            @XmlElement(name = "B", type = XmlAttributeHolder.class),
    })
    List<Object> values;

}

Now if you create your POJOs and serialize you will get the XML you need. If this is your need then you should be fine.

But if you want also to deserialize, since both classes are mapped into @XmlElement with name 'B' it will pick one of the target classes for deserialization (I believe the last specified).

martidis
  • 2,897
  • 1
  • 11
  • 13