是否有用于查询 HTML 表的 SQL 包装器
Is there an SQL wrapper to query an HTML table
关于如何将SQL查询的结果格式化为HTML表有很多问题,但我想走另一条路 - 给定一个带有标题行的任意HTML表,我希望能够使用SQL(或类似SQL的语言)从一行或多行中提取信息。陈述起来很简单,但显然不是那么容易完成。
最终,我更喜欢使用 libtidy 或 JSoup 之类的东西正确解析 HTML,但是虽然 API 文档通常是合理的,但当涉及到实际使用它们的示例或教程时,您通常会找到一个提取
使用 JSoup 将表转换为元组列表的简单代码如下所示:
public class Main {
public static void main(String[] args) throws Exception {
final String html =
"<html><head/><body>" +
"<table id="example">" +
"<tr><td>John</td><td>Doe</td></tr>" +
"<tr><td>Michael</td><td>Smith</td>" +
"</table>" +
"</body></html>";
final List<Tuple> tuples = parse (html, "example");
//... Here the table is parsed
}
private static final List<Tuple> parse(final String html, final String tableId) {
final List<Tuple> tuples = new LinkedList<Tuple> ();
final Element table = Jsoup.parse (html).getElementById(tableId);
final Elements rows = table.getElementsByTag("tr");
for (final Element row : rows) {
final Elements children = row.children();
final int childCount = children.size();
final Tuple tuple = new Tuple (childCount);
for (final Element child : children) {
tuple.addColumn (child.text ());
}
}
return tuples;
}
}
public final class Tuple {
private final String[] columns;
private int cursor;
public Tuple (final int size) {
columns = new String[size];
cursor = 0;
}
public String getColumn (final int no) {
return columns[no];
}
public void addColumn(final String value) {
columns[cursor++] = value;
}
}
从这里开始,您可以例如使用 H2 创建一个内存表并使用常规 SQL。
相关文章:
- 如何在c++17中制作一个模板包装器/装饰器
- std::vector的包装器,使数组的结构看起来像结构的数组
- 如何在c++迭代器类型中包装std::chrono
- 是否可以用"iostream"包装现有的TCP/OOpenSSL会话
- 用pybind11包装C++抽象类时出错
- 为左值和右值的包装器实现C++范围
- C结构,其指针将被包装在unique_ptr中
- 如何包装第三方DLL在R中使用
- 如何使用c++在VS 2019上运行SQL查询
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 在类型和包装器之间reinterpret_cast是否安全<Type>?
- 将 N-arg 函数包装到另一个函数中
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 包装一个对象并假装它是一个 int
- 使用 Python Extension API 包装复杂C++类
- 外壳包装器句柄/执行交互式命令管道C++ UNIX
- 包装C++类时不完整的类型 GLFWwindow
- 为 Sql 服务器实现 odbc 包装器.将数据库数据读取为字符或要求驱动程序将数据转换为 C 类型
- 是否有用于查询 HTML 表的 SQL 包装器
- 在Xubuntu上使用SOCI(sql包装器),简单的程序在编译时失败