Autor Beitrag
GerhardS
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: Di 27.07.10 14:04 
Hallo,
ich möchte eine einfache XML-Datei per Code auslesen, um sie wie eine Ini-Datei zu gebrauchen. Ich habe schon stundenlang gegoogelt und bin entweder auf neue XML-Komponenten oder neue Parser gestoßen. Dabei scheint die Aufgabe einfach sein. Ich habe folgendes XML-Document (Auszug):
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
<Software version="7" date="27.07.2010 00:05:19">
 <Form>
  <Breite Pixel="651"/>
  <Höhe Pixel="375"/>
 </Form>
</Software>

und diese Werte möchte ich beim Erzeugen der Form auslesen, um der Form die vorgegebene Höhe und Breite zu geben. Wie erhalte ich, um beim Beispiel zu bleiben, Breite=651 und Höhe=375 ?
LSanchez
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 26
Erhaltene Danke: 8



BeitragVerfasst: Di 27.07.10 14:54 
Verwende die Komponente "XMLDocument" von Delphi auf der Palettenseite "Internet"
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 27.07.10 19:29 
Als Beispiel kannst du dir zum Beispiel meine Config Utils anschauen, die stellen eine Konfiguration für Delphi Projekte zur Verfügung. Unter anderem auch als XML-Datei:
www.delphi-forum.de/viewtopic.php?t=92348

Zum Zugriff auf eine XML-Datei gibt es einmal von Delphi selbst Units (XMLDoc, XMLIntf) und andererseits kannst du auch einfach die XML-Typbibliothek von Windows importieren und nutzen. Ganz wie du möchtest.
GerhardS Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: Di 27.07.10 23:05 
user profile iconLSanchez hat folgendes geschrieben Zum zitierten Posting springen:
Verwende die Komponente "XMLDocument" von Delphi auf der Palettenseite "Internet"

Genau das will ich ja. Deshalb die Überschrift "Anfängerfrage zu TXMLDocument".
Das Erzeugen einer XML-Datei mit XMLDocument bekomme ich schon hin:
ausblenden 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:
with XMLDocument1 do
  try
    Active := True ;
    with AddChild('Software'do
    begin
      Attributes['version'] := 7;
      Attributes['date'] := DateTimeToStr(Now);

      with AddChild('Form'do
      begin
        with AddChild('Breite'do
        begin
          Attributes['Pixel'] := AWidth;
        end;
        with AddChild('Höhe'do
        begin
          Attributes['Pixel'] := AHeight;
        end;
      end;
    end;
   XMLDocument1.SaveToFile('mydocfile.xml');
  finally
    Active := False;
  end;

Ich hatte gehofft, dass das Auslesen genauso einfach wäre... Anders gesagt - haste mal 'n paar Zeile Code für mich? Die Delphi-Hilfe verzichtet an dieser Stelle auf ein Beispiel.

Moderiert von user profile iconNarses: Code- durch Delphi-Tags ersetzt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 28.07.10 06:24 
Nen kurzer Blick in den verlinkten Quelltext, statt FindNode gehts auch so wie hier:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
var
  i: Integer;
  XmlDoc: IXMLDocument;
  FormDataNode: IXMLNode;
begin
  XmlDoc := NewXMLDocument();
  XmlDoc.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'mydocfile.xml');
  ShowMessage(XmlDoc.ChildNodes['Software'].Attributes['version']);
  FormDataNode := XmlDoc.ChildNodes['Software'].ChildNodes['Form'];
  for i := 0 to FormDataNode.ChildNodes.Count - 1 do
    ShowMessage(FormDataNode.ChildNodes[i].XML);
Ob der Umlaut geht, weiß ich nicht.
GerhardS Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: Mi 28.07.10 13:29 
Ich hab's jetzt soweit:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
procedure TForm1.FormCreate(Sender: TObject);
var Doc: TXMLDocument;
FormDataNode: IXMLNode;
i: integer;
begin
Doc := TXMLDocument.Create(Self);
Doc.LoadFromFile('mydocfile.xml');
Doc.Active := true;  //kann beim Auslesen wegfallen
FormDataNode := Doc.ChildNodes['Software'].ChildNodes['Form'];
Form1.Width := FormDataNode.ChildNodes['Breite'].Attributes['Pixel'];
Form1.Height := FormDataNode.ChildNodes['Höhe'].Attributes['Pixel'];
//Wenn ich aber schreibe:
for i := 0 to FormDataNode.ChildNodes.Count - 1 do
   begin
   ShowMessage('Breite: '+FormDataNode.ChildNodes['Breite'].Attributes['Pixel']); 
   ShowMessage('Höhe: '+FormDataNode.ChildNodes['Höhe'].Attributes['Pixel']);
   end;
//dann werden Breite und Höhe je 2 mal angezeigt. Lässt sich das vermeiden?


end;


Moderiert von user profile iconNarses: Code- durch Delphi-Tags ersetzt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 28.07.10 19:44 
Ist das dein Ernst? :shock:

Wieviele Unterknoten hat denn FormDataNode, sprich <Form>? Genau, 2. Also wie oft wird wohl die Schleife durchlaufen? Genau, zweimal.

Das war doch nur ein Beispiel wie du eine unbekannte Anzahl Kindknoten durchlaufen kannst. Hier am Beispiel deiner XML-Datei brauchst du es natürlich nicht...