Java Language JAXB Manual field/property XML mapping configuration

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!


Annotations @XmlElement, @XmlAttribute or @XmlTransient and other in package javax.xml.bind.annotation allow the programmer to specify which and how marked fields or properties should be serialized.

@XmlAccessorType(XmlAccessType.NONE) // we want no automatic field/property marshalling
public class ManualXmlElementsExample {
    private String field="field value";

    private String attribute="attr value";
    private String oneAttribute="other attr value";
    @XmlElement(name="different name")
    private String oneName="different name value";
    private String transientField = "will not get serialized ever";
    public String getModifiedTransientValue() {
        return transientField.replace(" ever", ", unless in a getter");
    public void setModifiedTransientValue(String val) {} // empty on purpose

    public static void main(String[] args) {
        try {
            JAXB.marshal(new ManualXmlElementsExample(), System.out);
        } catch (Exception e) {
            System.err.println("Exception occurred while writing in XML!");

Got any Java Language Question?