1.将资源导入到resources包
2.创建Device类和Result类
Device代码如下:
package com.example.jxl.pojo;
import lombok.AllArgsConstructor;
import lombok.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Device {
private String devName;
private String devID;
private String image;
private String userName;
private String devState;
}
Result代码如下:
package com.example.jxl.pojo;
public class Result {
private Integer code;//1 成功 , 0 失败
private String msg; //提示信息
private Object data; //数据 data
public Result() {
}
public Result(Integer code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static Result success(Object data) {
return new Result(1, "success", data);
}
public static Result success() {
return new Result(1, "success", null);
}
public static Result error(String msg) {
return new Result(0, msg, null);
}
@Override
public String toString() {
return "Result{" +
"code=" + code +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
3.将代码分别填入Device和Result
4.创建工具包
加入代码如下:
package com.example.jxl.utils;
import java.io.File;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XmlParserUtils {
public static <T> List<T> parse(String file , Class<T> targetClass) {
ArrayList<T> list = new ArrayList<T>(); //封装解析出来的数据
try {
//1.获取一个解析器对象
SAXReader saxReader = new SAXReader();
//2.利用解析器把xml文件加载到内存中,并返回一个文档对象
Document document = saxReader.read(new File(file));
//3.获取到根标签
Element rootElement = document.getRootElement();
//4.通过根标签来获取 user 标签
List<Element> elements = rootElement.elements("dev");
//5.遍历集合,得到每一个 user 标签
for (Element element : elements) {
//获取 name 属性
String devName = element.element("devName").getText();
//获取 age 属性
String devID = element.element("devID").getText();
//获取 image 属性
String image = element.element("image").getText();
//获取 gender 属性
String userName = element.element("userName").getText();
//获取 job 属性
String devState = element.element("devState").getText();
//组装数据
Constructor<T> constructor = targetClass.getDeclaredConstructor(String.class, String.class, String.class, String.class, String.class);
constructor.setAccessible(true);
T object = constructor.newInstance(devName,devID, image, userName, devState);
list.add(object);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
5.报错,增添依赖项
如下:
<dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>2.1.3</version> </dependency>
6.创建服务层
DeviceService接口代码:
package com.example.jxl.Server;
import com.example.jxl.pojo.Device;
import java.util.List;
public interface DeviceService {
public List<Device> getXml(String url);
public List<Device> tran(List<Device> devList);
}
DeviceImpl代码:
package com.example.jxl.Server.Impl;
import com.example.jxl.pojo.Device;
import com.example.jxl.Server.DeviceService;
import com.example.jxl.utils.XmlParserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
@Component
public class DeviceImpl implements DeviceService {
@Override
public List<Device> getXml(String url) {
String file = Objects.requireNonNull(this.getClass().getClassLoader().getResource(url)).getFile();
System.out.println(file);
// 得到xml文件的路径 并赋值给file
// 读取xml文件的资源并赋值到链表
List<Device> devList = XmlParserUtils.parse(file, Device.class);
return devList;
}
@Override
public List<Device> tran(List<Device> devList) {
devList.forEach(dev -> {
//处理 DevState 1: 开, 0: 关
String state = dev.getDevState();
if("1".equals(state)){
dev.setDevState("开");
}else if("0".equals(state)){
dev.setDevState("关");
}
});
return devList;
}
}
7.创建控制层:
代码如下:
package com.example.jxl.Controller;
import com.example.jxl.pojo.Device;
import com.example.jxl.pojo.Result;
import com.example.jxl.Server.DeviceService;
import com.example.jxl.utils.XmlParserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//import javax.xml.transform.Result;
import java.util.List;
import java.util.Objects;
@RestController
public class DeviceController {
@Autowired
private DeviceService deviceServer;
@RequestMapping("/listDev")
public Result listDev(){
List<Device> list = deviceServer.getXml("devices.xml");
list = deviceServer.tran(list);
return Result.success(list);
}
}
8.打开浏览器,输入:ocalhost:8080/dev.html
即可看到: