在多个对象的情况下,java不能正确生成xml对象

时间:2015-05-21 14:17:53

标签: java xstream

如下图所示,我有一个带有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);
}

我最终获得的标签如下所示:

<brokermail>
  <invoiceReferenceNotificationMessage>
    <InvoiceReference>dffg</InvoiceReference>
    <ABSReference>Ifgfg</ABSReference>
    <Currency>Ggfg</Currency>
    <InvoiceAmount>2554546.0</InvoiceAmount>
    <PaidAmount>125445.0</PaidAmount>
    <BalanceAmount>0454.0</BalanceAmount>
    <ValueDate>2015-:34.165 IST</ValueDate>
    <Remarks>abc</Remarks>
  </invoiceReferenceNotificationMessage>
   <invoiceReferenceNotificationMessage>
    <InvoiceReference>xxRRR5</InvoiceReference>
    <ABSReference>IRMAR15657311</ABSReference>
    <Currency>EUR</Currency>
    <InvoiceAmount>2545446.0</InvoiceAmount>
    <PaidAmount>154245.0</PaidAmount>
    <BalanceAmount>045.0</BalanceAmount>
    <ValueDate>2015-05-20 21:34:34.165 IST</ValueDate>
    <Remarks>abffc</Remarks>
  </invoiceReferenceNotificationMessage>
</brokermail>

但是我已经改变了我的邮件类,以下面的方式存储多个对象

public class Mail {
    private  List<InvoiceReferenceNotificationMessage> invoiceReferenceNotificationMessage = new ArrayList<InvoiceReferenceNotificationMessage>();

    public List<InvoiceReferenceNotificationMessage> getInvoiceReferenceNotificationMessages() {
        return invoiceReferenceNotificationMessage;
    }

    public void addInvoiceReferenceNotificationMessages(List<InvoiceReferenceNotificationMessage> invoiceReferenceNotificationMessages) {
        this.invoiceReferenceNotificationMessage = invoiceReferenceNotificationMessages;
    }
}

现在当我创建如下所示的对象时..

List<InvoiceReferenceNotificationMessage> invoiceReferenceNotificationMessagest = new ArrayList<InvoiceReferenceNotificationMessage>();
        invoiceReferenceNotificationMessagest.add(invoiceReferenceNotificationMessage);
        invoiceReferenceNotificationMessagest.add(invoiceReferenceNotificationMessage1);

        Mail m = new Mail();
        m.addInvoiceReferenceNotificationMessages(invoiceReferenceNotificationMessagest);

XStream xstream = new XStream();
        xstream.alias("brokermail",Mail.class);
        String abc = xstream.toXML(m);

我得到的标签如下所示:

<brokermail>
  <invoiceReferenceNotificationMessage>
    <com.jms.InvoiceReferenceNotificationMessage>
      <InvoiceReference>SM/829743434309/0315</InvoiceReference>
      <RBSReference>IRMAR34343157311</RBSReference>
      <Currency>GBP</Currency>
      <InvoiceAmount>23434546.0</InvoiceAmount>
      <PaidAmount>124345.0</PaidAmount>
      <BalanceAmount>0.0</BalanceAmount>
      <ValueDate>2015-05-21 17:51:26.188 IST</ValueDate>
      <Remarks>abc</Remarks>
    </com.jms.InvoiceReferenceNotificationMessage>
    <com.rbsfm.ice.ioa.jms.InvoiceReferenceNotificationMessage>
      <InvoiceReference343>SM/4315</InvoiceReference>
      <RBSReference>I157311</RBSReference>
      <Currency>EUR</Currency>
      <InvoiceAmount>2554546.0</InvoiceAmount>
      <PaidAmount>12543545.0</PaidAmount>
      <BalanceAmount>0.0</BalanceAmount>
      <ValueDate>2015-05-21 17:51:26.188 IST</ValueDate>
      <Remarks>abERRc</Remarks>
    </com.jms.InvoiceReferenceNotificationMessage>
  </invoiceReferenceNotificationMessage>
</brokermail>

这是不正确的,因为我正在寻找下面的应该来,请告知如何实现这一目标。

<brokermail>
  <invoiceReferenceNotificationMessage>
    <InvoiceReference>AA/54545829709/0315</InvoiceReference>
    <RBSReference>IRMA4545R157311</RBSReference>
    <Currency>GBP</Currency>
    <InvoiceAmount>255446.0</InvoiceAmount>
    <PaidAmount>124545.0</PaidAmount>
    <BalanceAmount>0.0</BalanceAmount>
    <ValueDate>2015-05-20 21:34:34.165 IST</ValueDate>
    <Remarks>abc</Remarks>
  </invoiceReferenceNotificationMessage>
   <invoiceReferenceNotificationMessage>
    <InvoiceReference>xx/8245459709/0315</InvoiceReference>
    <RBSReference>IRMAR545157311</RBSReference>
    <Currency>GBP</Currency>
    <InvoiceAmount>2545456.0</InvoiceAmount>
    <PaidAmount>124455.0</PaidAmount>
    <BalanceAmount>0.0</BalanceAmount>
    <ValueDate>2015-05-20 21:34:34.165 IST</ValueDate>
    <Remarks>abc</Remarks>
  </invoiceReferenceNotificationMessage>
</brokermail> 

如您所见,以下标记为

<com.jms.InvoiceReferenceNotificationMessage>

是额外的,我不需要,请告诉我如何克服这一点。

1 个答案:

答案 0 :(得分:0)

变化:

    private  List<InvoiceReferenceNotificationMessage> invoiceReferenceNotificationMessage = 
new ArrayList<InvoiceReferenceNotificationMessage>();

为:

    @XStreamImplicit(itemFieldName="invoiceReferenceNotificationMessage")
    private  List<InvoiceReferenceNotificationMessage> InvoiceReferenceNotificationMessage = 
new ArrayList<InvoiceReferenceNotificationMessage>();

并添加:

xstream.processAnnotations(Mail.class);