Autor Beitrag
Gerhard_S
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 98



BeitragVerfasst: Fr 10.08.12 05:57 
Hallo,
mit
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
Doc.LoadFromFile('settings.xml');
    Doc.Active := true;
    nodelist := Doc.DocumentElement.ChildNodes;
    for i := 0 to nodelist.count-1 do
     begin
       Memo1.Lines.Add(nodelist[i].NodeName);
     end;

kann ich bequem die Namen aller Knoten in einem TXMLDocument auslesen, ohne sie zu kennen. Jetzt brauche ich aber noch die zugehörigen Werte. Wie geht das?
(Bei Google habe ich nichts gefunden.)
Das Dokument wird so erzeugt:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
Doc:= TXMLDocument.Create(Self);
with Doc do
   begin
      Active := True ;
      Version := '1.0';
      StandAlone := 'yes';
      Encoding := 'UTF-8';
       with AddChild('ConnData'do
         begin
           with
             AddChild('HostName'do
               Attributes['value']:= 'mysite.net';
           with AddChild('Password'do
               Attributes['value']:= '1234567890';
         end;
     Doc.SaveToFile('settings.xml');
     Active := false;
   end;


Moderiert von user profile iconNarses: Code- durch Delphi-Tags ersetzt
Moderiert von user profile iconNarses: Topic aus VCL (Visual Component Library) verschoben am Fr 10.08.2012 um 08:59
bummi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1248
Erhaltene Danke: 187

XP - Server 2008R2
D2 - Delphi XE
BeitragVerfasst: Fr 10.08.12 08:30 
Vielleicht kannst Du mit folgendem Fetzen etwas anfangen
ausblenden volle Höhe 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:
Function GetNodeText(n:IXMLNode;forceText:Boolean=true):String;
begin
  if ForceText then
    begin
        try
           Result := n.Text
        except

        end;
    end
   else
      begin
        if n.IsTextElement then Result := n.Text
        else Result :='';
      end;


end;

Procedure   TForm5.ParseNodes(n:IXMLNode;tn:TTreeNode);
var
 i,j:Integer;
 an:TTreeNode;
 att:String;
begin
  For i := 0 to  n.ChildNodes.Count - 1 do
    begin
    att :='';
    for j := 0 to  n.ChildNodes[i].AttributeNodes.Count - 1 do
        att := Att + n.ChildNodes[i].AttributeNodes[j].LocalName + '='+ n.ChildNodes[i].Attributes[n.ChildNodes[i].AttributeNodes[j].LocalName] +', ';
        if Length(n.ChildNodes[i].LocalName)>0 then
            begin
            an := TreeView1.Items.AddChild(tn,n.ChildNodes[i].Prefix+':' +n.ChildNodes[i].LocalName+' >>> '+ GetNodeText(n.ChildNodes[i]) + ' - '  + att);
            ParseNodes(n.ChildNodes[i],an);
            end;
    end;
end;

procedure TForm5.Button2Click(Sender: TObject);
var
 i:Integer;
begin
  x.XML.LoadFromFile('C:\temp\seite.xml');
  x.Active := true;
  ParseNodes(x.DocumentElement,nil);
end;

_________________
Das Problem liegt üblicherweise zwischen den Ohren H₂♂
DRY DRY KISS
Gerhard_S Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 98



BeitragVerfasst: Fr 10.08.12 13:47 
Hallo bommi,
kannst du noch mal gucken:
Zeile 33 ergibt E2010: Inkompatible Typen 'string' und 'TTreeNode'
ausblenden Delphi-Quelltext
1:
an := TreeView1.Items.AddChild(tn,n.ChildNodes[i].Prefix+':' +n.ChildNodes[i].LocalName+' >>> '+ GetNodeText(n.ChildNodes[i]) + ' - '  + att);					
bummi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1248
Erhaltene Danke: 187

XP - Server 2008R2
D2 - Delphi XE
BeitragVerfasst: Fr 10.08.12 20:14 
kann ich nicht nachvollziehen :gruebel:

_________________
Das Problem liegt üblicherweise zwischen den Ohren H₂♂
DRY DRY KISS
Gerhard_S Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 98



BeitragVerfasst: Fr 10.08.12 23:39 
Hier die Fassung von ParseNode für Normalsterbliche, die nur eine Stringliste oder ein Memo füllen wollen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
procedure TForm1.ParseNodes2(n:IXMLNode);
var
 i,j:Integer;
 att1, astr1: string;
begin
  for i := 0 to n.ChildNodes.Count - 1 do
    begin
    att1 :='';
    for j := 0 to n.ChildNodes[i].AttributeNodes.Count - 1 do
        begin
        att1 := n.ChildNodes[i].Attributes[n.ChildNodes[i].AttributeNodes[j].LocalName]; //Attribut
        end;
        if Length(n.ChildNodes[i].LocalName)>0 then
            begin
            astr1 := n.ChildNodes[i].LocalName+' : '+att1;  //Kind : Attribut
            Memo1.Lines.Add(astr1);
            ParseNodes2(n.ChildNodes[i]);
            end;
    end;
end;

Der Aufruf erfolgt z.B. so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
  Doc := TXMLDocument.Create(Self);
  if FileExists('settings.xml'then
  begin
    Doc.LoadFromFile('settings.xml');
    Doc.Active := true;
    ParseNodes2(Doc.DocumentElement);
  end;
borstel27
Hält's aus hier
Beiträge: 6



BeitragVerfasst: So 02.09.12 09:00 
Das ist noch verbesserungswürdig:
In Zeile 11 überscheibst du in der Schleife immer mit dem letzten Attribut ;-)
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6386
Erhaltene Danke: 146

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: So 02.09.12 10:05 
@user profile iconborstel27:
Das macht doch nichts, da ja in Zeile 15 damit ein anderer String gefüllt wird, der ins Memo kommt. Danach ist kann es ja ohne Probleme wieder überschrieben werden.
borstel27
Hält's aus hier
Beiträge: 6



BeitragVerfasst: So 02.09.12 11:30 
Stimmt nicht. Die Schleife geht erst alle Attribute durch und überschreibt jedesmal mit dem nachfolgenden, so daß nur das letzte ins Memo geschrieben wird.
Am besten einfach mal ausprobieren ;-)
Woran ich gerade verzweifle: Wie bekommt man es hin, nicht nur den Wert des Attributs (der kurioserweise mit Localname ermittelt wird) sondern auch seinen Namen auszuspucken?
Laut Doku gibt es dafür AttrName, aber ich bekomme das irgendwie nicht gebacken :-(

PS: ich beziehe mich auf den Code von Gerhard_S

PPS: Problem gelöst, habe gerade den Code von bummi genommen, damit ist alles bestens :-)
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6386
Erhaltene Danke: 146

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: So 02.09.12 17:54 
user profile iconborstel27 hat folgendes geschrieben Zum zitierten Posting springen:
Stimmt nicht. Die Schleife geht erst alle Attribute durch und überschreibt jedesmal mit dem nachfolgenden, so daß nur das letzte ins Memo geschrieben wird.

Ach Mist. Da hatte ich wohl meine Brille noch nicht geputzt. :wink: