java

时间:2015-05-23 15:48:17

标签: java xml xslt-1.0

我使用xstream从java对象生成xml,下面是从java对象生成的xml

<brokermail>
  <invoiceReferenceNotificationMessage>
    <com.absfm.ice.ioa.jms.InvoiceReferenceNotificationMessage>
      <InvoiceReference>SM/829709/0315</InvoiceReference>
      <ABSReference>IRMAR157311</ABSReference>
      <Currency>GBP</Currency>
      <InvoiceAmount>2546.0</InvoiceAmount>
      <PaidAmount>1245.0</PaidAmount>
      <BalanceAmount>0.0</BalanceAmount>
      <ValueDate>2015-05-23 20:07:20.78 IST</ValueDate>
      <Remarks>abc</Remarks>
    </com.rbsfm.ice.ioa.jms.InvoiceReferenceNotificationMessage>
    <com.rbsfm.ice.ioa.jms.InvoiceReferenceNotificationMessage>
      <InvoiceReference>SM/15</InvoiceReference>
      <ABSReference>I157311</ABSReference>
      <Currency>EUR</Currency>
      <InvoiceAmount>255546.0</InvoiceAmount>
      <PaidAmount>125545.0</PaidAmount>
      <BalanceAmount>0.0</BalanceAmount>
      <ValueDate>2015-05-23 20:07:20.78 IST</ValueDate>
      <Remarks>abERRc</Remarks>
    </com.absfm.ice.ioa.jms.InvoiceReferenceNotificationMessage>
  </invoiceReferenceNotificationMessage>
</brokermail>

但我希望以下面的方式生成xml

<brokermail>

  <invoiceReferenceNotificationMessage>
   <InvoiceReference>SM/829709/0315</InvoiceReference>
      <ABSReference>IRMAR157311</ABSReference>
      <Currency>GBP</Currency>
      <InvoiceAmount>2546.0</InvoiceAmount>
      <PaidAmount>1245.0</PaidAmount>
      <BalanceAmount>0.0</BalanceAmount>
      <ValueDate>2015-05-23 20:38:35.110 IST</ValueDate>
      <Remarks>abc</Remarks>
     </invoiceReferenceNotificationMessage>

     <invoiceReferenceNotificationMessage>
        <InvoiceReference>SM/15</InvoiceReference>
      <ABSReference>I157311</ABSReference>
      <Currency>EUR</Currency>
      <InvoiceAmount>255546.0</InvoiceAmount>
      <PaidAmount>125545.0</PaidAmount>
      <BalanceAmount>0.0</BalanceAmount>
      <ValueDate>2015-05-23 20:38:35.110 IST</ValueDate>
      <Remarks>abERRc</Remarks>
  </invoiceReferenceNotificationMessage>

</brokermail>

现在正如上面所示,我希望以上述方式生成xml,而这种方式现在还没有发生。你能否告诉我在下面的pojo中需要做些什么改变才能以上述方式获取xml。下面是我的pojo

  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;
            }
    }

这是正在发生的主要方法调用

下面是使用xstream以下生成xml的主要类。

InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage = new InvoiceReferenceNotificationMessage();
        invoiceReferenceNotificationMessage.setInvoiceReference("SM/854565");
        invoiceReferenceNotificationMessage.ABSReference("IRM5454311");
        invoiceReferenceNotificationMessage.setCurrency("GBP");
        invoiceReferenceNotificationMessage.setInvoiceAmount(255446);
        invoiceReferenceNotificationMessage.setPaidAmount(12445);
        invoiceReferenceNotificationMessage.setBalanceAmount(0);
        invoiceReferenceNotificationMessage.setValueDate(new Date());
        invoiceReferenceNotificationMessage.setRemarks("abc");


        InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage1 = new InvoiceReferenceNotificationMessage();
        invoiceReferenceNotificationMessage1.setInvoiceReference("SM/14545");
        invoiceReferenceNotificationMessage1.ABSReference("I15745311");
        invoiceReferenceNotificationMessage1.setCurrency("EUR");
        invoiceReferenceNotificationMessage1.setInvoiceAmount(2555546);
        invoiceReferenceNotificationMessage1.setPaidAmount(125545);
        invoiceReferenceNotificationMessage1.setBalanceAmount(0);
        invoiceReferenceNotificationMessage1.setValueDate(new Date());
        invoiceReferenceNotificationMessage1.setRemarks("abERRc");

        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);
        System.out.println(abc);

伙计们请告知我在上面的pojos中需要做些什么更改才能获得所需的xml,如我所示

1 个答案:

答案 0 :(得分:0)

为了解决问题,并为您的收藏品设置相关的项目名称,而不是每个都具有完全限定的类名称:

  • 使用合适的名称使用@XStreamImplicit注释您的项目集合实例字段:

    public class Mail {
    
      @XStreamImplicit( itemFieldName = "invoiceReferenceNotificationMessage" )
      private  List<InvoiceReferenceNotificationMessage> invoiceReferenceNotificationMessage;
      //...
    }
    
  • 在序列化对象之前,请调用#processAnnotations(Class)实例上的xstream方法以触发元数据处理:

    Mail m = new Mail();
    m.addInvoiceReferenceNotificationMessages(invoiceReferenceNotificationMessagest);
    
    XStream xstream = new XStream();
    xstream.alias("brokermail", Mail.class);
    xstream.processAnnotations(Mail.class);
    String abc = xstream.toXML(m);
    System.out.println(abc);