Einzelnen Beitrag anzeigen

Aviator

Registriert seit: 3. Jun 2010
1.611 Beiträge
 
Delphi 10.3 Rio
 
#14

AW: Brauche Hilfe bei XML-Datei

  Alt 19. Jun 2016, 12:08
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.
Hallo DeddyH,
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:
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;
Falls an meiner XML-Struktur noch etwas falsch sein sollte, dann kann ich das gerne korrigieren. Das soll auf keinen Fall das Problem sein.
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.
  Mit Zitat antworten Zitat