Let's go

java XML对象互转 注解实例

java JAXB @XmlAttribute @XmlElement @XmlValue实例

@XmlAttribute 用法

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

效果:

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

 

常用备注

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "response")         // XML文件中的根标识
// 控制JAXB 绑定类中属性和字段的排序
@XmlType(propOrder = {
        "result",
        "commands",
        "versions",
})
public class Heartbeat_Response implements Serializable {

    @XmlAttribute(name = "result")
    private Com_Result result;

    @XmlElementWrapper(name = "commands")
    @XmlElement(name = "command")
    private List<Heartbeat_Command> commands;

    @XmlElementWrapper(name = "versions")
    @XmlElement(name = "version")
    private List<Heartbeat_Version> versions;

    public Com_Result getResult() {
        return result;
    }

    public void setResult(Com_Result result) {
        this.result = result;
    }

    public List<Heartbeat_Command> getCommands() {
        return commands;
    }

    public void setCommands(List<Heartbeat_Command> commands) {
        this.commands = commands;
    }

    public List<Heartbeat_Version> getVersions() {
        return versions;
    }

    public void setVersions(List<Heartbeat_Version> versions) {
        this.versions = versions;
    }
}

 

其他

 

posted @ 2022-12-01 08:43  chenze  阅读(442)  评论(0编辑  收藏  举报
有事您Q我