我使用@XmlRootElement
注释和@XmlElement
在我的Spring MVC应用程序中生成xml输出。我有以下课程:
@XmlRootElement
public class Chart
{
private String id;
private String name;
private List<Connection> connections;
//..................
@XmlElement(name = "connection")
public List<Connection> getConnections()
{
return this.connections;
}
//..................
}
public class Connection
{
private String from;
private String to;
public String getFrom()
{
return this.from;
}
public void setFrom(String from)
{
this.from = from;
}
public String getTo()
{
return this.to;
}
public void setTo(String to)
{
this.to = to;
}
}
我正在获得这样的xml输出:
<chart>
<id>a1</id>
<name>chart1</name>
<connection>
<from>a1</from>
<to>a2</to>
</connection>
....
</chart>
但是,我需要以这种方式格式化xml:
<chart>
<id>a1</id>
<name>chart1</name>
<connection from="a1" to="a2"></connection>
....
</chart>
如何配置注释以实现此结果?
答案 0 :(得分:2)
对@XmlAttribute
要用作属性的特定元素使用Connection
。
public class Connection
{
private String from;
private String to;
public String getFrom()
{
return this.from;
}
@XmlAttribute
public void setFrom(String from)
{
this.from = from;
}
public String getTo()
{
return this.to;
}
@XmlAttribute
public void setTo(String to)
{
this.to = to;
}
}