[英]What can change the order of elements' attributes in generated XML file by Java?
[英]xml is not generated in the correct order by java
我正在使用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(如我所示)
為了解決該問題,並為您的收集項目使用一個相關的項目名稱,而不要使用每個項目的完全限定的類名稱:
使用合適的名稱用@XStreamImplicit
注釋您的項目集合實例字段:
public class Mail { @XStreamImplicit( itemFieldName = "invoiceReferenceNotificationMessage" ) private List<InvoiceReferenceNotificationMessage> invoiceReferenceNotificationMessage; //... }
在序列化對象之前,請在xstream
實例上調用#processAnnotations(Class)
方法以觸發元數據處理:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.