Archive for the 'Delphi code' Category

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

REST web service with Delphi

Monday, March 26th, 2007

When I was looking for examples of how to make REST calls in Delphi 7, I found
XML-RPC for Delphi. XML-RPC for Delphi is an object library that utilises Open Source Indy Components and helps your application connects REST web service. XML-RPC comes full source code and some examples, but does not come any documentation.

Here is a round up of what XML-RPC supports:

  • Basic data types, string, interger, datetime etc.
  • Array data type
  • Struct data type
  • Automatic data type conversion between Delphi data type and REST data type
  • Base64 string conversion

I am current using XML-RPC to develop a Wordpress client in Delphi. I will post more result and findings in the future.

Delphi code part 4 - Bitmap

Wednesday, March 7th, 2007

After I learned how colour can be manupilated, I started to work on bitmaps. Back then I even borrowed a book to learn how bitmaps can be modified, like blur, sharp, find edge, etc. The technique was called filtering.

Here is the code

Show Code | Download Grafman.pas

I also written another unit which is required by the above unit (grafman.pas), it contains all useful function for tile bitmaps, apply a mask to a bitmap, draw a 3D text, etc

Show Code | Download DrawMan.pas