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

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.lryc.cn/news/2393889.html

相关文章:

  • 立志成为一名优秀测试开发工程师(第七天)——unittest框架的学习
  • 精益数据分析(85/126):营收阶段的核心指标与盈利模型优化——从数据到商业决策的落地
  • 论坛系统(4)
  • 本地Markdown开源知识库选型指南
  • 【.net core】SkiaSharp 如何在Linux上实现
  • 后端项目中静态文案国际化语言包构建选型
  • 前端面经 React常见的生命周期
  • 力扣面试150题--二叉树的层平均值
  • 【Doris入门】Doris初识:分布式分析型数据库的核心价值与架构解析
  • C#面试问题41-60
  • 数据结构与算法学习笔记(Acwing 提高课)----动态规划·区间DP
  • 【合集】Linux——31个普通信号
  • 从0到1搭建AI绘画模型:Stable Diffusion微调全流程避坑指南
  • ASP.NET Core 中JWT的基本使用
  • 未来技术展望
  • 从一到无穷大 #46:探讨时序数据库Deduplicate与Compaction的设计权衡
  • vue3 导出excel
  • 带你手写React中的useReducer函数。(底层实现)
  • day024-网络基础-TCP与UDP、DNS
  • 专场回顾 | 重新定义交互,智能硬件的未来设计
  • 如何把一台电脑作为另外一台电脑的显示器
  • WPS 免登录解锁编辑
  • 【C/C++】线程安全初始化:std::call_once详解
  • 技术分享 | Oracle SQL优化案例一则
  • ​什么是RFID电子标签​
  • 华为手机用的时间长了,提示手机电池性能下降,需要去换电池吗?平时要怎么用能让电池寿命长久一些?
  • BERT***
  • 超级对话2:大跨界且大综合的学问融智学应用场景述评(不同第三方的回应)之二
  • 在Linux环境里面,Python调用C#写的动态库,如何实现?
  • 【Linux 基础知识系列】第三篇-Linux 基本命令