java JAXB @XmlAttribute @XmlElement @XmlValue实例

 实体类

@Data
public static class CouponOther{  
    private String type;

  
    private List<Ref> ref;

    @Data   
    public static class Ref{
	
	private String code;

	
	private String value;
    }

}

  

@XmlAttribute 用法

@Data
@XmlAccessorType(XmlAccessType.FIELD)
public static class CouponOther{
    @XmlElement(name = "Type")
    private String type;

}

x效果:

  

<Other>
      <Type>210</Type>      
</Other>

  

@XmlElement用法 :<Other Type="210"> </Other>

@Data
@XmlAccessorType(XmlAccessType.FIELD)
public static class CouponOther{
    @XmlAttribute(name = "Type")
    private String type;

}

  

效果:

<Other Type="210">  </Other>

  

@XmlValue用法:<Ref Code="couponid">TEST071909</Ref>

@Data
@XmlAccessorType(XmlAccessType.FIELD)
public static class CouponOther{
    @XmlAttribute(name = "Type")
    private String type;

    @XmlElement
    private List<Ref> ref;

    @Data
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Ref{
	@XmlAttribute(name = "Code")
	private String code;

	@XmlValue
	private String value;
    }

}

  

效果:

 <Other Type="210">
                    <Ref Code="OJ_SuperPNR_RPH">1</Ref>
                    <Ref Code="couponid">TEST071909</Ref>
                </Other>

  

原文地址:https://www.cnblogs.com/achengmu/p/15045198.html