Adding an Attribute to a JAXB Element

By | January 12, 2018

I’m struggling with some JAXB parsing and need some guidance.

Essentially, I’m trying to add attributes to my class variables that I have already declared as Elements using @XmlElement. So far, any attempt to use @XmlAttribute sets the attribute at the class level.

What I”m currently getting is this:

<DataClass newAttribute="test">
  <myElement>I wish this element had an attribute</myElement>
  <anotherElement>I wish this element had an attribute too</anotherElement>

I’d like to to do this:

  <myElement thisAtt="this is what I'm talking about">This is better</myElement>
  <anotherElement thisAtt="a different attribute here">So is this</anotherElement>

I’ve seen other posts add an attribute to a single element using the @XmlValue, but that doesn’t work when you have Elements, and won’t work on multiple elements.

Does anyone have a thought on how this could be accomplished?



This will create that XML:

public class JaxbAttributes {
    public static void main(String[] args) throws Exception {
        Marshaller marshaller =
        StringWriter stringWriter = new StringWriter();
        DataClass dataClass = new DataClass(
               new Foo("this is what I'm talking about", "This is better"),
               new Foo("a different attribute here", "So is this"));
        marshaller.marshal(dataClass, stringWriter);

    @XmlRootElement(name = "DataClass")
    @XmlType(propOrder = {"myElement", "anotherElement"})
    static class DataClass {
        private Foo myElement;
        private Foo anotherElement;

        DataClass() {}
        public DataClass(Foo myElement, Foo anotherElement) {
            this.myElement = myElement;
            this.anotherElement = anotherElement;

        public Foo getMyElement() { return myElement; }
        public void setMyElement(Foo myElement) { this.myElement = myElement; }
        public Foo getAnotherElement() { return anotherElement; }
        public void setAnotherElement(Foo anotherElement) { this.anotherElement = anotherElement; }

    static class Foo {
        private String thisAtt;
        private String value;

        Foo() {}
        public Foo(String thisAtt, String value) {
            this.thisAtt = thisAtt;
            this.value = value;

        public String getThisAtt() { return thisAtt; }
        public void setThisAtt(String thisAtt) { this.thisAtt = thisAtt; }
        public String getValue() { return value; }
        public void setValue(String value) { this.value = value; }


Note: I’m the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2.X (JSR-222) expert group.

Alternatively you could use the @XmlPath extension in MOXy to handle this use case:


The @XmlPath annotation can be used with the standard JAXB annotations:

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlType(propOrder={"myElement", "anotherElement"})
public class DataClass {

    private String myElement;
    private String myElementThisAtt;
    private String anotherElement;
    private String anotherElementThisAtt;

    public String getMyElement() {
        return myElement;

    public void setMyElement(String myElement) {
        this.myElement = myElement;

    public String getMyElementThisAtt() {
        return myElementThisAtt;

    public void setMyElementThisAtt(String myElementThisAtt) {
        this.myElementThisAtt = myElementThisAtt;

    public String getAnotherElement() {
        return anotherElement;

    public void setAnotherElement(String anotherElement) {
        this.anotherElement = anotherElement;

    public String getAnotherElementThisAtt() {
        return anotherElementThisAtt;

    public void setAnotherElementThisAtt(String anotherElementThisAtt) {
        this.anotherElementThisAtt = anotherElementThisAtt;



import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(DataClass.class);

        DataClass dataClass = new DataClass();
        dataClass.setMyElement("This is better");
        dataClass.setMyElementThisAtt("this is what I'm talking about");
        dataClass.setAnotherElement("So is this");
        dataClass.setAnotherElementThisAtt("a different attribute here");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(dataClass, System.out);


<?xml version="1.0" encoding="UTF-8"?>
   <myElement thisAtt="this is what I'm talking about">This is better</myElement>
   <anotherElement thisAtt="a different attribute here">So is this</anotherElement>

More Information

Category: Xml

Leave a Reply

Your email address will not be published. Required fields are marked *