Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB rename attribute

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 ?

like image 308
merce handig Avatar asked Dec 12 '22 17:12

merce handig


1 Answers

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;

}

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

}
like image 140
bdoughan Avatar answered Dec 14 '22 07:12

bdoughan