xStream javaBean转为xml时,怎么保留空节点

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xStream javaBean转为xml时,怎么保留空节点相关的知识,希望对你有一定的参考价值。

目前当javaBean中属性name没有值时 ,转为xml后就没有name这个节点了。

我需要的是,即便属性name没有值,转换后的xml也保留name这个节点,如<name/>

使用一个自定义的Converter:

public class Student
   String name;
   int age;
   String teacher;
   public void setName(String name)
       this.name = name;
   
   public String getName()
      return name;
   
.......
......//Like that mention other getter and setter method

public class StudentConverter implements Converter
    public boolean canConvert(Class clazz) 
         return clazz.equals(Test.class);
    
    public void marshal(Object value, HierarchicalStreamWriter writer,
         MarshallingContext context) 
       Student student = (Student ) value;
       writer.startNode("name");
       writer.setValue(test.getName());
       writer.endNode();
       writer.startNode("age");
       writer.setValue(test.getAge());
       writer.endNode();
       writer.startNode("teacher");
       String teacher = test.getTeacher();
       if(teacher == null) 
           writer.setValue("");
       else 
           writer.setValue(teacher);
       writer.endNode();
   
    public Object unmarshal(HierarchicalStreamReader reader,
         UnmarshallingContext context) 
       Student student = new Student();
       test.setName(reader.getNodeName());
       test.setAge(reader.getNodeName());
       test.setTeacher(reader.getNodeName());
       return student;
    




public class App1
    public static void main(String args[]) 
        XStream stream = new XStream(new StaxDriver());
        Student st = new Student();
        st.setName("ABC");
        t.setAge(21);
        stream.registerConverter(new StudentConverter());
        stream.alias("student", Student.class);
        System.out.println(stream.toXML(st));
   

参考技术A 之前我也是用XStream这个框架的,默认就是会这样啊
或者建议你在这个实体类的字段中给个初始值
例如String name = "";
这样就肯定有了本回答被提问者采纳

xstream 复杂结构的xml转javabean

1、引入XStream工具,maven配置如下:
技术分享图片
1     <dependency>
2         <groupId>com.thoughtworks.xstream</groupId>
3         <artifactId>xstream</artifactId>
4         <version>1.4.10</version>
5     </dependency>
View Code

2、要解析的xml格式如下:

技术分享图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <mail-config>
 3     <head>
 4         <systemType>ccp</systemType>
 5     </head>
 6     <mails>
 7         <mail>
 8             <isOneTest>true</isOneTest>
 9             <mialfrom>from11</mialfrom>
10             <mailto>to11</mailto>
11             <mailcc>cc11</mailcc>
12             <mailbcc>bcc11</mailbcc>
13             <mailBody id=‘zh‘>
14                 <TestCode1>testcode1-zh</TestCode1>
15                 <TestCode2>testcode2-zh</TestCode2>
16                 <TestCode3>testcode3-zh</TestCode3>
17             </mailBody>
18             <mailBody id=‘en‘>
19                 <TestCode1>testcode1-en</TestCode1>
20                 <TestCode2>testcode2-en</TestCode2>
21                 <TestCode3>testcode3-en</TestCode3>
22             </mailBody>
23         </mail>
24     </mails>
25 </mail-config>
View Code

3、根据xml的结构定义对应的Java对象。定义的时候使用到相关的注解@XStreamAlias、@XStreamAsAttribute、@XStreamImplicit(具体解析参考https://blog.csdn.net/Robert_mm/article/details/8459879)

根节点类如下:

技术分享图片
 1 import com.thoughtworks.xstream.annotations.XStreamAlias;
 2 import java.util.ArrayList;
 3 import java.util.List;
 4 
 5 @XStreamAlias("mail-config")
 6 public class MailConfig {
 7     private  MailHead head;
 8     private List<Mail> mails=new ArrayList<Mail>();
 9 
10     public MailHead getHead() {
11         return head;
12     }
13 
14     public void setHead(MailHead head) {
15         this.head = head;
16     }
17 
18     public List<Mail> getMails() {
19         return mails;
20     }
21 
22     public void setMails(List<Mail> mails) {
23         this.mails = mails;
24     }
25 }
View Code

第二层定义如下:

head

技术分享图片
 1 import com.thoughtworks.xstream.annotations.XStreamAlias;
 2 
 3 import java.io.Serializable;
 4 
 5 @XStreamAlias("head")
 6 public class MailHead implements Serializable {
 7     private  String systemType;
 8     private  String systemCode;
 9 
10     public String getSystemType() {
11         return systemType;
12     }
13 
14     public void setSystemType(String systemType) {
15         this.systemType = systemType;
16     }
17 
18     public String getSystemCode() {
19         return systemCode;
20     }
21 
22     public void setSystemCode(String systemCode) {
23         this.systemCode = systemCode;
24     }
25 }
View Code

mail类中注意注解@XStreamImplicit(itemFieldName="mailBody"),根据xml结构mailBody是没有根节点的,并排两个作为一个List,所以此处要使用这个注解才能正确解析成功。

技术分享图片
 1 import com.thoughtworks.xstream.annotations.XStreamAlias;
 2 import com.thoughtworks.xstream.annotations.XStreamImplicit;
 3 
 4 import java.io.Serializable;
 5 import java.util.List;
 6 
 7 @XStreamAlias("mail")
 8 public class Mail implements Serializable {
 9     private   boolean isOneTest=false;
10     private  String mialfrom;
11     private  String mailto;
12     private  String mailcc;
13     private  String mailbcc;
14 
15     @XStreamImplicit(itemFieldName="mailBody")
16     private List<MailBodyConfig> mailBodys;
17 
18     public boolean isOneTest() {
19         return isOneTest;
20     }
21 
22     public void setOneTest(boolean oneTest) {
23         isOneTest = oneTest;
24     }
25 
26     public String getMialfrom() {
27         return mialfrom;
28     }
29 
30     public void setMialfrom(String mialfrom) {
31         this.mialfrom = mialfrom;
32     }
33 
34     public String getMailto() {
35         return mailto;
36     }
37 
38     public void setMailto(String mailto) {
39         this.mailto = mailto;
40     }
41 
42     public String getMailcc() {
43         return mailcc;
44     }
45 
46     public void setMailcc(String mailcc) {
47         this.mailcc = mailcc;
48     }
49 
50     public String getMailbcc() {
51         return mailbcc;
52     }
53 
54     public void setMailbcc(String mailbcc) {
55         this.mailbcc = mailbcc;
56     }
57 
58     public List<MailBodyConfig> getMailBodys() {
59         return mailBodys;
60     }
61 
62     public void setMailBodys(List<MailBodyConfig> mailBodys) {
63         this.mailBodys = mailBodys;
64     }
65 }
View Code
技术分享图片
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

import java.io.Serializable;

@XStreamAlias("mailBody")
public class MailBodyConfig implements Serializable {
    @XStreamAsAttribute
    private  String id;
    //xml中的节点名为:TestCode1,解决名称不对应问题
    @XStreamAlias("TestCode1")
    private  String testCode1;
    @XStreamAlias("TestCode2")
    private  String testCode2;
    @XStreamAlias("TestCode3")
    private  String testCode3;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTestCode1() {
        return testCode1;
    }

    public void setTestCode1(String testCode1) {
        this.testCode1 = testCode1;
    }

    public String getTestCode2() {
        return testCode2;
    }

    public void setTestCode2(String testCode2) {
        this.testCode2 = testCode2;
    }

    public String getTestCode3() {
        return testCode3;
    }

    public void setTestCode3(String testCode3) {
        this.testCode3 = testCode3;
    }
}
View Code

 

 

具体调用测试代码如下:

技术分享图片
 1 public  void TestXml()
 2     {
 3         String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
 4                 "<mail-config>\n" +
 5                 "    <head>\n" +
 6                 "        <systemType>ccp</systemType>\n" +
 7                 "    </head>\n" +
 8                 "    <mails>\n" +
 9                 "        <mail>\n" +
10                 "            <isOneTest>true</isOneTest>\n" +
11                 "            <mialfrom>from11</mialfrom>\n" +
12                 "            <mailto>to11</mailto>\n" +
13                 "            <mailcc>cc11</mailcc>\n" +
14                 "            <mailbcc>bcc11</mailbcc>\n" +
15                 "            <mailBody id=‘zh‘>\n" +
16                 "                <TestCode1>testcode1zh</TestCode1>\n" +
17                 "                <TestCode2>testcode2zh</TestCode2>\n" +
18                 "                <TestCode3>testcode3zh</TestCode3>\n" +
19                 "            </mailBody>\n" +
20                 "            <mailBody id=‘en‘>\n" +
21                 "                <TestCode1>testcode1-en</TestCode1>\n" +
22                 "                <TestCode2>testcode2-en</TestCode2>\n" +
23                 "                <TestCode3>testcode3-en</TestCode3>\n" +
24                 "            </mailBody>\n" +
25                 "        </mail>\n" +
26                 "    </mails>\n" +
27                 "</mail-config>";
28         //设置了这个别名才能识别下面 xml 中的 类对象节点的别名,否则要用类全限名称
29         XStream xstream = new XStream();
30         XStream.setupDefaultSecurity(xstream);
31         xstream.autodetectAnnotations(true);
32         xstream.alias("mail-config",MailConfig.class);
33         xstream.allowTypes(new Class[]{MailConfig.class, Mail.class});
34 
35         System.out.print(xml);
36         MailConfig mailconfig=(MailConfig)xstream.fromXML(xml);
37 
38         System.out.print("转换成功!");
39     }
View Code

 

以上是关于xStream javaBean转为xml时,怎么保留空节点的主要内容,如果未能解决你的问题,请参考以下文章

怎么将json文件转成xml文件

XStream-----把JavaBean转换为xml的工具

xstream 复杂结构的xml转javabean

Xstream将XML转换为javabean的问题

JAVA bean与XML互转的利器---XStream

java用XStream把xml转换成javabean的问题