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



BeitragVerfasst: Do 05.01.12 07:20 
Hallo,
ich muss XML-Dateien auslesen, die so aufgebaut sind:
ausblenden XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
<?xml version="1.0" encoding="UTF-8"?>
<session xmlns="http://winscp.net/schema/session/1.0" name="sessionname" start="2012-01-04T22:09:53.504Z">
  <ls>
    <destination value="/httpdocs/content/test" />
    <files>
      <file>
        <filename value="meinOrdner" />
        <type value="D" />
        <modification value="2011-08-31T22:59:45.000Z" />
        <permissions value="rwxr-x---" />
      </file>
      ...
    </files>
   <result success="true" />
  </ls>
</session>
Meine Frage: wie hangle ich mich zum ersten file-Eintrag durch? Ich benutze das bei Delphi mitgelieferte TXMLDocument.
Mit der Kette
ausblenden Delphi-Quelltext
1:
2:
ARoot := Doc.DocumentElement;
AFileName := ARoot.ChildNodes['session'].ChildNodes['ls'].ChildNodes['destination'].ChildNodes['files'].ChildNodes[1].attributes['value'];
geht's jedenfalls nicht.

Moderiert von user profile iconNarses: XML-Tags hinzugefügt
Moderiert von user profile iconNarses: Delphi-Tags hinzugefügt
Andreas L.
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1703
Erhaltene Danke: 25

Windows Vista / Windows 10
Delphi 2009 Pro (JVCL, DragDrop, rmKlever, ICS, EmbeddedWB, DEC, Indy)
BeitragVerfasst: Do 05.01.12 08:14 
Fehlt da in deinem Code nicht ein Kindknoten? Vielleicht klappt das:

ausblenden Delphi-Quelltext
1:
AFileName := ARoot.ChildNodes['session'].ChildNodes['ls'].ChildNodes['destination'].ChildNodes['files'].ChildNodes[1].ChildNotes['filename'].attributes['value'];					
Gerhard_S Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 98



BeitragVerfasst: Do 05.01.12 11:07 
user profile iconAndreas L. hat folgendes geschrieben Zum zitierten Posting springen:
Fehlt da in deinem Code nicht ein Kindknoten? Vielleicht klappt das:

ausblenden Delphi-Quelltext
1:
AFileName := ARoot.ChildNodes['session'].ChildNodes['ls'].ChildNodes['destination'].ChildNodes['files'].ChildNodes[1].ChildNotes['filename'].attributes['value'];					


Leider nicht: "Listenindex überschreitet das Maximum (1)" meldet Delphi. Ich vermute, dass es am Aufbau der XML-Datei liegt. Eine Abfrage auf ChildNodes.Count hat ergeben, dass nur ARoot einen Kindknoten hat, 'session', 'ls', 'destination' und 'files' melden 0 ChildNodes, obwohl 'files' mindestens einen Kindknoten hat.
Lemmy
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 792
Erhaltene Danke: 49

Windows 7 / 10; CentOS 7; LinuxMint
Delphi 7-XE10.1, VS 2015
BeitragVerfasst: Do 05.01.12 11:43 
user profile iconGerhard_S hat folgendes geschrieben Zum zitierten Posting springen:

ausblenden Quelltext
1:
2:
ARoot := Doc.DocumentElement;
AFileName := ARoot.ChildNodes['session'].ChildNodes['ls'].ChildNodes['destination'].ChildNodes['files'].ChildNodes[1].attributes['value'];

geht's jedenfalls nicht.


Weil Files kein Kind von Destination ist - Destination dient lediglich dafür Attribute zu speichern - das wird sofort wieder geschlossen. Besorg dir nen anständigen XML-Viewer, wenns nichts kosten soll, dann das XML-Notepad von Microsoft.

ausblenden Quelltext
1:
2:
ARoot := Doc.DocumentElement;
AFileName := ARoot.ChildNodes['session'].ChildNodes['ls'].ChildNodes['files'].ChildNodes['file'].ChildNodes['filename'].attributes['value'];


Und wegen dem Attribut - da musst Du dir auch den Node holen in dem das Attribut steckt und nicht den Elternknoten... So sollte das eigentlich passen...
Gerhard_S Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 98



BeitragVerfasst: Do 05.01.12 12:02 
user profile iconLemmy hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden Delphi-Quelltext
1:
2:
ARoot := Doc.DocumentElement;
AFileName := ARoot.ChildNodes['session'].ChildNodes['ls'].ChildNodes['files'].ChildNodes['file'].ChildNodes['filename'].attributes['value'];

Und wegen dem Attribut - da musst Du dir auch den Node holen in dem das Attribut steckt und nicht den Elternknoten... So sollte das eigentlich passen...

Hm, Delphi antwortet, wie es sich für ein Orakel gehört:
"Variante des Typs (Null) konnte nicht in Typ (OleStr) konvertiert werden."

Moderiert von user profile iconNarses: Zitat repariert.
Lemmy
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 792
Erhaltene Danke: 49

Windows 7 / 10; CentOS 7; LinuxMint
Delphi 7-XE10.1, VS 2015
BeitragVerfasst: Do 05.01.12 12:11 
argh... lesen sollte man können:

ausblenden Quelltext
1:
AFileName := ARoot.ChildNodes['ls'].ChildNodes['files'].ChildNodes['file'].ChildNodes['filename'].attributes['value'];					


Session ist ja der DocumentRoot. Deshalb darf der da nicht mehr auftauchen.
baka0815
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 489
Erhaltene Danke: 14

Win 10, Win 8, Debian GNU/Linux
Delphi 10.1 Berlin, Java, C#
BeitragVerfasst: Do 05.01.12 12:13 
Hier mal das ganze als XML und eingerückt:
ausblenden XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
<?xml version="1.0" encoding="UTF-8"?>
<session xmlns="http://winscp.net/schema/session/1.0" name="sessionname" start="2012-01-04T22:09:53.504Z">
  <ls>
    <destination value="/httpdocs/content/test" />
    <files>
      <file>
        <filename value="meinOrdner" />
        <type value="D" />
        <modification value="2011-08-31T22:59:45.000Z" />
        <permissions value="rwxr-x---" />
      </file>
...
    </files>
    <result success="true" />
  </ls>
</session>
Gerhard_S Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 98



BeitragVerfasst: Do 05.01.12 12:30 
user profile iconLemmy hat folgendes geschrieben Zum zitierten Posting springen:
argh... lesen sollte man können:

ausblenden Quelltext
1:
AFileName := ARoot.ChildNodes['ls'].ChildNodes['files'].ChildNodes['file'].ChildNodes['filename'].attributes['value'];					


Session ist ja der DocumentRoot. Deshalb darf der da nicht mehr auftauchen.


Danke. So geht's.