1. 程式人生 > >xstream 復雜結構的xml轉javabean

xstream 復雜結構的xml轉javabean

AS close 技術 stx details pri () ann sdn

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 復雜結構的xml轉javabean