0

I carefully studied the discussion "JAXB Adding attributes..." and would like to move a little further.

For example, there is a following class:

@XmlRootElement(name = "company")
@XmlType(propOrder = {"id", "name", "address"})
public class Company {

    private String id;
    private String name;
    private String address;

    @XmlElement(name = "id")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlElement(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement(name = "address")
    public String getAddress() {
        return name;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

After marshaling an object we have:

<company>
    <id>1</id>
    <name>Abc</name>
    <address>Mountain View, United States</address>
</company>

Is there an elegant solution, - for example using annotations @XmlPaths, @XmlPath, @XmlElements, @XmlElement, - to receive as a result:

<company>
    <id>1</id>
    <name lang="en">Abc</name>
    <address lang="en">Mountain View, United States</address>
</company>
2
  • what is the problem with the solution in the link that you provided. Commented Mar 17, 2017 at 13:30
  • If you're using MOXy have a look at blog.bdoughan.com/2011/03/…. Commented Mar 17, 2017 at 13:56

1 Answer 1

1

How about creating a custom String with the lang attribute and use that instead of string

for example :

public class LangString {

    @XmlValue
    protected String value;
    @XmlAttribute(name = "lang")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "language")
    protected String lang;

    //GETTERS & SETTERS
}

Your code :

@XmlRootElement(name = "company")
@XmlType(propOrder = {"id", "name", "address"})
public class Company {

    private String id;
    private LangString name;
    private LangString address;

    @XmlElement(name = "id")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlElement(name = "name")
    public LangString getName() {
        return name;
    }

    public void setName(LangString name) {
        this.name = name;
    }

    @XmlElement(name = "address")
    public LangString getAddress() {
        return name;
    }

    public void setAddress(LangString address) {
        this.address = address;
    }
}

The code above was generated from an xsd schema for my application that had elements with the lang attribute thus the @XmlSchemaType.

Hope it helps

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, but what if you have to add an attribute to many xml elements that are not objects (classes). You would have to write a class for each one of them , which makes this solution impractical.
You mean an attribute with different name ?
No, I mean an attribute with the same name. For instance, in the OPs example the attribute lang should be associated with both name and address fields. However, using your suggestion, you would have to create to 2 new classes (e.g. AddressLang and NameLang respectively), each of which would than have an attribute named lang. This works but is impractical. Also, in this scenario there's no need for extending XmlAdapter.
No on the contrary, you just reuse the class with a different name. I edited my answer to include your code.
Yes, that makes sense , at least in the OP's scenario. +1)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.