如何用Java中的XStream构建更好的XML? [英] how to build a better looking XML with XStream in Java?

查看:174
本文介绍了如何用Java中的XStream构建更好的XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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(新收件人(user1@somecompany.com));
toRecipient.add(新收件人(user2@somecompany.com));

列表<收件人> ccRecipient = new ArrayList< Recipient>();
ccRecipient.add(新收件人(user3@somecompany.com));
ccRecipient.add(新收件人(user4@somecompany.com));

列表<收件人> bccRecipient = new ArrayList< Recipient>();
bccRecipient.add(新收件人(user5@somecompany.com));
bccRecipient.add(新收件人(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>至;
私人列表<收件人>立方厘米;
私人列表<收件人> 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>
<收件人>
<收件人> user1@company.com< /收件人>
< / recipient>
<收件人>
<收件人> user2@company.com< / recipient>
< / recipient>
< /至>
< cc>
<收件人>
<收件人> user3@company.com< /收件人>
< / recipient>
<收件人>
<收件人> user4@company.com< /收件人>
< / recipient>
< / cc>
< bcc>
<收件人>
<收件人> user5@company.com< /收件人>
< / recipient>
<收件人>
<收件人> user6@company.com< /收件人>
< / recipient>
< / bcc>
< subject>科目考试< /科目>
< body>身体测试< / body>
< / email>

但是,我希望它是这样的:

 < email> 
< to>
<收件人> user1@company.com< /收件人>
<收件人> user2@company.com< / recipient>
< /至>
< cc>
<收件人> user3@company.com< /收件人>
<收件人> user4@company.com< /收件人>
< / cc>
< bcc>
<收件人> user5@company.com< /收件人>
<收件人> user6@company.com< /收件人>
< / 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("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!

解决方案

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

这篇关于如何用Java中的XStream构建更好的XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆