Java提取markdown中的表格

Java提取markdown中的表格

说明

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

实现步骤

引入pom依赖

xml 复制代码
		<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

java 复制代码
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<>();

    @Override
    public void visit(CustomBlock customBlock) {
        if (customBlock instanceof TableBlock) {
            handleTableBlock((TableBlock) customBlock);
        } else {
            super.visit(customBlock);
        }
    }

    @Override
    public 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;
    }
}

测试用例

java 复制代码
    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。希望可以对其他同学有所帮助吧。

相关推荐
没有bug.的程序员13 小时前
分布式架构未来趋势:从云原生到智能边缘的演进之路
java·分布式·微服务·云原生·架构·分布式系统
AI云原生13 小时前
云原生系列Bug修复:Docker镜像无法启动的终极解决方案与排查思路
运维·服务器·python·docker·云原生·容器·bug
万粉变现经纪人15 小时前
如何解决 pip install -r requirements.txt 私有索引未设为 trusted-host 导致拒绝 问题
开发语言·python·scrapy·flask·beautifulsoup·pandas·pip
毕业设计制作和分享15 小时前
springboot150基于springboot的贸易行业crm系统
java·vue.js·spring boot·后端·毕业设计·mybatis
查士丁尼·绵16 小时前
笔试-九宫格三阶积幻方
python·九宫格·三阶积幻方
l1t18 小时前
DeepSeek辅助利用搬移底层xml实现快速编辑xlsx文件的python程序
xml·开发语言·python·xlsx
大飞记Python18 小时前
部门管理|“编辑部门”功能实现(Django5零基础Web平台)
前端·数据库·python·django
Lxinccode19 小时前
python(55) : python程序设置为Windows快捷方式
windows·python固定到开始·快速调用开始
小梁努力敲代码20 小时前
java数据结构--List的介绍
java·开发语言·数据结构
查士丁尼·绵20 小时前
笔试-羊狼过河
python