[英]JAXB rename attribute
I have some class with a terrible long name, which is transformed into XML with JAXB. 我有一个名字很烂的类,它用JAXB转换成XML。 Using
@XmlRootElement(name="nicername")
, I am able to rename the outer XML tag to <nicername>
. 使用
@XmlRootElement(name="nicername")
,我能够将外部XML标签重命名为<nicername>
。
How do I rename individual attributes with ugly names of the class to some nice name too ? 如何将具有丑陋类名的单个属性重命名为一些不错的名称?
You can use the @XmlAttribute
and @XmlElement
annotations to change the XML names. 您可以使用
@XmlAttribute
和@XmlElement
批注来更改XML名称。 If you annotate the fields be sure to use the @XmlAccessorType(XmlAccessType.FIELD)
annotation on the class: 如果您对字段进行注释,请确保在类上使用
@XmlAccessorType(XmlAccessType.FIELD)
注释:
@XmlRootElement(name="nice-name")
@XmlAccessorType(XmlAccessType.FIELD)
public class UglyName {
@XmlElement(name="nice-element-name")
private String uglyElementName;
@XmlAttribute(name="nice-attribute-name")
private String uglyAttributeName;
}
Or you can annotate the properties: 或者,您可以注释属性:
@XmlRootElement(name="nice-name")
public class UglyName {
private String uglyElementName;
private String uglyAttributeName;
@XmlElement(name="nice-element-name")
public String getUglyElementName() {
return uglyElementName;
}
public void setUglyElementName(String name) {
this.uglyElementNamne = name;
}
@XmlAttribute(name="nice-attribute-name")
public String getUglyAttributeName() {
return uglyAttributeName;
}
public void setUglyAttributeName(String name) {
this.uglyAttributeNamne = name;
}
}
You can define alternate names for your object properties using: 您可以使用以下方法为对象属性定义备用名称:
XMLAttribute
annotation for attributes XMLAttribute
批注 XMLElement
annotations for elements XMLElement
注释 Both the @XmlAttribute and @XmlElement annotations can be remapped by name using identical syntax as the @XmlRootElement annotation. @XmlAttribute和@XmlElement批注都可以使用与@XmlRootElement批注相同的语法按名称重新映射。 So, just attach the relevant annotations to each individual field/property you need remapped and provide an argument for "name".
因此,只需将相关注释附加到您需要重新映射的每个单独的字段/属性,并为“名称”提供参数即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.