0
Im嘗試使用嵌套元素從java類創建XML。 我不明白我的錯誤在哪裏,我該怎麼改變 謝謝!未能封送元素嵌套
我的主類:
String MY_XML = "my path..."; //hier is the path...
JAXBContext context = JAXBContext.newInstance(MasterDataRM.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
Master temp = new Master();
temp.setTransactionStatus("AlmostOk");
m.marshal(temp, new File(MY_XML));
m.marshal(temp, System.out);
我的類將被編組:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Master {
@XmlElement
protected Date ResponseDatetime = new Date();
@XmlElement
protected Transaction transaction;
public void setResponseDatetime(Date date){
this.ResponseDatetime = date;
}
public Date getDate(){
return ResponseDatetime;
}
public static class Transaction{
@XmlElement
String status = "OK";
}
public void setStatus(String status){
transaction.status = status; // This throws the NullPointerException !!!
}
public String getStatus(){
return transaction.status;
}