-1

I am using xstream for parsing that is converting object to xml i have the below pojo along with setters and 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;

    }

and below i am using xstream to generate the xml in this fashion

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

and the xml that is generated is in below fashion is ..

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

now my query is that i want to generate the xml in the below fashion as shown below as you can see below that mail tag is parent tag and inside it there is subtag named invoiceReferenceNotificationMessage which contain all the information with respect to one block can you please advise how to achieve the same

<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>
ndsfd ddsfd
  • 235
  • 1
  • 5
  • 13

1 Answers1

0

Simplest thing to do is use a wrapper class:

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

And serialize this as your root instead.

  • Thanks for the advise i am using jdk 1.5 and when i use the above advise code it is showing the compilation problem Nonnull annotation can you please advise how to overcome from this – ndsfd ddsfd May 20 '15 at 15:56
  • also please advise what changes need to do in xstream then where i am creating the instance – ndsfd ddsfd May 20 '15 at 16:00