Here is a sample project I wrote for someone last year that downloads Saudi
stock data and loads it into a stringgrid. I wish I had a sample file but I do 
not and the website currently is inoperative. You may or may not get the idea
of how to parse XML here but if you need more help, reply to me off list and 
I'll help further.

unit usaudi;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, MSXML2_TLB, Grids, StdCtrls, COMObj, Registry;

const
  TrustedHost = 'http://www2.tadawul.com.sa';
  XMLJspUrl   =
'http://www2.tadawul.com.sa/wps/portal/!ut/p/.cmd/cp/.c/6_1_A9/.ce/7_1_569/.p/5_1_500/.pm/V/.ps/X?'
+
                'x=11&actionPerformed=viewAllmarketAsXML';

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    function CheckTrustedDomain: Boolean;
    procedure GetStockData;
  public
    { Public declarations }
    oXMLDoc:                   IXMLDOMDocument2;
    oXMLHTTP:                  IXMLHTTPRequest;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.CheckTrustedDomain: Boolean;
var
  Reg: TRegistry;
  bZoneOK: Boolean;
begin
  Result := False;
  Reg := TRegistry.Create(KEY_READ);
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Internet
Settings\ZoneMap\Domains\com.sa\www2.tadawul',False);
    try
      bZoneOK := Reg.ReadInteger('http') = 2;
      if bZoneOK then
        Result := True;
    except
      Result := False;
    end;
  finally
    Reg.Free;
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  oXMLDoc  := CreateOleObject('MSXML2.DOMDocument.3.0') as IXMLDOMDocument2;
  oXMLHTTP := CreateOleObject('MSXML2.XMLHTTP') as IXMLHTTPRequest;
  for i := 1 to 10 do
    StringGrid1.ColWidths[i] := 71;
  StringGrid1.ColWidths[0] := 108;
  StringGrid1.Cells[0, 0] := 'Company';
  StringGrid1.Cells[1, 0] := 'L. Price';
  StringGrid1.Cells[2, 0] := 'Chg.';
  StringGrid1.Cells[3, 0] := 'volume';
  StringGrid1.Cells[4, 0] := 'B. Bid';
  StringGrid1.Cells[5, 0] := 'Bid Qty.';
  StringGrid1.Cells[6, 0] := 'B. Offer';
  StringGrid1.Cells[7, 0] := 'Offer Qty.';
  StringGrid1.Cells[8, 0] := 'Open';
  StringGrid1.Cells[9, 0] := 'High';
  StringGrid1.Cells[10, 0] := 'Low';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  oXMLHTTP := nil;
  oXMLDoc  := nil;
end;

procedure TForm1.GetStockData;
var
  LResponse:    String;
  LIndex:       Integer;
  oRoot:        IXMLDOMElement;
  oCompany:     IXMLDOMNode;
begin
  //*********************************************
  // MUST do this for security reasons with IE6+
  //*********************************************
  if not CheckTrustedDomain then
  begin
    MessageDlg('The site ' + TrustedHost + ' is not in the IE Trusted Zone' +
#13 +
    'Please correct this for this demo to work properly', mtError, [mbOK], 0);
    Exit;
  end;
  //*********************************************
  oXMLHTTP.open('GET', XMLJspUrl, False, EmptyParam, EmptyParam);
  oXMLHTTP.send(EmptyParam);
  oXMLHTTP.open('GET', XMLJspUrl, False, EmptyParam, EmptyParam);
  oXMLHTTP.send(EmptyParam);
  LResponse := oXMLHTTP.responseText;
  LResponse := '<?xml version="1.0" encoding="utf-8" ?>' +
               Copy(LResponse, Pos('<companys>', LResponse),
                    Pos('</company></companys>',
LResponse)+21-Pos('<companys>', LResponse));
  LResponse := StringReplace(LResponse, #9, '', [rfReplaceAll]);
  LResponse := StringReplace(LResponse, '&', '&amp;', [rfReplaceAll]);
  oXMLDoc.LoadXML(LResponse);
  oRoot := oXMLDoc.documentElement;
  if Assigned(oRoot) then
  begin
    if oRoot.childNodes.length > 0 then
    begin
      for LIndex := 0 to oRoot.childNodes.length-1 do
      begin
        oCompany := oRoot.childNodes.item[LIndex];
        StringGrid1.Cells[0, StringGrid1.RowCount-1] :=
oCompany.childNodes.item[0].text;
        StringGrid1.Cells[1, StringGrid1.RowCount-1] :=
oCompany.childNodes.item[1].text;
        StringGrid1.Cells[2, StringGrid1.RowCount-1] :=
oCompany.childNodes.item[2].text;
        StringGrid1.Cells[3, StringGrid1.RowCount-1] :=
oCompany.childNodes.item[3].text;
        StringGrid1.Cells[4, StringGrid1.RowCount-1] :=
oCompany.childNodes.item[4].text;
        StringGrid1.Cells[5, StringGrid1.RowCount-1] :=
oCompany.childNodes.item[5].text;
        StringGrid1.Cells[6, StringGrid1.RowCount-1] :=
oCompany.childNodes.item[6].text;
        StringGrid1.Cells[7, StringGrid1.RowCount-1] :=
oCompany.childNodes.item[7].text;
        StringGrid1.Cells[8, StringGrid1.RowCount-1] :=
oCompany.childNodes.item[8].text;
        StringGrid1.Cells[9, StringGrid1.RowCount-1] :=
oCompany.childNodes.item[9].text;
        StringGrid1.Cells[10, StringGrid1.RowCount-1] :=
oCompany.childNodes.item[10].text;
        if StringGrid1.RowCount < oRoot.childNodes.length then
          StringGrid1.RowCount := StringGrid1.RowCount + 1;
      end;
    end;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  GetStockData;
end;

end.


--- Chris Stebbing <[EMAIL PROTECTED]> wrote:

> Hi All,
> 
> I need a crash course in XML parsing, as well as some pointers to 
> some components which will help me do all the hard work.
> 
> My project is to download data from a webpage which provides XML 
> data.  I then need to parse this code and extract certain bits of 
> information.  I have Delphi 2005.  I'd prefer freeware stuff or 
> alternatively cheap components.
> 
> I gather there's components out there that will turn the XML page 
> into a tree-like structure which will allow me to browse "nodes" and 
> extract the data.... That's about the limit of what I understand at the
> moment.
> 
> Any help and pointers you can provide would be most appreciated.
> 
> Cheers,
> Chris.
> 
> 
> 
> _______________________________________________
> Delphi mailing list -> [email protected]
> http://www.elists.org/mailman/listinfo/delphi
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to