java解析XML映射成對象實體


 

1.創建一個XML文件:config.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <root >
 3 
 4     <school name="第一初級中學" description="學校信息">
 5         <grade name="九年級" description="年級信息" >
 6             <classes name="一班" description="班級信息" master="張非" studentCount="40" />
 7             <classes name="二班" description="班級信息" master="李虧" studentCount="38" />
 8             <classes name="三班" description="班級信息" master="關雨" studentCount="39" />
 9             <classes name="四班" description="班級信息" master="趙芸" studentCount="41" />
10         </grade>
11     </school>
12 
13     <school name="第二初級中學" description="學校信息">
14         <grade name="九年級" description="年級信息" >
15             <classes name="一班" description="班級信息" master="張非" studentCount="40" />
16             <classes name="二班" description="班級信息" master="李虧" studentCount="38" />
17             <classes name="三班" description="班級信息" master="關雨" studentCount="39" />
18             <classes name="四班" description="班級信息" master="趙芸" studentCount="41" />
19         </grade>
20     </school>
21 
22 </root>

 

2.寫一個解析工具類:XMLUtil.java

 1 package org.example.util;
 2 
 3 import javax.xml.bind.JAXBContext;
 4 import javax.xml.bind.Unmarshaller;
 5 import java.io.*;
 6 
 7 /**
 8  * 將XML轉換成Object
 9  */
10 public class XMLUtil {
11 
12     /**
13      * 將String類型的xml轉換成對象
14      */
15     public static Object convertXmlStrToObject(Class<?> clazz, String xmlStr) {
16         Object xmlObject = null;
17         try {
18             JAXBContext context = JAXBContext.newInstance(clazz);
19             // 進行將Xml轉成對象的核心接口
20             Unmarshaller unmarshal = context.createUnmarshaller();
21             StringReader sr = new StringReader(xmlStr);
22             xmlObject = unmarshal.unmarshal(sr);
23         } catch (Exception e) {
24             e.printStackTrace();
25         }
26         return xmlObject;
27     }
28 
29     /**
30      * 將file類型的xml轉換成對象
31      */
32     public static Object convertXmlFileToObject(Class<?> clazz, String xmlPath) {
33         Object xmlObject = null;
34         try {
35             JAXBContext context = JAXBContext.newInstance(clazz);
36             Unmarshaller unmarshaller = context.createUnmarshaller();
37             InputStreamReader isr=new InputStreamReader(new FileInputStream(xmlPath),"UTF-8");
38             xmlObject = unmarshaller.unmarshal(isr);
39         } catch (Exception e) {
40             e.printStackTrace();
41         }
42         return xmlObject;
43     }
44 
45 }

 

3.按照XML結構,寫出將要映射的實體類:

  1)Root.java

 1 package org.example.entity;
 2 
 3 import lombok.Data;
 4 
 5 import javax.xml.bind.annotation.XmlRootElement;
 6 import java.util.List;
 7 
 8 @Data
 9 //注解用於標記當前類是XML的根節點,name值為根節點名
10 @XmlRootElement(name = "root")
11 public class Root {
12 
13     /**
14      * 注意:屬性名稱要跟xml節點名統一
15      */
16     private List<School> school;
17 
18 }

  2)School.java

 1 package org.example.entity;
 2 
 3 import lombok.Setter;
 4 
 5 import javax.xml.bind.annotation.XmlAttribute;
 6 import javax.xml.bind.annotation.XmlTransient;
 7 import java.util.List;
 8 
 9 @Setter
10 public class School {
11 
12     /**
13      *  注解 @XmlAttribute 表示該屬性是XML節點的屬性
14      */
15     @XmlAttribute
16     private String name;
17     @XmlAttribute
18     private String description;
19 
20 
21     private List<Grade> grade;
22 
23     /**
24      * @XmlTransient 注解解決 JavaBean 屬性名稱與字段名稱之間的名稱沖突
25      * @return
26      */
27     @XmlTransient
28     public String getName() {
29         return name;
30     }
31 
32     @XmlTransient
33     public String getDescription() {
34         return description;
35     }
36 
37     public List<Grade> getGrade() {
38         return grade;
39     }
40 
41     @Override
42     public String toString() {
43         return "School{" +
44                 "name='" + name + '\'' +
45                 ", description='" + description + '\'' +
46                 ", grade=" + grade +
47                 '}';
48     }
49 }

 

  3)Grade.java

 1 package org.example.entity;
 2 
 3 import lombok.Setter;
 4 
 5 import javax.xml.bind.annotation.XmlAttribute;
 6 import javax.xml.bind.annotation.XmlTransient;
 7 import java.util.List;
 8 
 9 @Setter
10 public class Grade {
11 
12     @XmlAttribute
13     private String name;
14     @XmlAttribute
15     private String description;
16 
17     private List<Classes> classes;
18 
19     @XmlTransient
20     public String getName() {
21         return name;
22     }
23 
24     @XmlTransient
25     public String getDescription() {
26         return description;
27     }
28 
29     public List<Classes> getClasses() {
30         return classes;
31     }
32 
33     @Override
34     public String toString() {
35         return "Grade{" +
36                 "name='" + name + '\'' +
37                 ", description='" + description + '\'' +
38                 ", classes=" + classes +
39                 '}';
40     }
41 }

 

  4)Classes.java

 1 package org.example.entity;
 2 
 3 import lombok.Setter;
 4 
 5 import javax.xml.bind.annotation.XmlAttribute;
 6 import javax.xml.bind.annotation.XmlTransient;
 7 
 8 @Setter
 9 public class Classes {
10 
11     @XmlAttribute
12     private String name;
13     @XmlAttribute
14     private String master;
15     @XmlAttribute
16     private String description;
17     @XmlAttribute
18     private Integer studentCount;
19 
20     @XmlTransient
21     public String getName() {
22         return name;
23     }
24 
25     @XmlTransient
26     public String getMaster() {
27         return master;
28     }
29 
30     @XmlTransient
31     public Integer getStudentCount() {
32         return studentCount;
33     }
34 
35     @XmlTransient
36     public String getDescription() {
37         return description;
38     }
39 
40     @Override
41     public String toString() {
42         return "Classes{" +
43                 "name='" + name + '\'' +
44                 ", master='" + master + '\'' +
45                 ", description='" + description + '\'' +
46                 ", studentCount=" + studentCount +
47                 '}';
48     }
49 }

 

4.編寫一個測試類

 1 package org.example;
 2 
 3 import org.example.entity.Root;
 4 import org.example.util.XMLUtil;
 5 
 6 public class Test {
 7 
 8     public static void main(String[] args) {
 9 
10         String path = "config.xml";
11         Root root = (Root) XMLUtil.convertXmlFileToObject(Root.class, path);
12         System.out.println(root);
13 
14     }
15 }

 

5.文件結構

  

 

 

 

 

附:lambok 引用

1     <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
2     <dependency>
3       <groupId>org.projectlombok</groupId>
4       <artifactId>lombok</artifactId>
5       <version>1.18.20</version>
6       <scope>provided</scope>
7     </dependency>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM