Reputation: 469
How do I put JAXB annotations on my bean class (Product) when one element (Books) needs attributes of its own (title and pages)?
Sample Output
<?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>
Upvotes: 0
Views: 45
Reputation: 2225
You should annotate like this
@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 generated XML
<Product identifier="123-12" price="500.0">
<Book title="Test Book" pages="100"/>
</Product>
Upvotes: 1