Search code examples
javaxmlemailxstream

how to deserialize a list of emails on a xml file in Java?


I have an application that will spit out a XML file with the contents of a email. This XML file will be parsed by a different application and will deliver the email. The part where the application sends the email is validated.

The method that actually sends the email is this:

public void sendEmail(List<String> toRecipients, List<String> ccRecipients, List<String> bccRecipients, String subject, String body) { 

// code.. 

}

The test email I'm trying to send should come from this XML file:

<?xml version="1.0" encoding="utf-8"?>
<email>
  <to>
    <recipient>user1@somecompany.com</recipient>
    <recipient>user2@somecompany.com</recipient>
  </to>
  <cc>
    <recipient>user3@somecompany.com</recipient>
  </cc>
  <bcc>
    <recipient>user5@somecompany.com</recipient>
  </bcc>
  <subject>test ABC </subject>
  <body><h1>test XYZ</h1></body>
</email>

I'm using the XStream library, and my problem resides on parsing a list of . I've tried a few different approaches, but am stuck. The XML parsing method is:

private void parseXmlFile(String xmlFilePath) {
        XStream xstream = new XStream(new DomDriver());

        xstream.alias("email", EmailPojo.class);
        xstream.alias("recipient", Recipient.class);
        xstream.alias("to", To.class);
        xstream.alias("cc", Cc.class);
        xstream.alias("bcc", Bcc.class);

        xstream.addImplicitCollection(To.class, "to", "to", Recipient.class);
        // xstream.addImplicitCollection(To.class, "to");
        // xstream.addImplicitCollection(Cc.class, "cc");
        // xstream.addImplicitCollection(Bcc.class, "bcc");

        EmailPojo emailPojo = new EmailPojo();

        StringBuilder sb = new StringBuilder();
        try {
            // filename is filepath string
            BufferedReader br = new BufferedReader(new FileReader(new File(xmlFilePath)));
            String line;

            while ((line = br.readLine()) != null) {
                sb.append(line.trim());
            }
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        List<EmailPojo> emailPojoList = new ArrayList<EmailPojo>();

        try {
            emailPojoList = (List<EmailPojo>) xstream.fromXML(sb.toString());

        } catch (Exception e) {
            emailPojo = null;// TODO: handle exception
        }
    }

THis seems straight forward enough, but I'm not able to get this going. Wha am I missing here? What's wrong here?

Thanks in advance!

Edit: forgot the exception output:

Exception in thread "main" com.thoughtworks.xstream.InitializationException: No field "to" for implicit collection


Solution

  • The line:

    xstream.addImplicitCollection(To.class, "to", "to", Recipient.class);
    

    Is saying there's a collection field on the class To called to that is where the instances of Recipient should be added.

    Without seeing the To class this is a guess, but I reckon the field on collection field on the class To is more likely to be called recipients which should be registered with:

    xstream.addImplicitCollection(To.class, "recipients", Recipient.class);
    

    See the JavaDoc for xstream.addImplicitCollection(Class, String, Class)