Java 如何使用JSoup解析和格式化HTML文件元素的内容?

Java 如何使用JSoup解析和格式化HTML文件元素的内容?,java,html,parsing,html-table,jsoup,Java,Html,Parsing,Html Table,Jsoup,我被要求使用任何方法以表中特定的(正确且相同的)格式输出给定的代码(但是我选择了JSoup解析器,因为我认为它是最好的解决方案)。我做了一些研究,并观看了一些教程,以了解事情是如何运作的,但我陷入了困境 我已经尝试查看StackOverflow,发现了一个类似的问题,但是,我无法将以前的解决方案应用到我的任务中 我有一个表,有三行八列,其中每个单元格都必须以特定的方式进行格式化(有些格式与全文类似,大写,日期格式)。我想提取每一行,然后尝试使用正则表达式,但是,我只提取了每一行 下面是我试图解析

我被要求使用任何方法以表中特定的(正确且相同的)格式输出给定的代码(但是我选择了JSoup解析器,因为我认为它是最好的解决方案)。我做了一些研究,并观看了一些教程,以了解事情是如何运作的,但我陷入了困境

我已经尝试查看StackOverflow,发现了一个类似的问题,但是,我无法将以前的解决方案应用到我的任务中

我有一个表,有三行八列,其中每个单元格都必须以特定的方式进行格式化(有些格式与全文类似,大写,日期格式)。我想提取每一行,然后尝试使用正则表达式,但是,我只提取了每一行

下面是我试图解析的上述表格

守则:

    ArrayList<String> tableContent = new ArrayList<>();
    File input = new File("[..]task_A1.html");
    Document doc = Jsoup.parse(input, "UTF-8");


    Element table = doc.select("table").get(0);
    Elements rows = table.select("tr");


    for(int i = 0; i < rows.size(); i++){
        Element row = rows.get(i);
        Elements cols = row.select("th");

        tableContent.add(cols.text());
    }

}
ArrayList tableContent=new ArrayList();
文件输入=新文件(“[…]task_A1.html”);
文档doc=Jsoup.parse(输入“UTF-8”);
元素表=单据选择(“表”).get(0);
元素行=表。选择(“tr”);
对于(int i=0;i

2017/10/10
需求(不列颠哥伦比亚省)
新一代(1234.56)
柴油发动机
涡轮增压器
燃料+天然气
Ciclo combinado(3)
一般辅助设备(4)
10102017T0000
需求(不列颠哥伦比亚省)
新一代(1234.56)
柴油发动机
涡轮增压器气体百分比
燃气(3)
西克罗·科米纳多
一般辅助设备(4)
10-10-2017
需求(不列颠哥伦比亚省)
第二代(1234,56)
柴油发动机
涡轮增压器
燃气
西克罗·科米纳多
一般辅助设备
表:

2017/10/10需求量(b.c)发电(1234.56)电机柴油涡轮燃气+燃气循环组合(3)一般辅助(4)

10102017T0000需求量(B.C)发电(1234.56)电机柴油涡轮增压%气体燃料y气体(3)Ciclo组合一般辅助设备(4)

2017年10月10日需求量(b.c)发电(1234,56)电动机柴油涡轮增压燃气和循环燃气联合发电机组

正确的表格输出:

2017年10月10日需求(卑诗省)发电(1234,56)电动机柴油涡轮增压器燃气燃料和循环燃气联合发电厂通用辅助

2017年10月10日需求(卑诗省)发电(1234,56)电动机柴油涡轮增压器燃气燃料和循环燃气联合发电厂通用辅助

2017年10月10日需求(卑诗省)发电(1234,56)电动机柴油涡轮增压器燃气燃料和循环燃气联合发电厂通用辅助

问题是,如何提取/格式化/解析给定的表,以便获得相同且正确格式化的所有单元格?甚至可以通过使用JSoup来完成这项任务,或者有更好的解决方案来解决这个问题吗

如有任何建议,我将不胜感激

<table>
<tr class="primera odd">
<th class="titulo ini" scope="row">2017/10/10</th>
<th class="titulo" scope="col">Demand (b.c)</th>
<th class="titulo" scope="col">Generation(1,234.56)</th>
<th class="titulo" scope="col">Motores diesel</th>
<th class="titulo" scope="col">Turbina de gas</th>
<th class="titulo" scope="col">Fuel + Gas</th>
<th class="titulo" scope="col">Ciclo combinado (3)</th>
<th class="titulo" scope="col">Generación auxiliar (4)</th>
</tr>

<tr class="primera odd">
<th class="titulo ini" scope="row">10102017T0000</th>
<th class="titulo" scope="col">Demand (B.C)</th>
<th class="titulo" scope="col">GENERATION(1234.56)</th>
<th class="titulo" scope="col">Motores diesel</th>
<th class="titulo" scope="col">Turbina%de%gas</th>
<th class="titulo" scope="col">Fuel y Gas(3)</th>
<th class="titulo" scope="col">Ciclo combinado</th>
<th class="titulo" scope="col">Generación auxiliar (4)</th>
</tr>

<tr class="primera odd">
<th class="titulo ini" scope="row">10-10-2017</th>
<th class="titulo" scope="col">Demand (b.c)</th>
<th class="titulo" scope="col">Generation(1234,56)</th>
<th class="titulo" scope="col">Motores diesel</th>
<th class="titulo" scope="col">Turbina de gas</th>
<th class="titulo" scope="col">Fuel y Gas</th>
<th class="titulo" scope="col">Ciclo combinado</th>
<th class="titulo" scope="col">Generación.auxiliar</th>
</tr>
</table>