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>
成功不是终点,失败也并非末日,重要的是前行的勇气!