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; } }
其他
作者:chenze 出处:https://www.cnblogs.com/chenze-Index/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |