Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   XML (https://www.delphipraxis.net/46-xml/)
-   -   Delphi Zugriff auf XML Attribute mit '@' (https://www.delphipraxis.net/152237-zugriff-auf-xml-attribute-mit-%40.html)

little_budda 16. Jun 2010 08:17

Zugriff auf XML Attribute mit '@'
 
Hallo,

ich möchte gerne Atttibute aus einem Knoten in ein dynamisches Array einlesen.
Hier im Forum habe ich gefunden das man auf Attribute mit einem führenden'@' als Subknoten des Primärelements zugreifen kann.
Will aber leider nicht klappen.

Ich versuche es aktuell so:
Delphi-Quellcode:
  iXML.LoadFromFile(sAppStartPath + sTextDirPart + '\' + sVersion + '\' + sZsTimeFileDe);
  iXML.Active:=true;
  RootNode := iXML.ChildNodes['timezones'];
  iXmlMldList := RootNode.ChildNodes;
  iMldCount := iXmlMldList.Count;
  setlength(aXmlTimeTexteDe, iMldCount + 1);
  aXmlTimeTexteDe[0].iIndex := iMldCount;
  for ix := 0 to (iMldCount - 1) do
    begin
      Node:= iXmlMldList.Nodes[ix];

      aXmlTimeTexteDe[ix+1].sId          := Node['@id'].Text;
      aXmlTimeTexteDe[ix+1].sDisplay     := Node['@Display'].Text;
      aXmlTimeTexteDe[ix+1].sStd         := Node['@Std'].Text;
      aXmlTimeTexteDe[ix+1].sDlt         := Node['@Dlt'].Text;
      aXmlTimeTexteDe[ix+1].iIndex       := strtointdef(Node['@Index'].Text, -1);
      aXmlTimeTexteDe[ix+1].sDisplayID   := Node['@DisplayID'].Text;
    end;
Das Array aXmlTimeTexteDe ist vom Typ
Delphi-Quellcode:
type TXmlTimeTexte = record
  sId              : string;
  sDisplay         : string;
  sStd             : string;
  sDlt             : string;
  iIndex           : integer;
  sDisplayID       : string;
  tDelayTimeStd    : TTime;
  tDelayTimeDlt    : TTime;
  bIncreaseTime    : boolean;
end;
Die XML Datei sieht aus wie folgt:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<timezones>
  <timezone
    id="Afghanistan Normalzeit"
    Display="(GMT+04:30)
    Kabul" Std="Afghanistan Normalzeit"
    Dlt="Afghanistan Sommerzeit"
    Index="175"
    DisplayID="(GMT+04:30) Kabul" />

  ...

<timezones>
Sobald ich auf den ersten Zugriff laufe (Node['@id'].Text) bekomme ich eine Exception "Ungültige Variantenoperation".
Was mache ich denn bitte falsch?
:?:

himitsu 16. Jun 2010 08:33

AW: Zugriff auf XML Attribute mit '@'
 
Node[] greift auf Nodes zu und nicht auf Attribute.

Wie wäre es mit Attribute['...'] und ohne das XPath-@ ?

little_budda 16. Jun 2010 08:45

AW: Zugriff auf XML Attribute mit '@'
 
Guter Tipp.

Über:
Delphi-Quellcode:
            Node:= iXmlMldList.Nodes[ix];
            iXmlAttributList := Node.AttributeNodes;

            if (iXmlAttributList.Count > 0) then
              begin
                // ID
                AttribNode := iXmlAttributList[0];
                aXmlTimeTexteDe[ix+1].sId := AttribNode.Text;
                // Display
                AttribNode := iXmlAttributList[1];
                aXmlTimeTexteDe[ix+1].sDisplay := AttribNode.Text;
                // Std
                AttribNode := iXmlAttributList[2];
                aXmlTimeTexteDe[ix+1].sStd := AttribNode.Text;
                // Dlt
                AttribNode := iXmlAttributList[3];
                aXmlTimeTexteDe[ix+1].sDlt := AttribNode.Text;
                // Index
                AttribNode := iXmlAttributList[4];
                aXmlTimeTexteDe[ix+1].iIndex := strtointdef(AttribNode.Text, -1);
                // DisplayID
                AttribNode := iXmlAttributList[5];
                aXmlTimeTexteDe[ix+1].sDisplayID := AttribNode.Text;
              end;
klappt es.

Danke.

himitsu 16. Jun 2010 08:52

AW: Zugriff auf XML Attribute mit '@'
 
Die eine Variable könntest du noch einsparen.
Delphi-Quellcode:
aXmlTimeTexteDe[ix+1].sId := iXmlAttributList[0].Text;
// oder besser
aXmlTimeTexteDe[ix+1].sId := iXmlAttributList['id'].Text;
Aber geht es über den Namen nicht?
Wäre zumindestens besser, falls die Reihenfolge mal nicht genau stimmt.


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:16 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz