我正在使用 JAXB 使用 java 对象创建 xml。
我正在尝试创建此标签:
<preTaxAmount currency="USD">84</preTaxAmount>
为此,我使用以下域类:
public class PreTaxAmount
{
@XmlElement(required = false, nillable = true, name = "content")
private String content;
@XmlElement(required = false, nillable = true, name = "currency")
private String currency;
public String getContent ()
{
return content;
}
public void setContent (String content)
{
this.content = content;
}
public String getCurrency ()
{
return currency;
}
public void setCurrency (String currency)
{
this.currency = currency;
}
}
上面的代码生成以下 xml:
<preTaxAmount>
<content>380.0</content>
<currency>USD</currency>
</preTaxAmount>
此格式与所需的格式不同。如何获得所需的格式。
最佳答案
您需要对货币使用@XmlAttribute
注释。类似问题here .
关于java - 如何使用 JAXB 创建复杂的 xml 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29115716/