how to build a better looking XML with XStream in Java?

六月ゝ 毕业季﹏ 提交于 2019-12-12 03:56:20

问题


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



来源:https://stackoverflow.com/questions/14440011/how-to-build-a-better-looking-xml-with-xstream-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!