如下图所示,我有一个带有setter和getter的POJO类:
public class InvoiceReferenceNotificationMessage {
private String InvoiceReference;
private String ABSReference;
private String Currency;
private double InvoiceAmount;
private double PaidAmount;
private double BalanceAmount;
private Date ValueDate;
private String Remarks;
}
及以下是引用上述内容的类:
public class Mail {
@SuppressWarnings("unused")
private InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage;
public InvoiceReferenceNotificationMessage getInvoiceReferenceNotificationMessage() {
return invoiceReferenceNotificationMessage;
}
public void setInvoiceReferenceNotificationMessage(
InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage) {
this.invoiceReferenceNotificationMessage = invoiceReferenceNotificationMessage;
}
}
我正在使用xstream从对象生成xml。我在java中创建单个对象没有问题,如下所示:
public class InvoiceReferenceNotificationMessagetest {
InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage = new InvoiceReferenceNotificationMessage();
invoiceReferenceNotificationMessage.setInvoiceReference("S15");
invoiceReferenceNotificationMessage.setABSReference("IRMA1");
invoiceReferenceNotificationMessage.setCurrency("GBP");
invoiceReferenceNotificationMessage.setInvoiceAmount(25746);
invoiceReferenceNotificationMessage.setPaidAmount(18245);
invoiceReferenceNotificationMessage.setBalanceAmount(90);
invoiceReferenceNotificationMessage.setValueDate(new Date());
invoiceReferenceNotificationMessage.setRemarks("abc");
Mail m = new Mail();
m.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage);
XStream xstream = new XStream();
xstream.alias("brokermail",Mail.class);
String abc = xstream.toXML(m);
}
如何继续创建多个对象?我应该将它们添加到列表中然后将其传递给xstream吗?请告知我如何更改我的邮件类以存储InvoiceReferenceNotificationMessage类的多个对象以下是我的尝试:
InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage1 = new InvoiceReferenceNotificationMessage();
invoiceReferenceNotificationMessage.setInvoiceReference("S93159");
invoiceReferenceNotificationMessage.setABSReference("IRM9311");
invoiceReferenceNotificationMessage.setCurrency("GBP");
invoiceReferenceNotificationMessage.setInvoiceAmount(257646);
invoiceReferenceNotificationMessage.setPaidAmount(12475);
invoiceReferenceNotificationMessage.setBalanceAmount(0);
invoiceReferenceNotificationMessage.setValueDate(new Date());
invoiceReferenceNotificationMessage.setRemarks("abrtc");
InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage2 = new InvoiceReferenceNotificationMessage();
invoiceReferenceNotificationMessage.setInvoiceReference("S6315");
invoiceReferenceNotificationMessage.setABSReference("IR11");
invoiceReferenceNotificationMessage.setCurrency("GBP");
invoiceReferenceNotificationMessage.setInvoiceAmount(266546);
invoiceReferenceNotificationMessage.setPaidAmount(12645);
invoiceReferenceNotificationMessage.setBalanceAmount(0);
invoiceReferenceNotificationMessage.setValueDate(new Date());
invoiceReferenceNotificationMessage.setRemarks("abcdgdg");
答案 0 :(得分:0)
Mail
只能包含一个InvoiceReferenceNotificationMessage
,因此您需要创建第二个“邮件”对象(或在生成XML后重复使用第一个对象)以保留第二个“InvoiceReferenceNotificationMessage”&#39 ;对象
Mail m1 = new Mail();
Mail m2 = new Mail()
XStream xstream = new XStream();
xstream.alias("brokermail",Mail.class);
m1.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage1);
m2.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage2);
String abc1 = xstream.toXML(m1);
String abc2 = xstream.toXML(m2);
...或......
Mail m1 = new Mail();
XStream xstream = new XStream();
xstream.alias("brokermail",Mail.class);
m1.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage1);
String abc1 = xstream.toXML(m1);
m1.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage2);
String abc2 = xstream.toXML(m1);
答案 1 :(得分:0)
尝试使用此版本的Mail
作为起点:
public class Mail {
private List<InvoiceReferenceNotificationMessage> invoiceReferenceNotificationMessages = new ArrayList<>();
public List<InvoiceReferenceNotificationMessage> getInvoiceReferenceNotificationMessages() {
return invoiceReferenceNotificationMessages;
}
public void addInvoiceReferenceNotificationMessage(InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage) {
this.invoiceReferenceNotificationMessages.add(invoiceReferenceNotificationMessage);
}
public void addInvoiceReferenceNotificationMessages(List<InvoiceReferenceNotificationMessage> invoiceReferenceNotificationMessages) {
this.invoiceReferenceNotificationMessages = invoiceReferenceNotificationMessages;
}
}
您可以根据需要更改/添加方法......