当前位置: 首页 > backend >正文

Java提取markdown中的表格

Java提取markdown中的表格

说明

这篇博文是一个舍近求远的操作,如果只需要要对markdown中的表格数据进行提取,完全可以通过正在表达式或者字符串切分来完成。但是鉴于学习的目的,这次采用了commonmark包中的工具来完成。具体实现过程如下

实现步骤

引入pom依赖

		<dependency><groupId>org.commonmark</groupId><artifactId>commonmark</artifactId><version>0.21.0</version></dependency><dependency><groupId>org.commonmark</groupId><artifactId>commonmark-ext-gfm-tables</artifactId><version>0.21.0</version></dependency>

自定义vistor

import org.commonmark.ext.gfm.tables.*;
import org.commonmark.node.*;import java.util.ArrayList;
import java.util.List;public class TableVisitor extends AbstractVisitor {private boolean inHeader = false;private boolean inBody = false;private List<String> currentRow = null;private List<String> headers = new ArrayList<>();private final List<List<String>> rows = new ArrayList<>();@Overridepublic void visit(CustomBlock customBlock) {if (customBlock instanceof TableBlock) {handleTableBlock((TableBlock) customBlock);} else {super.visit(customBlock);}}@Overridepublic void visit(CustomNode customNode) {if (customNode instanceof TableHead) {handleTableHead((TableHead) customNode);} else if (customNode instanceof TableBody) {handleTableBody((TableBody) customNode);} else if (customNode instanceof TableRow) {handleTableRow((TableRow) customNode);} else if (customNode instanceof TableCell) {handleTableCell((TableCell) customNode);} else {super.visit(customNode);}}private void handleTableBlock(TableBlock tableBlock) {// 重置状态inHeader = false;inBody = false;visitChildren(tableBlock);}private void handleTableHead(TableHead tableHead) {inHeader = true;visitChildren(tableHead);inHeader = false;}private void handleTableBody(TableBody tableBody) {inBody = true;visitChildren(tableBody);inBody = false;}private void handleTableRow(TableRow tableRow) {currentRow = new ArrayList<>();visitChildren(tableRow);if (inHeader) {this.headers = currentRow;} else if (inBody) {this.rows.add(currentRow);}}private void handleTableCell(TableCell tableCell) {if (currentRow != null) {currentRow.add(getTextContent(tableCell));}visitChildren(tableCell);}private String getTextContent(Node node) {StringBuilder sb = new StringBuilder();Node child = node.getFirstChild();while (child != null) {if (child instanceof Text) {sb.append(((Text) child).getLiteral());}child = child.getNext();}return sb.toString().trim();}public List<String> getTableHeaders() {return headers;}public List<List<String>> getTableRows() {return rows;}
}

测试用例

public static void main(String[] args) {String content = """| 姓名       | 性别   | 班级        | 年龄          ||--------------|------|--------------------|--------------------|| 张三       |  男    |   兴趣一班                 |             17       || 李四         | 男 | 兴趣一班  | 16  |""";List<Extension> extensions = Arrays.asList(TablesExtension.create());Parser parser = Parser.builder().extensions(extensions).build();Node document = parser.parse(content);TableVisitor visitor = new TableVisitor();document.accept(visitor);List<String> tableHeaders = visitor.getTableHeaders();List<List<String>> tableRows = visitor.getTableRows();System.out.println("表头: " + tableHeaders);System.out.println("表格行数据: "+ tableRows);}

总结

由于没有在commonmark中找到我们需要的vistor,所以自定义了vistor。希望可以对其他同学有所帮助吧。

http://www.xdnf.cn/news/9236.html

相关文章:

  • go并发与锁之sync.Mutex入门
  • 国11阶乘约数-质因数分解
  • C/C++的OpenCV的锐化
  • vue 前端请求跨域解决办法
  • 九级融智台阶与五大要素协同的量子化解析
  • MGAug:图像变形潜空间中的多模态几何增强|文献速递-深度学习医疗AI最新文献
  • 端口 3389 服务 ms - wbt - server 漏洞修复方法
  • 你的网站真的安全吗?如何防止网站被攻击?
  • 联软科技统一安全工作空间:零信任架构下的远程办公数据安全守护者
  • 每天掌握一个Linux命令 - sqlite3
  • EasyRTC嵌入式SDK音视频实时通话助力WebRTC技术与智能硬件协同发展
  • Nginx 配置文件深度解析:从核心模块到扩展机制
  • WPF【11_4】WPF实战-重构与美化(MVVM 架构)
  • 【elasticsearch 7 或8 的安装及配置SSL 操作指引】
  • 【Doris入门】Doris初识:分布式分析型数据库的核心价值与架构解析
  • 关于空调温度控制仿真模型的详细技术文档,包含数学模型、Python实现和系统分析
  • 引导者之歌------------嵌入式软件面试问题集成
  • 修改SpringBootApplication类的入参后,引用外部yml的启动命令要修改
  • ArcGIS Pro 3.4 二次开发 - 地理处理
  • 计算机网络练习题
  • 自动生成提示技术突破:AUTOPROMPT重塑语言模型应用
  • Java+Playwright自动化-2-环境准备与搭建-基于Maven
  • Jenkins实践(9):pipeline构建历史展示包名和各阶段间传递参数
  • C++:设计模式--工厂模式
  • linux安装MYSQL
  • 图论学习笔记 5 - 最小树形图
  • leetcode hot100刷题日记——22.只出现一次的数字
  • Python安装、pycharm配置和添加库下载
  • 投影机光源三代发展史:从高压汞灯、白光 LED 到三色光源
  • 基于51单片机的音乐盒点阵屏proteus仿真