I am trying to output to XML a table bean with a table name, row count and a list of columns. If I annotate them like attributes, they show:
So this definition:
@XmlRootElement(name = "table")
public class Table {
private String tableName;
private int rowCount;
private List<Column> columnList;
@XmlAttribute(name = "name")
public String getTableName() {
return tableName;
}
@XmlAttribute(name = "rowCount")
public int getRowCount() {
return rowCount;
}
@XmlElement(name = "column")
public List<Column> getColumnList() {
return columnList;
}
}
Outputs this:
<tables>
<table name="GGS_MARKER" rowCount="19190">
<column>
<columnName>MARKER_TEXT</columnName>
<datatype>VARCHAR2</datatype>
<length>4000.0</length>
</column>
...
But if I change @XmlAttribute with @XmlElement, it just shows:
<tables>
<table>
<column>
<columnName>MARKER_TEXT</columnName>
<datatype>VARCHAR2</datatype>
<length>4000.0</length>
</column>
...
What should I put in the class to get “name” and “rowcount” as elements?
All you need to do in your example is change
@XmlAttributeto@XmlElement. If as in your post you only havegetmethods and notsetmethods you will need to explicitly add the@XmlElementannotation as the default one won’t be applied in this use case (by default all unmapped properties are assumed to have a@XmlElementannotation).Table
Demo
Output