如果我不想将某些变量编组到XML文件中怎么办? [英] What if I do not want some variables to be marshalled into XML file?
问题描述
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;
}
}
所以,这是一个类表示转换的待处理任务。这将转换为包含已保存数据的XML。
但是,我不希望 isChanging
, isExecuting
将被制作成XML。当他们被回读时,我希望它们是 false
。
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.
我该怎么做?
推荐答案
有几种方法可以支持此用例:
There are a couple of ways to support this use case:
选项#1 - @XmlTransient
Option #1 - @XmlTransient
您可以使用 @XmlTransient
注释来防止字段/属性被编组或者是unmarshalled。
You can use the @XmlTransient
annotation to prevent a field/property from being marshalled or unmarshalled.
选项#2 - @XmlAccessorType(XmlAccessType.NONE)
Option #2 - @XmlAccessorType(XmlAccessType.NONE)
您可以使用 @XmlAccessorType(XmlAccessType.NONE)
对类进行批注,以便仅对带注释的字段/属性进行编组/ unmarshalled。
You can annotated the class with @XmlAccessorType(XmlAccessType.NONE)
so that only annotated fields/properties will be marshalled/unmarshalled.
更多信息
- http://blog.bdoughan.com/2012/04/jaxb-and-unmapped-properties.html
这篇关于如果我不想将某些变量编组到XML文件中怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!