Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - XML-Datei in Listbox


colaka - Di 25.02.14 06:45
Titel: XML-Datei in Listbox
Hallo,

von der EZB lade ich mir die Wechselkurse in Form einer XML-Datei und möchte sie in einer Listbox darstellen. Da ich mich mit XML-Dateien nicht auskenne, mache ich das jetzt über Stringfunktionen:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
  ListBoxAlle.Clear;
  X := XMLKurse.Text;
  while Length(X) > 60 do
  begin
    i := Pos('currency=', X);
    ListBoxAlle.Items.Add(Copy(X, i+103) + '=' + Copy(X, i+21, Pos('/',Copy(X, i+21))-2));
    X := Copy(X, i + 21, Length(X));
  end;


Das funktioniert zwar, aber ich glaube, für XML gibt es spezielle Befehle.

Wie macht man es richtig?


Delete - Di 25.02.14 09:20

Wieso nimmst du dann nicht einfach das Zip-File [http://www.ecb.europa.eu/stats/eurofxref/eurofxref.zip?09221608ccf718cc0f630708c3f90c0e], das dort ebenso zum Download angeboten wird? Dort drinnen befindet sich nämlich eine CSV-Datei [http://de.wikipedia.org/wiki/CSV_%28Dateiformat%29] (Comma-Separated Values), die einen im Verhältnis zur XML-Datei [http://de.wikipedia.org/wiki/XML] (Extensible Markup Language) sehr einfachen Aufbau hat.


jasocul - Di 25.02.14 09:52

Es gibt in der Komponenten-Palette unter Internet eine Komponente TXMLDocument.
Ich verarbeiten damit auch die EZB-Kurse. Ist allerdings dienstlicher Source und ich kann den daher nicht veröffentlichen. Aber mehr als eine Bildschirmseite Source ist das auch nicht.


Delete - Di 25.02.14 14:33

Reference rates der European Central Bank aus dem XML-File auslesen

http://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html

http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml


Delphi-Quelltext
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:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, comobj,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;

type
  TForm2 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    IdHTTP1: TIdHTTP;
    cbXML: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;
  Target : String;

implementation

{$R *.dfm}

procedure MM(s:string); begin Form2.Memo1.Lines.Add(s); end;

procedure ReadXMLFile2(const FileName:TFileName);
const Msxml2_DOMDocument='MSXML2.DOMDocument';//'Msxml2.DOMDocument.6.0';
var XmlDoc, Nodes : OleVariant;
    sXMLTime, sXMLRate1, sXMLRate2, CURR : String;
    i : Integer;
begin
  XmlDoc := CreateOleObject(Msxml2_DOMDocument);

  sXMLTime:= '*/*/*/@time';
  sXMLRate1:= '*/*/*/*/@currency';
  sXMLRate2:= '*/*/*/*/@rate';

  try
    XmlDoc.Async := False;
    XmlDoc.Load(FileName);
    XmlDoc.SetProperty('SelectionLanguage','XPath');
    if (XmlDoc.parseError.errorCode <> 0then
       raise Exception.CreateFmt('Error in Xml Data %s',[XmlDoc.parseError]);

  try
    Nodes := XmlDoc.selectNodes(sXMLTime);
    MM(Nodes.Item(0).Text);
    Form2.Label1.caption:= Nodes.Item(0).Text;
  except on E:Exception do MM('0');  end;

  CURR:= 'USD';//Beispiel
try
    Nodes := XmlDoc.selectNodes(sXMLRate1);
  for I := 0 to 1000 do
    BEGIN
    if Nodes.Item(I).Text = CURR then
    BEGIN
      Form2.Label2.caption:= Nodes.Item(I).Text;
      Nodes := XmlDoc.selectNodes(sXMLRate2);
      Form2.Label3.caption:= Nodes.Item(I).Text;
      Break;
    END;
    END;
except on E:Exception do MM(CURR+' not exists!');  end;

  finally
   XmlDoc :=Unassigned;
  end;
end;

procedure TForm2.FormCreate(Sender: TObject);
var URL : String;
    fs: TFileStream;    //Target : String; global
begin
  URL:= 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
  Target:= 'D:\eurofxref-daily.xml';
  fs := TFileStream.Create(Target, fmCreate);
  try
      IdHTTP1.Get(URL, fs);
  finally
    fs.Free;
  end;
end;

procedure TForm2.FormShow(Sender: TObject);
var i : Integer;
begin
MM('Euro foreign exchange reference rates');
MM('The reference rates are usually updated by 3 p.m. C.E.T.');
MM('They are based on a regular daily concertation procedure between');
MM('central banks across Europe and worldwide,');
MM('which normally takes place at 2.15 p.m. CET.');
MM('');
i:=0;
repeat
  Sleep(1000); inc(i);
  cbXML.Checked:= FileExists(Target); //Target : String; global
  Application.ProcessMessages;
until FileExists(Target) or (i>10); // 10 sec
if i>10 then ShowMessage('XML-File not exists!');
if FileExists(Target) then
begin
    Memo1.Lines.LoadFromFile(Target);
    ReadXMLFile2(Target);
end;
end;

end.