JAXB重命名属性 [英] JAXB rename attribute
本文介绍了JAXB重命名属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有可怕长名称的类,它使用JAXB转换为XML。使用 @XmlRootElement(name =nicername)
,我可以将外部XML标记重命名为< nicername>
。
I have some class with a terrible long name, which is transformed into XML with JAXB. Using @XmlRootElement(name="nicername")
, I am able to rename the outer XML tag to <nicername>
.
如何将具有丑陋类名的个别属性重命名为一些好名字?
How do I rename individual attributes with ugly names of the class to some nice name too ?
推荐答案
您可以使用 @XmlAttribute
和 @XmlElement
注释来更改XML名称。如果您注释字段,请确保在类上使用 @XmlAccessorType(XmlAccessType.FIELD)
注释:
You can use the @XmlAttribute
and @XmlElement
annotations to change the XML names. If you annotate the fields be sure to use the @XmlAccessorType(XmlAccessType.FIELD)
annotation on the class:
@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;
}
或者您可以注释属性:
@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;
}
}
这篇关于JAXB重命名属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文