![]() |
AW: Brauche Hilfe bei XML-Datei
Hallo zusammen,
ich hänge mich mal an dieses Thema mit dran, da ich auch gerade ein Problem damit habe, XML-Dateien auszulesen. Habe bisher nur mit INI-Files gearbeitet, wollte jetzt aber mal ein Projekt mit XML-Dateien aufbauen. Und zwar geht es um folgende XML-Datei:
XML-Code:
Meine Frage ist jetzt, wie ich an die Einzelnen Knoten dran komme. Ich brauche alle Informationen wie z.B. name, orbs, affinity usw. Diese sollen in einem Object gespeichert werden. Folgenden Code habe ich dazu geschrieben (angelehnt an den Code von DeddyH):
<?xml version="1.0"?>
<CardInformation> <name></name> <orbs count="x"> <fire>x</fire> <frost>x</frost> <nature>x</nature> <shadow>x</shadow> </orbs> <affinity></affinity> <energy></energy> <type kind=""></type> <charges></charges> <rarity></rarity> <edition></edition> <weapontype></weapontype> <size></size> <attack></attack> <defense></defense> <special> <special1></special1> </special> <picture></picture> </CardInformation>
Delphi-Quellcode:
Allerdings steigt er schon in der Zeile
procedure TCardReader.CreateBFCardInfo(const AXmlList: TStringList; const ABFCardInfoList: TBFCardInfoList);
var i, j: Integer; ci: TBFCardInfo; doc: IXmlDocument; Node: IXMLNode; begin CoInitialize(nil); try doc := TXMLDocument.Create(nil); for i := 0 to AXmlList.Count - 1 do begin ci := TBFCardInfo.Create; doc.LoadFromFile(AXmlList[i]); // Load XmlDocument doc.Active := True; // Activate Document and check it if doc.DocumentElement.HasChildNodes then begin for j := 0 to doc.DocumentElement.ChildNodes.Count - 1 do begin end; Node := doc.DocumentElement.ChildNodes.FindNode('CardInformation'); if Assigned(Node) then begin ci.Name := Node.ChildNodes.FindNode('name').NodeValue; ci.Orbs := Node.ChildNodes.FindNode('orbs').NodeValue; ABFCardInfoList.Add(ci); end; end; end; finally CoUninitialize; end; end;
Delphi-Quellcode:
aus und überspringt den nachfolgenden Part. Ich schaffe es also nicht, überhaupt an die untergeordneten Knoten von
if doc.DocumentElement.HasChildNodes then begin
Delphi-Quellcode:
ranzukommen. Wo liegt mein Fehler? Ich hoffe mir kann da jemand bei helfen. Vielleicht kann auch jemand noch ein Beispiel machen, wie ich an die Subknoten von
CardInformation
Delphi-Quellcode:
herankomme. Ich denke zwar, dass das dann das gleiche Verfahren sein wird, aber bevor ich hier nochmal gegen die Wand renne ... :roll:
orbs
|
AW: Brauche Hilfe bei XML-Datei
ggf. könnte Dir ein Blick in
![]() Gruß K-H |
AW: Brauche Hilfe bei XML-Datei
Kann es denn mehrere CardInformations innerhalb einer XML-Datei geben? Wenn ja, stimmt AFAIK die Struktur nicht, es müsste dann noch ein umschließendes Tag geben, aber das kann man im RFC bestimmt genauer nachlesen.
|
AW: Brauche Hilfe bei XML-Datei
Zitat:
nein es bleibt bei einer CardDefinition. Habe natürlich auch in der Zeit ein bisschen weiter probiert und bin mit ein paar anderen Thread hier und bei SO schon ein kleines Stück weiter gekommen. Folgender Code funktioniert bei mir zumindest teilweise:
Delphi-Quellcode:
procedure TCardReader.CreateBFCardInfo(const AXmlList: TStringList; const ABFCardInfoList: TBFCardInfoList);
var i, j: Integer; ci: TBFCardInfo; doc: IXmlDocument; BaseNode, CurrentNode: IXMLNode; begin CoInitialize(nil); try doc := TXMLDocument.Create(nil); for i := 0 to AXmlList.Count - 1 do begin ci := TBFCardInfo.Create; doc.LoadFromFile(AXmlList[i]); // Load XmlDocument for specific card doc.Active := True; // Activate Document and check it BaseNode := doc.DocumentElement; if Assigned(BaseNode) then begin CurrentNode := BaseNode.ChildNodes['name']; if Assigned(CurrentNode) then begin if CurrentNode.HasAttribute('name') then ci.Name := CurrentNode.Attributes['name'] else ci.Name := '"Error while getting Name"'; end; CurrentNode := BaseNode.ChildNodes['orbs']; ci.Orbs := GetOrbs(ExtractFileName(AXmlList[i]), CurrentNode); ABFCardInfoList.Add(ci); end; end; finally CoUninitialize; end; end;
Delphi-Quellcode:
Falls an meiner XML-Struktur noch etwas falsch sein sollte, dann kann ich das gerne korrigieren. Das soll auf keinen Fall das Problem sein. :)
function TCardReader.GetOrbs(const CurrentFile: string; const XmlNode: IXMLNode): TOrbs;
var OrbCount, Sum: Integer; begin if Assigned(XmlNode) then begin if (XmlNode.HasAttribute('count')) then if TryStrToInt(XmlNode.Attributes['count'], OrbCount) then begin if (XmlNode.HasAttribute('fire')) then Result.Fire := StrToIntDef(XmlNode.Attributes['fire'], 0); if (XmlNode.HasAttribute('frost')) then Result.Frost := StrToIntDef(XmlNode.Attributes['frost'], 0); if (XmlNode.HasAttribute('nature')) then Result.Nature := StrToIntDef(XmlNode.Attributes['nature'], 0); if (XmlNode.HasAttribute('shadow')) then Result.Shadow := StrToIntDef(XmlNode.Attributes['shadow'], 0); // Build sum of all orbs an check against count attribute // If the sum is not the same as the count attribute raise an error which will be logged // and visible to the user to correct the error Sum := Result.Fire + Result.Frost + Result.Nature + Result.Shadow; // (*****************************) //if (OrbCount <> Sum) then // Log Error (*****************************) end; end; end; Ich versuche jetzt gerade, ob ich über diese Methode auch an die anderen Knoten dran komme. Ich werde dann auch gleich Rückmeldung geben sobald ich ein Ergebnis habe. |
AW: Brauche Hilfe bei XML-Datei
Also ich habe es jetzt dann doch so hinbekommen. Siehe den Code oben. Um an die direkten Unterknoten von CardInformation zu kommen, habe ich dann mit
Delphi-Quellcode:
gearbeitet. Das funktioniert soweit problemlos.
BaseNode.ChildNodes['energy'].NodeValue
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 01:00 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