2016-05-19 6 views
0

1つの要素(書籍)に独自の属性(タイトルとページ)が必要な場合、JAXB注釈をBeanクラス(Product)に配置するにはどうすればよいですか?JAXBで属性を持つ要素

サンプル出力

<?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> 

答えて

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> 
関連する問題