I have my User class in java. When I want to unmarshal it, I get xml with <UserIn>
root element, and when I want to marshal it I should do <UserOut>
to be XML root element. If I provide @XmlRootElement("UserIn")
it is not dynamic and it is always UserIn root. Is there any way to do dynamic root element on class? thanks.
Asked
Active
Viewed 539 times
0

Matija Župančić
- 1,080
- 2
- 11
- 21
1 Answers
1
You could create two classes that extend your User class, and then use the specific child class based on if you are marshalling on unmarshalling.
For example, for a class User:
public class User {
@XmlElement
private String value;
public User() { }
public User(String value) {
this.value = value;
}
}
You can have UserIn:
@XmlRootElement(name = "UserIn")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserIn extends User {
public UserIn() { }
public UserIn(String value) {
super(value);
}
}
and UserOut:
@XmlRootElement(name = "UserOut")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserOut extends User {
public UserOut() { }
public UserOut(String value) {
super(value);
}
}
Provide the appropriate class where you need, and you will get it working with the input or output you wish.

martidis
- 2,897
- 1
- 11
- 13
-
Thanks. That was my option too, but I was hoping I can do it with one annotation. – Matija Župančić Dec 10 '18 at 14:56