how to build a better looking XML with XStream in

2019-09-02 02:04发布

问题:

I've created a main() method in a class to test the construction of a XML file.

public static void main(String[] args) {
    createXmlEmail();
}

And this is the method that builds the XML file.

private static void createXmlEmail() {
    XStream xstream = new XStream(new DomDriver());
    xstream.setMode(XStream.NO_REFERENCES);
    xstream.alias("email", EmailPojo.class);
    xstream.alias("recipient", Recipient.class);

    EmailPojo ep = new EmailPojo();

    List<Recipient> toRecipient = new ArrayList<Recipient>();
    toRecipient.add(new Recipient("user1@somecompany.com"));
    toRecipient.add(new Recipient("user2@somecompany.com"));

    List<Recipient> ccRecipient = new ArrayList<Recipient>();
    ccRecipient.add(new Recipient("user3@somecompany.com"));
    ccRecipient.add(new Recipient("user4@somecompany.com"));

    List<Recipient> bccRecipient = new ArrayList<Recipient>();
    bccRecipient.add(new Recipient("user5@somecompany.com"));
    bccRecipient.add(new Recipient("user6@somecompany.com"));

    ep.setTo(toRecipient);
    ep.setCc(ccRecipient);
    ep.setBcc(bccRecipient);
    ep.setSubject("subject test");
    ep.setBody("body test");

    String xml = xstream.toXML(ep);
    System.out.println(xml);
}

The EmailPojo and Recipient classes mentioned are defined as:

public static class EmailPojo {
        private List<Recipient> to;
        private List<Recipient> cc;
        private List<Recipient> bcc;
        private String subject;
        private String body;

        public List<Recipient> getTo() {
            return to;
        }

        public void setTo(List<Recipient> to) {
            this.to = to;
        }

        public List<Recipient> getCc() {
            return cc;
        }

        public void setCc(List<Recipient> cc) {
            this.cc = cc;
        }

        public List<Recipient> getBcc() {
            return bcc;
        }

        public void setBcc(List<Recipient> bcc) {
            this.bcc = bcc;
        }

        public String getSubject() {
            return subject;
        }

        public void setSubject(String subject) {
            this.subject = subject;
        }

        public String getBody() {
            return body;
        }

        public void setBody(String body) {
            this.body = body;
        }
    }


public static class Recipient {
    private String recipient;

    public Recipient(String recipient) {
        this.recipient = recipient;
    }

    public String getRecipient() {
        return recipient;
    }

    public void setRecipient(String recipient) {
        this.recipient = recipient;
    }
}

When I run this main class, I don't get any errors or exceptions and the output is:

<email>
  <to>
    <recipient>
      <recipient>user1@company.com</recipient>
    </recipient>
    <recipient>
      <recipient>user2@company.com</recipient>
    </recipient>
  </to>
  <cc>
    <recipient>
      <recipient>user3@company.com</recipient>
    </recipient>
    <recipient>
      <recipient>user4@company.com</recipient>
    </recipient>
  </cc>
  <bcc>
    <recipient>
      <recipient>user5@company.com</recipient>
    </recipient>
    <recipient>
      <recipient>user6@company.com</recipient>
    </recipient>
  </bcc>
  <subject>subject test</subject>
  <body>body test</body>
</email>

But I'd like it to be like this:

<email>
  <to>
    <recipient>user1@company.com</recipient>
    <recipient>user2@company.com</recipient>
  </to>
  <cc>
    <recipient>user3@company.com</recipient>
    <recipient>user4@company.com</recipient>
  </cc>
  <bcc>
    <recipient>user5@company.com</recipient>
    <recipient>user6@company.com</recipient>
  </bcc>
  <subject>subject test</subject>
  <body>body test</body>
</email>

What am I missing here?

Thanks in advance!

回答1:

This is because Recipient class has property recipient. If you mark your collections @XStreamImplicit, then the outer recipient tags will be eliminated.

See Implicit Collections