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 @XmlReadOnly
extension. The field/property annotated with @XmlReadOnly
will be populated during an unmarshal (read), but will not be written during a marshal.
User
package forum10208143;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlReadOnly;
/**
* @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")
@XmlReadOnly
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
jaxb.properites
To specify MOXy as your JAXB provider you need to add a file called jaxb.properties
in the same package as your domain model with the following entry:
javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory
Demo
package forum10208143;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(User.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum10208143/input.xml");
User user = (User) unmarshaller.unmarshal(xml);
System.out.println(user.getPassword());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(user, System.out);
}
}
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<user>
<id>jdoe</id>
<nickname>Jane</nickname>
<email>[email protected]</email>
<password>secret</password>
</user>
Output
secret
<?xml version="1.0" encoding="UTF-8"?>
<user>
<email>[email protected]</email>
<id>jdoe</id>
<nickname>Jane</nickname>
</user>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With