Archive for July, 2007

Delphi HTMLParser - Sample 1 - Reading tags

Monday, July 30th, 2007

HTMLParser is really easy to use. I have made an sample project for anyone who likes to use HTMLParser for their project.

Assume you have downloaded HTMLParser , and have extracted the files into your library directory.

First you need to drop the following items to your form.

  • Memo1: TMemo;
  • Edit1: TEdit;
  • OpenDialog1: TOpenDialog;
  • Button1: TButton;
  • Button2: TButton;
  • txtTitle: TEdit;
  • txtDescription: TEdit;
  • txtKeywords: TEdit;
  • Memo2: TMemo;

HTMLParser Sample 1 - reading tag screenshot

2. Add the following code.

Show Code

  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   if (OpenDialog1.Execute) then
  4.   begin
  5.     Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
  6.   end;
  7. end;
  8.  
  9. procedure TForm1.FormCreate(Sender: TObject);
  10. begin
  11.   fHTML := THTMLObject.Create;
  12. end;
  13.  
  14. procedure TForm1.FormDestroy(Sender: TObject);
  15. begin
  16.   fHTML.Free;
  17. end;
  18.  
  19. procedure TForm1.Memo1Change(Sender: TObject);
  20. begin
  21.   fHTML.LoadFromStrings(Memo1.Lines);
  22.   UpdateFields;
  23. end;

3. Create a new procedure in Form1.

This method is used to make calls to fHTML to get TITLE tag, Description and Keywords meta tag

Show Code

  1. procedure TForm1.UpdateFields;
  2. var
  3.   i: Integer;
  4.   metatag: TTagObject;
  5. begin
  6.   //Get title content, just like in Javascipt, tagobject has innerText property
  7.   txtTitle.Text := fHTML.TagByName['TITLE'].innerText;
  8.   //Specify which tag we need look for
  9.   fHTML.TagName := 'META';
  10.   // Reset tag array index to first
  11.   fHTML.First;
  12.   // Look for next META tag
  13.   metatag := fHTML.Next;
  14.   while metatag <> nil do
  15.   begin
  16.     if metatag.CompareValue('NAME', 'DESCRIPTION') or metatag.CompareValue('HTTP-EQUIV', 'DESCRIPTION') then
  17.     begin
  18.       txtDescription.Text := metatag.ReadString('CONTENT', '');
  19.     end else
  20.     if metatag.CompareValue('NAME', 'KEYWORDS') or metatag.CompareValue('HTTP-EQUIV', 'KEYWORDS') then
  21.     begin
  22.       txtKeywords.Text := metatag.ReadString('CONTENT', '');
  23.     end;
  24.     metatag := fHTML.Next;
  25.   end;
  26. end;

4. At this point you can already run this program, you should be able click Browse (button1) to parse any file on your computer or paste the HTML content from clipboard.

The following code is to show you how to get all A tags from an HTML content

Show Code

  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   metatag: TTagObject;
  5. begin
  6.   fHTML.TagName := 'A';
  7.   // Reset tag array index to first
  8.   fHTML.First;
  9.   // Look for next A tag
  10.   metatag := fHTML.Next;
  11.   Memo2.Lines.Clear;
  12.   while metatag <> nil do
  13.   begin
  14.     Memo2.Lines.Add(metatag._RS('href', '(empty url)'));
  15.     metatag := fHTML.Next;
  16.   end;
  17. end;

Download full source code

Delphi component - tiaonImageButton

Thursday, July 26th, 2007

OpenCV Face Detection

Friday, July 20th, 2007