如何在Java中使用XStream构建外观更好的XML?
我在一个类中创建了一个main()方法来测试XML文件的构造如何在Java中使用XStream构建外观更好的XML?,java,xml,formatting,xstream,Java,Xml,Formatting,Xstream,我在一个类中创建了一个main()方法来测试XML文件的构造 public static void main(String[] args) { createXmlEmail(); } private static void createXmlEmail() { XStream xstream = new XStream(new DomDriver()); xstream.setMode(XStream.NO_REFERENCES); xstream.alias(
public static void main(String[] args) {
createXmlEmail();
}
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);
}
这就是构建XML文件的方法
public static void main(String[] args) {
createXmlEmail();
}
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);
}
private static void createXmlEmail(){
XStream XStream=newxstream(newdomdriver());
setMode(xstream.NO_引用);
别名(“email”,EmailPojo.class);
别名(“收件人”,recipient.class);
EmailPojo ep=新的EmailPojo();
List toRecipient=new ArrayList();
toRecipient.add(新收件人(“user1@somecompany.com"));
toRecipient.add(新收件人(“user2@somecompany.com"));
List ccRecipient=new ArrayList();
ccRecipient.add(新收件人(“user3@somecompany.com"));
ccRecipient.add(新收件人(“user4@somecompany.com"));
List bccrecient=new ArrayList();
b收件人。添加(新收件人(“user5@somecompany.com"));
b收件人。添加(新收件人(“user6@somecompany.com"));
ep.setTo(收件人);
ep.setCc(CCCC接收方);
ep.setBcc(b客户);
ep.设置主体(“主体测试”);
ep.立根体(“车身试验”);
字符串xml=xstream.toXML(ep);
System.out.println(xml);
}
提到的EmailPojo和收件人类定义如下:
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;
}
}
公共静态类EmailPojo{
私人名单;
私人名单抄送;
私人名单密件抄送;
私有字符串主题;
私有字符串体;
公共列表getTo(){
返回;
}
公共无效设置到(列表到){
这个;
}
公共列表getCc(){
返回cc;
}
公共无效setCc(列表cc){
this.cc=cc;
}
公共列表getBcc(){
返回密件抄送;
}
公用事业单位(列表密件抄送){
this.bcc=bcc;
}
公共字符串getSubject(){
返回主题;
}
public void setSubject(字符串主题){
this.subject=主语;
}
公共字符串getBody(){
返回体;
}
公共体(字符串体){
这个身体=身体;
}
}
公共静态类收件人{
私有字符串接收者;
公共收件人(字符串收件人){
this.recipient=收件人;
}
公共字符串getRecipient(){
返回收件人;
}
公共空集合收件人(字符串收件人){
this.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>
user1@company.com
user2@company.com
user3@company.com
user4@company.com
user5@company.com
user6@company.com
科目测验
身体测试
但我希望是这样的:
<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>
user1@company.com
user2@company.com
user3@company.com
user4@company.com
user5@company.com
user6@company.com
科目测验
身体测试
我错过了什么
提前谢谢 这是因为Recipient类具有属性Recipient。如果将集合标记为@XStreamImplicit,则外部收件人标记将被删除
请参见可能必须使用自定义转换器i管理的变通方法来输出下面的xml。虽然没有我想象的那么好看,但它很管用。
user1@company.com user2@company.com
你可以在这篇文章中找到答案:(参见:将对象映射到简单内容)@BlaiseDoughan,谢谢!我去看看!