![]() |
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:
Das Array aXmlTimeTexteDe ist vom Typ
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;
Delphi-Quellcode:
Die XML Datei sieht aus wie folgt:
type TXmlTimeTexte = record
sId : string; sDisplay : string; sStd : string; sDlt : string; iIndex : integer; sDisplayID : string; tDelayTimeStd : TTime; tDelayTimeDlt : TTime; bIncreaseTime : boolean; end;
Code:
Sobald ich auf den ersten Zugriff laufe (Node['@id'].Text) bekomme ich eine Exception "Ungültige Variantenoperation".
<?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> Was mache ich denn bitte falsch? :?: |
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-@ ? |
AW: Zugriff auf XML Attribute mit '@'
Guter Tipp.
Über:
Delphi-Quellcode:
klappt es.
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; Danke. |
AW: Zugriff auf XML Attribute mit '@'
Die eine Variable könntest du noch einsparen.
Delphi-Quellcode:
Aber geht es über den Namen nicht?
aXmlTimeTexteDe[ix+1].sId := iXmlAttributList[0].Text;
// oder besser aXmlTimeTexteDe[ix+1].sId := iXmlAttributList['id'].Text; 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