一.引入依赖
<!--jsoup-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
二.解析html
准备一个简单的html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
<div id="data" class="myStyle">hello</div>
</body>
</html>
三.测试
/**
* @author qinxun
* @date 2023-06-02
* @Descripion: 测试jsoup
*/
@RestController
public class JsoupController {
/**
* 解析html 获取里面的属性
*/
@RequestMapping("/getJsoup")
public String getJoupData() throws IOException {
// 通过html文件 获取Document
Document document = Jsoup.parse(new File("E:" + File.separator + "html" + File.separator + "test.html"), "utf-8");
//通过ID获取html元素
Element element = document.getElementById("data");
//获取元素的属性
return element.attr("class");
}
}
查询执行 获取结果
四.解析CSV文件
1、CSV文件
2、Bean类
/**
* @author qinxun
* @date 2023-06-02
* @Descripion: score实体
*/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ScoreBean {
private Long id;
private Double score;
}
3.测试
/**
* @author qinxun
* @date 2023-06-02
* @Descripion: csv测试
*/
@RestController
public class CsvController {
/**
* 获取csv文件中的数据
*/
@RequestMapping("/getCsv")
public List<ScoreBean> getCsvData() throws IOException {
//csv文件地址
String filePath = "E:" + File.separator + "csv" + File.separator + "score.csv";
List<ScoreBean> list = Lists.newArrayList();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filePath));
String line = null;
ScoreBean scoreBean = null;
while ((line = reader.readLine()) != null) {
HashMap<String, Object> hashMap = new HashMap<>();
String[] itemArray = line.split(",");
scoreBean = new ScoreBean(Long.parseLong(itemArray[0]), Double.parseDouble(itemArray[1]));
list.add(scoreBean);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != reader) {
reader.close();
}
}
return list;
}
}
页面结果