当一个元素(Books)需要自己的属性(标题和页面)时,如何将JAXB注释放在我的bean类(Product)上?
示例输出
<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:getProductsv2Response xmlns:ns2="http://sample.targetnamespace.org/">
<Product price="123" identifier="23423">
<Book title="Super Cool Book" pages="45"/>
</Product>
</ns2:getProductsv2Response>
</S:Body>
</S:Envelope>
答案 0 :(得分:1)
你应该这样注释
@XmlAccessorType(XmlAccessType.FIELD)
public class Product {
@XmlAttribute
private String identifier;
@XmlAttribute
private Double price;
@XmlElement(name="Book")
private Book book;
public String getIdentifier() {
return identifier;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
}
JAXB生成XML
<Product identifier="123-12" price="500.0">
<Book title="Test Book" pages="100"/>
</Product>