在网络通讯中,对方传的数据经常是XML格式包装的数据集合。在Java开发中,我们如何将XML转成Java实体类呢?
对方发送的报文
<ns2:response xmlns:ns2="http://service.zxl.cn/test/xml">
<school>
<location>长江边上</location>
<level>初中</level>
<scale>很大</scale>
</school >
<student>
<height>150cm</height>
<gender>女校</gender>
<scale>很多</scale>
</student>
<teacher>
<teacherAttribute id="gender">
<attributeValue>男的</attributeValue>
</teacherAttribute>
<teacherAttribute id="height">
<attributeValue>177cm</attributeValue>
</teacherAttribute>
<teacherAttribute id="age">
<attributeValue>22岁</attributeValue>
</teacherAttribute>
</teacher>
</ns2:response>
在实体类上加上注解
package com.zxl.bean.xmlStudy;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@Data
@AllArgsConstructor
@NoArgsConstructor
@XmlRootElement(name = "response",namespace="http://service.zxl.cn/test/xml")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class XmlInfo {
@XmlElement(name = "school")
private School school;
@XmlElement(name = "student")
private Student student;
@XmlElement(name = "teacher")
private Teacher teacher;
}
@XmlRootElement 根节点的注解,里面填两个参数,
一个是name,就是根节点的名称,叫做response我们就填response。
一个是namespace,根节点的命名空间,填入xml中后面跟上的url。(不填报错)
@XmlAccessorType(XmlAccessType.FIELD)
将所有属性均绑定在bean上(默认只绑public的属性,加上后绑定所有的)
@XmlElement(name = “school”)
报文分为3部分,将对应的部分名称填上
package com.zxl.bean.xmlStudy;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@Data
@AllArgsConstructor
@NoArgsConstructor
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement(name = "school")
public class School {
@XmlElement(name = "location")
private String location;
@XmlElement(name = "level")
private String level;
@XmlElement(name = "scale")
private String scale;
}
继续填上@XmlRootElement和 @XmlElement注解
package com.zxl.bean.xmlStudy;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@Data
@AllArgsConstructor
@NoArgsConstructor
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement(name = "student")
public class Student {
@XmlElement(name = "height")
private String height;
@XmlElement(name = "gender")
private String gender;
@XmlElement(name = "scale")
private String scale;
}
继续填上@XmlRootElement和 @XmlElement注解
这里的id属性,没有单独放出来
我们在建立一个类,单独放id和里面的attributeValue
package com.zxl.bean.xmlStudy;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement(name = "teacher")
public class Teacher {
@XmlElement(name = "teacherAttribute")
private List<TeacherAttribute> teacherAttributes;
}
package com.zxl.bean.xmlStudy;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.xml.bind.annotation.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement(name = "school")
public class TeacherAttribute {
@XmlAttribute(name="id")
private String id;
@XmlElement(name = "attributeValue")
private String attributeValue;
}
使用 @XmlAttribute注解,将属性值绑定到id上
解析代码
private String getXml() throws IOException {
// 测试xml转bean
InputStream path = this.getClass().getResourceAsStream("/1.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(path,"utf-8"));
StringBuffer buffer=new StringBuffer();
String line="";
while((line=reader.readLine())!=null){
buffer.append(line);
}
reader.close();
return buffer.toString();
}
读取xml的方法
@GetMapping("/testXml2Bean")
public XmlInfo testXml2Bean() throws IOException, JAXBException {
String xml = this.getXml();
StringReader reader = new StringReader(xml);
JAXBContext jaxbContext = JAXBContext.newInstance(XmlInfo.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
XmlInfo xmlVo = (XmlInfo) jaxbUnmarshaller.unmarshal(reader);
return xmlVo;
}
转换方法
测试
成功转换!
(有问题留言)