我在一个类中创建了一个 main() 方法来测试 XML 文件的构造。
public static void main(String[] args) {
createXmlEmail();
}
这就是构建 XML 文件的方法。
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);
}
提到的 EmailPojo 和 Recipient 类定义为:
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;
}
}
当我运行这个主类时,我没有收到任何错误或异常,输出是:
<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>
但我希望它是这样的:
<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>
我在这里想念什么?
提前致谢!