Parse HTML table text

Download source code

function TForm1.GetCellText(ATag: TclHtmlTag): string;
begin
  if (ATag <> nil) and (ATag.IsText) then
  begin
    Result := ATag.Text;
  end else
  begin
    Result := GetCellText(ATag.NextTag);
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  i, j, k: Integer;
  cell: TclHtmlTableCell;
  table: TclHtmlTable;
  row: TclHtmlTableRow;
begin
  clHtmlParser1.ParseMethod := pmAll;
  clHtmlParser1.Parse(Memo1.Lines);
 
  Memo2.Lines.Clear();
 
  for i := 0 to clHtmlParser1.Tables.Count - 1 do
  begin
    Memo2.Lines.Add('table ' + IntToStr(i));
 
    table := clHtmlParser1.Tables[i];
    for j := 0 to table.Rows.Count - 1 do
    begin
      Memo2.Lines.Add('row ' + IntToStr(j));
 
      row := table.Rows[j];
      for k := 0 to row.Cells.Count - 1 do
      begin
        cell := row.Cells[i];
        Memo2.Lines.Add(GetCellText(cell));
      end;
    end;
  end;
end;

Add Feedback