问题描述
public static 我在类中创建了一个main()方法来测试XML文件的构造。 void main(String [] args){
createXmlEmail();
}
这是构建XML文件的方法。 b
$ b pre code $ 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();
列表<收件人> toRecipient = new ArrayList< Recipient>();
toRecipient.add(新收件人([email protected]));
toRecipient.add(新收件人([email protected]));
列表<收件人> ccRecipient = new ArrayList< Recipient>();
ccRecipient.add(新收件人([email protected]));
ccRecipient.add(新收件人([email protected]));
列表<收件人> bccRecipient = new ArrayList< Recipient>();
bccRecipient.add(新收件人([email protected]));
bccRecipient.add(新收件人([email protected]));
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>至;
私人列表<收件人>立方厘米;
私人列表<收件人> BCC;
private String subject;
私人字符串正文;
公共列表<收件人> getTo(){
返回;
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(){
返回密件抄送;
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;
公共收件人(字符串收件人){
this.recipient =收件人;
}
public String getRecipient(){
return recipient;
}
public void setRecipient(String recipient){
this.recipient = recipient;
$ / code $ / pre
当我运行这个主类时,得到任何错误或异常,并输出:
< email>
< to>
<收件人>
<收件人> [email protected]< /收件人>
< / recipient>
<收件人>
<收件人> [email protected]< / recipient>
< / recipient>
< /至>
< cc>
<收件人>
<收件人> [email protected]< /收件人>
< / recipient>
<收件人>
<收件人> [email protected]< /收件人>
< / recipient>
< / cc>
< bcc>
<收件人>
<收件人> [email protected]< /收件人>
< / recipient>
<收件人>
<收件人> [email protected]< /收件人>
< / recipient>
< / bcc>
< subject>科目考试< /科目>
< body>身体测试< / body>
< / email>
但是,我希望它是这样的:
< email>
< to>
<收件人> [email protected]< /收件人>
<收件人> [email protected]< / recipient>
< /至>
< cc>
<收件人> [email protected]< /收件人>
<收件人> [email protected]< /收件人>
< / cc>
< bcc>
<收件人> [email protected]< /收件人>
<收件人> [email protected]< /收件人>
< / bcc>
< subject>科目考试< /科目>
< body>身体测试< / body>
< / email>
我在这里错过了什么?
提前致谢!
解决方案这是因为Recipient类具有属性收件人。如果您标记您的集合@XStreamImplicit,那么外部收件人标记将被消除。
请参阅
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("[email protected]"));
toRecipient.add(new Recipient("[email protected]"));
List<Recipient> ccRecipient = new ArrayList<Recipient>();
ccRecipient.add(new Recipient("[email protected]"));
ccRecipient.add(new Recipient("[email protected]"));
List<Recipient> bccRecipient = new ArrayList<Recipient>();
bccRecipient.add(new Recipient("[email protected]"));
bccRecipient.add(new Recipient("[email protected]"));
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>[email protected]</recipient>
</recipient>
<recipient>
<recipient>[email protected]</recipient>
</recipient>
</to>
<cc>
<recipient>
<recipient>[email protected]</recipient>
</recipient>
<recipient>
<recipient>[email protected]</recipient>
</recipient>
</cc>
<bcc>
<recipient>
<recipient>[email protected]</recipient>
</recipient>
<recipient>
<recipient>[email protected]</recipient>
</recipient>
</bcc>
<subject>subject test</subject>
<body>body test</body>
</email>
But I'd like it to be like this:
<email>
<to>
<recipient>[email protected]</recipient>
<recipient>[email protected]</recipient>
</to>
<cc>
<recipient>[email protected]</recipient>
<recipient>[email protected]</recipient>
</cc>
<bcc>
<recipient>[email protected]</recipient>
<recipient>[email protected]</recipient>
</bcc>
<subject>subject test</subject>
<body>body test</body>
</email>
What am I missing here?
Thanks in advance!
解决方案 This is because Recipient class has property recipient. If you mark your collections @XStreamImplicit, then the outer recipient tags will be eliminated.
这篇关于如何用Java中的XStream构建更好的XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!