I’m new to java XML binding.
This is my class
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author Martin Burchard
*
*/
@XmlRootElement(name = "user")
public class User {
private String id;
private String nickname;
private String email;
private String password;
@XmlElement(name = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(name = "nickname")
public String getNickName() {
return nickname;
}
public void setNickName(String nickname) {
this.nickname = nickname;
}
@XmlElement(name = "email")
public String getEMail() {
return email;
}
public void setEMail(String email) {
this.email = email;
}
@XmlElement(name = "password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
During creation/registration of a user the password must be given, but later, when asking for userinformation, the XML should not contain the password element.
Is it possible to define an Element as writeonly?
Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
MOXy has an
@XmlReadOnlyextension. The field/property annotated with@XmlReadOnlywill be populated during an unmarshal (read), but will not be written during a marshal.User
jaxb.properites
To specify MOXy as your JAXB provider you need to add a file called
jaxb.propertiesin the same package as your domain model with the following entry:Demo
input.xml
Output