问题
package jaxb.classes;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Task {
@XmlElement(name="input")
private String input; // String representing the input file
@XmlElement(name="output")
private String output; // String representing the output file
@XmlElement(name="format")
private Format format; // a jaxb.classes.Format representing the format of conversion
@XmlElement(name="taskID")
private long taskID; // a unique ID for each task.
@XmlElement(name="isReady")
public boolean isReady; // boolean value representing whether the task is ready for conversion
public boolean isChanging; // boolean representing if the user is changing the task DO NOT MARSHALL
public boolean isExecuting; // boolean representing whether the task is being executed DO NOT MARSHALL
public long getTaskID(){
return taskID;
}
public String getInput(){
return input;
}
public String getOutput(){
return output;
}
public Format getFormat(){
return format;
}
public void setOutput(String out){
output = out;
}
public void setFormat(Format f){
format = f;
}
}
So, here is a class that represents the pending tasks for conversion. This will be converted into XML which will contain the saved data.
However, I do not want isChanging
, isExecuting
to be made into XML. I want them to be false
when they are read back.
How can I do that ?
回答1:
There are a couple of ways to support this use case:
Option #1 - @XmlTransient
You can use the @XmlTransient
annotation to prevent a field/property from being marshalled or unmarshalled.
Option #2 - @XmlAccessorType(XmlAccessType.NONE)
You can annotated the class with @XmlAccessorType(XmlAccessType.NONE)
so that only annotated fields/properties will be marshalled/unmarshalled.
For More Information
- http://blog.bdoughan.com/2012/04/jaxb-and-unmapped-properties.html
来源:https://stackoverflow.com/questions/16698423/what-if-i-do-not-want-some-variables-to-be-marshalled-into-xml-file