0

As shown below, I have a POJO class with setters and getters:

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 is the class in which the above is being referenced:

public class Mail {
    @SuppressWarnings("unused")
    private  InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage;

    public InvoiceReferenceNotificationMessage getInvoiceReferenceNotificationMessage() {
        return invoiceReferenceNotificationMessage;
    }

    public void setInvoiceReferenceNotificationMessage(
            InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage) {
        this.invoiceReferenceNotificationMessage = invoiceReferenceNotificationMessage;
    }

}

I am using xstream to generate the xml from the object. I have no problem creating a single object in java as shown below:

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

How would I proceed in creating multiple objects? Should I add them to a list and then pass it to xstream? please advise how can I change my mail class to store multiple objects of InvoiceReferenceNotificationMessage class Below is my attempt:

InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage1 = new InvoiceReferenceNotificationMessage();
invoiceReferenceNotificationMessage.setInvoiceReference("S93159");
invoiceReferenceNotificationMessage.setABSReference("IRM9311");
invoiceReferenceNotificationMessage.setCurrency("GBP");
invoiceReferenceNotificationMessage.setInvoiceAmount(257646);
invoiceReferenceNotificationMessage.setPaidAmount(12475);
invoiceReferenceNotificationMessage.setBalanceAmount(0);
invoiceReferenceNotificationMessage.setValueDate(new Date());
invoiceReferenceNotificationMessage.setRemarks("abrtc");


InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage2 = new InvoiceReferenceNotificationMessage();
invoiceReferenceNotificationMessage.setInvoiceReference("S6315");
invoiceReferenceNotificationMessage.setABSReference("IR11");
invoiceReferenceNotificationMessage.setCurrency("GBP");
invoiceReferenceNotificationMessage.setInvoiceAmount(266546);
invoiceReferenceNotificationMessage.setPaidAmount(12645);
invoiceReferenceNotificationMessage.setBalanceAmount(0);
invoiceReferenceNotificationMessage.setValueDate(new Date());
invoiceReferenceNotificationMessage.setRemarks("abcdgdg");
ndsfd ddsfd
  • 235
  • 1
  • 5
  • 13
  • possible duplicate of [XStream arrayList to and from XML](http://stackoverflow.com/questions/24016334/xstream-arraylist-to-and-from-xml) – Robert Moskal May 21 '15 at 11:38
  • I have answered in your other thread. For future: do not duplicate posts! http://stackoverflow.com/questions/tagged/xstream – Stugal May 21 '15 at 14:37

2 Answers2

0

I seems fairly obvious. Since Mail can only contain one InvoiceReferenceNotificationMessage you will need to create a second 'Mail` object (or reuse the first after generating the XML) to persist the second 'InvoiceReferenceNotificationMessage' object.

Mail m1 = new Mail();
Mail m2 = new Mail()

XStream xstream = new XStream();
xstream.alias("brokermail",Mail.class);

m1.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage1);
m2.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage2);

String abc1 = xstream.toXML(m1);
String abc2 = xstream.toXML(m2);

... OR ...

Mail m1 = new Mail();

XStream xstream = new XStream();
xstream.alias("brokermail",Mail.class);

m1.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage1);

String abc1 = xstream.toXML(m1);

m1.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage2);

String abc2 = xstream.toXML(m1);
Brett Walker
  • 3,566
  • 1
  • 18
  • 36
  • @Breet Thanks but please advise what about if in class mail i could have use arraylist to store objects of class invoiceReferenceNotificationMessage – ndsfd ddsfd May 21 '15 at 11:44
  • I you have control over the design of `Mail`, and it makes sense to allow more than one copy of `InvoiceReferenceNotificationMessage ` per `Mail` object; then go right ahead. – Brett Walker May 21 '15 at 11:45
  • Thanks please advise what changes i need to do in mail class then if possible you can change the mail class i have posted above itself, Thanks in advance – ndsfd ddsfd May 21 '15 at 11:51
  • @ndsfd see my other answer. – Brett Walker May 21 '15 at 11:56
0

Try this version of Mail as a starting point:

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

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

    public void addInvoiceReferenceNotificationMessage(InvoiceReferenceNotificationMessage invoiceReferenceNotificationMessage) {
        this.invoiceReferenceNotificationMessages.add(invoiceReferenceNotificationMessage);
    }

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

You can change/add methods as you like...

Brett Walker
  • 3,566
  • 1
  • 18
  • 36
  • Thanks a lot in this you have provided bot h the options to add single or multiple InvoiceReferenceNotificationMessage class objects can you please advise now in the main class where i am creating the objects of InvoiceReferenceNotificationMessage shall i add the objetc in a list please advise now how i will create the objects pleaseThanks inadvance – ndsfd ddsfd May 21 '15 at 12:00
  • Mail m1 = new Mail(); XStream xstream = new XStream(); xstream.alias("brokermail",Mail.class); m1.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage1); String abc1 = xstream.toXML(m1); m1.setInvoiceReferenceNotificationMessage(invoiceReferenceNotificationMessage2); String abc2 = xstream.toXML(m1); – ndsfd ddsfd May 21 '15 at 12:01
  • It is up to you with the given the methods above. You can create your own list of `InvoiceReferenceNotificationMessage` outside of `Mail` and pass the list to `Mail` or create and pass individual `InvoiceReferenceNotificationMessage` to `Mail`. I prefer the second option when given the chance. – Brett Walker May 21 '15 at 12:05
  • i have use this approch..List invoiceReferenceNotificationMessagest = new ArrayList(); 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); – ndsfd ddsfd May 21 '15 at 12:15