Parse simple HTML Tables – embarcadero.delphi.xml

like this:

<TABLE cellpadding=3 cellspacing=0 width="100%"><TR><TD align="right" 
class="cartbasic"><P class="smallfade">First name</P></TD><TD 
class="cartbasic"><P>Peter</P></TD></TR>
<TR><TD align="right" class="cartbasic"><P class="smallfade">Last 
name</P></TD><TD class="cartbasic"><P>Smith</P></TD></TR>
<TR><TD align="right" class="cartbasic"><P 
class="smallfade">Address</P></TD><TD class="cartbasic"><P>Home street 
46</P></TD></TR>
<TR><TD align="right" class="cartbasic"><P 
class="smallfade">ZIP</P></TD><TD class="cartbasic"><P>87300</P></TD></TR>
</TABLE>

and the information taken out from there looks like this:

  First name  Peter
  Last name   Smith
  Address     Home street 46
  ZIP         87300

So, are there any obvious, simple HTML parse algorithms available for 
Delphi that could do this?

There are a lot of HTML-parsers in general. I have studied for instance 
the parsing code from this free Browser component:
http://pbear.com/htmlviewers.html.
After two days of studying, I have to admit that I can't get a grab of 
this parser. I do not get how to use this nice HTML-tool to parse data 
from my Html-tables. The code is far too advanced and complicated for my 
tiny need.

I also found that TWebBrowser component would be able to parse those 
HTML-tables, like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  i, j: Integer;
  ovTable: OleVariant;
begin
  ovTable := WebBrowser1.OleObject.Document.all.tags('TABLE').item(0);
  for i := 0 to (ovTable.Rows.Length - 1) do
  begin
    for j := 0 to (ovTable.Rows.Item(i).Cells.Length - 1) do
    begin
      StringGrid1.Cells[j+1, i+1] :=
        ovTable.Rows.Item(i).Cells.Item(j).InnerText;
    end;
  end;

This way the data goes nicely go TStringGrid.

 

Finding and replacing a string within a text file

Finding and replacing a string within a text file


Try loading the html file into a RichEdit 
The following example shows how to search and replace in the RichEdit

Var 
  TheFile : String; 
  SearchStringPos : Integer; 
  YourSearchString : ShortString; 
  YourReplaceString : ShortString; 
begin 
  RichEdit1.PlainText:=true; 
  RichEdit1.Lines.LoadFromFile(‘YourFile.html’); 
  YourSearchString := ‘<BR>’; 
  YourReplaceString := ‘<HR>’; 
  TheFile:=RichEdit1.Text; 
  SearchStringPos:=Pos(YourSearchString,TheFile); 
  While SearchStringPos > 0 do 
  begin 
    RichEdit1.SelStart:=SearchStringPos-1; 
    RichEdit1.SelLength:=length(YourSearchString); 
    RichEdit1.SelText:=YourReplaceString; 
    TheFile:=RichEdit1.Text; 
    SearchStringPos:=Pos(YourSearchString,TheFile) 
  end; 
  RichEdit1.Lines.SaveToFile(‘YourFile.html’); 
end;