Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中通过xstream从对象生成xml_Java_Xml_Xstream - Fatal编程技术网

在java中通过xstream从对象生成xml

在java中通过xstream从对象生成xml,java,xml,xstream,Java,Xml,Xstream,我使用xstream进行解析,将对象转换为xml。我有下面的pojo以及setter和getter public class InvoiceReferenceNotificationMessage { private String InvoiceReference; private String ABSReference; private String Currency; private double InvoiceAmount; private do

我使用xstream进行解析,将对象转换为xml。我有下面的pojo以及setter和getter

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;

    }
下面我使用xstream以这种方式生成xml

public class InvoiceReferenceNotificationMessagetest {

        InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage = new InvoiceReferenceNotificationMessage();
        invoiceReferenceNotificationMessage.setInvoiceReference("SM/8315");
        invoiceReferenceNotificationMessage.setABSReference("IRMAR157311");
        invoiceReferenceNotificationMessage.setCurrency("GBP");
        invoiceReferenceNotificationMessage.setInvoiceAmount(2546);
        invoiceReferenceNotificationMessage.setPaidAmount(1245);
        invoiceReferenceNotificationMessage.setBalanceAmount(0);
        invoiceReferenceNotificationMessage.setValueDate(new Date());
        invoiceReferenceNotificationMessage.setRemarks("abc");

        XStream xstream = new XStream();
        xstream.alias("invoiceReferenceNotificationMessage",InvoiceReferenceNotificationMessage.class);
        String abc = xstream.toXML(invoiceReferenceNotificationMessage);
生成的xml如下所示:

<invoiceReferenceNotificationMessage>
  <InvoiceReference>SM/8315</InvoiceReference>
  <ABSReference>IRMAR157311</ABSReference>
  <Currency>GBP</Currency>
  <InvoiceAmount>2546</InvoiceAmount>
  <PaidAmount>1245</PaidAmount>
  <BalanceAmount>0</BalanceAmount>
  <ValueDate>2015-05-20 19:57:51.188 IST</ValueDate>
  <Remarks>abc</Remarks>
</invoiceReferenceNotificationMessage>

SM/8315
IRMAR157311
英镑
2546
1245
0
2015-05-20 19:57:51.188伊斯特
abc
现在我的查询是,我想以如下所示的方式生成xml,正如您可以在下面看到的,mail标记是父标记 在它里面有一个名为invoiceReferenceNotificationMessage的子标签,其中包含关于一个区块的所有信息。您能建议如何实现相同的功能吗

<mail>

<invoiceReferenceNotificationMessage>
  <InvoiceReference>SM/8315</InvoiceReference>
  <ABSReference>IR311</ABSReference>
  <Currency>GBP</Currency>
  <InvoiceAmount>2546.0</InvoiceAmount>
  <PaidAmount>1245.0</PaidAmount>
  <BalanceAmount>0.0</BalanceAmount>
  <ValueDate>2015-05-20 19:57:51.188 IST</ValueDate>
  <Remarks>abc</Remarks>
  </invoiceReferenceNotificationMessage>


</mail>

SM/8315
IR311
英镑
2546
1245
0
2015-05-20 19:57:51.188伊斯特
abc

最简单的方法是使用包装器类:

public class Mail
{
    private final InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage;
    public Mail(@Nonnull final InvoiceReferenceNotificationMessage msg)
    { this.invoiceReferenceNotificationMessage = msg; }
    public InvoiceReferenceNotificationMessage getinvoiceReferenceNotificationMessage() { return this.invoiceReferenceNotificationMessage; }
}

并将其序列化为您的根。

感谢您的建议,我正在使用jdk 1.5,当我使用上述建议代码时,它显示了编译问题非空注释。您能否建议如何克服此问题?另外,请建议在xstream中需要做哪些更改,然后我将在何处创建实例