1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| procedure TUnThread.Execute;
var
ns: TNetworkState;
rs: TFileStream;
id: TIdHTTP;
ch: string;
f: TFile;
xml : TXMLDocument;
i, t: integer;
begin
FiTraitement := 0;
FiMax := 0;
try
ns := TNetworkState.Create;
// récupération du fichier XML
if ns.IsConnected then
begin
try
id := fAccueil.IdHTTP;
id.Disconnect;
ch := TPath.GetDocumentsPath + PathDelim + 'commentaire.xml';
rs := TFileStream.Create(ch, fmCreate);
id.Get('http://monsite.fr', rs);
finally
FreeAndNil(rs);
end;
end;
// exploitation des données XML
if FileExists(ch) then
begin
try
if not Assigned(xml) then
xml := TXMLDocument.Create(fAccueil);
xml.LoadFromFile(ch);
t := xml.ChildNodes.Nodes['main'].ChildNodes.Count;
fAccueil.ProgressBar1.Max := t;
for i := 0 to t - 1 do
begin
if i = 0 then
begin
fAccueil.qrySelect.SQL.Clear;
fAccueil.qrySelect.SQL.Append('insert into commentaire (id_commentaire, msg_commentaire, note_commentaire,');
fAccueil.qrySelect.SQL.Append('date_commentaire, id_utilisateur, nom_utilisateur, id_whisky)');
fAccueil.qrySelect.SQL.Append('VALUES (:id, :msg, :note, :date, :id_user, :nom_user, :whisky)');
end;
fAccueil.qrySelect.Params.ParamByName('id').AsString := GetValueNode(xml.ChildNodes.Nodes['main'].ChildNodes[i], 'id');
if fAccueil.qrySelect.Params.ParamByName('id').AsString <> '' then
begin
fAccueil.qrySelect.Params.ParamByName('msg').AsString := GetValueNode(xml.ChildNodes.Nodes['main'].ChildNodes[i], 'message');
fAccueil.qrySelect.Params.ParamByName('note').AsString := GetValueNode(xml.ChildNodes.Nodes['main'].ChildNodes[i], 'note');
fAccueil.qrySelect.Params.ParamByName('date').AsString := GetValueNode(xml.ChildNodes.Nodes['main'].ChildNodes[i], 'date');
fAccueil.qrySelect.Params.ParamByName('id_user').AsString := GetValueNode(xml.ChildNodes.Nodes['main'].ChildNodes[i], 'user');
fAccueil.qrySelect.Params.ParamByName('nom_user').AsString := GetValueNode(xml.ChildNodes.Nodes['main'].ChildNodes[i], 'nom');
fAccueil.qrySelect.Params.ParamByName('whisky').AsString := GetValueNode(xml.ChildNodes.Nodes['main'].ChildNodes[i], 'whisky');
fAccueil.qrySelect.ExecSQL();
Inc(FiTraitement);
Synchronize(UpdateCompteur);
end;
end;
finally
FreeAndNil(xml);
DeleteFile(ch)
end;
end;
finally
FreeAndNil(ns);
end;
end;
function TUnThread.GetValueNode(XMLNode: IXMLNode; sNode: string): string;
begin
Result := '';
if not VarIsNull(XMLNode.ChildValues[sNode]) then
Result := XMLNode.ChildValues[sNode];
end;
procedure TUnThread.UpdateCompteur;
begin
fAccueil.ProgressBar1.Value := FiTraitement;
end; |
Partager