我一直在使用JAXB库来阅读&暂时写XML文件,没有任何问题。但是,我最近遇到了在编组期间将NUL字符写入XML文件的问题。
奇怪的是,在土耳其语版本的Windows上进行测试时,这个问题才出现。我不能用英语或日语(我可以访问atm的唯一平台)分发它们。知道是什么原因引起的吗?下面的示例代码。
编组类:
public class InputsMarshaller {
public static Inputs unmarshal(String filePath) {
Inputs inputs = null;
try {
JAXBContext context = JAXBContext.newInstance(Inputs.class); //1
Unmarshaller unmarshaller = context.createUnmarshaller();
inputs = (Inputs) unmarshaller.unmarshal(new File(filePath));
} catch (JAXBException ex) {
ex.printStackTrace(System.out);
}
return inputs;
}
public static void marshal(Inputs inputs, String filePath) {
try {
JAXBContext context = JAXBContext.newInstance(Inputs.class); //1
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(inputs, new FileWriter(new File(filePath)));
} catch (IOException | JAXBException ex) {
ex.printStackTrace(System.out);
}
}
}
输入类:
@XmlRootElement(name = "inputs")
public class Inputs {
private ArrayList<String> values;
private String name, unit;
@XmlElement(name="values")
public List<String> getValues() {
if(values == null) {
values = new ArrayList<>();
}
return values;
}
@XmlAttribute(name = "name")
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@XmlAttribute(name = "unit")
public void setUnit(String unit) {
this.unit = unit;
}
public String getUnit() {
return unit;
}
}