Einzelnen Beitrag anzeigen

bernhard_LA

Registriert seit: 8. Jun 2009
Ort: Bayern
1.123 Beiträge
 
Delphi 11 Alexandria
 
#7

AW: graphml format - Erzeugen von Graphen-files mit Delphi

  Alt 28. Feb 2020, 13:37
ohne Verwendung des XML Databinding sieht meine aktuelle Lösung wie folgt aus:



Delphi-Quellcode:

var
  i: Integer;
  ColorstringList: TStringList;
  LDocument: IXMLDocument;
  LNodeElement, LGRAPHelement , ChildnodeElement, LSubNodeElement, NodeText,
    NodeCData: IXMLNode;
begin

  ///
  ///
  ///
  ColorstringList := TStringList.Create;
  ColorstringList.Add('green');
  ColorstringList.Add('blue');
  ColorstringList.Add('red');
  ColorstringList.Add('white');
  ColorstringList.Add('purple');
  ColorstringList.Add('green');
  ColorstringList.Add('blue');
  ColorstringList.Add('red');
  ColorstringList.Add('white');
  ColorstringList.Add('purple');
  ColorstringList.Add('green');
  ColorstringList.Add('blue');
  ColorstringList.Add('red');
  ColorstringList.Add('white');
  ColorstringList.Add('purple');

  LDocument := TXMLDocument.Create(nil);
  LDocument.Active := True;

  { Define document content. }
  LDocument.DocumentElement := LDocument.CreateNode('graphml', ntElement, '');
  LDocument.DocumentElement.Attributes['xmlns'] :=
    'http://graphml.graphdrawing.org/xmlns';
  LDocument.DocumentElement.Attributes['xmlns:xsi'] :=
    'http://www.w3.org/2001/XMLSchema-instance';

  ///
  /// <key id="d0" for="node" attr.name="color" attr.type="string"> <default>yellow</default>
  /// </key> <key id="d1" for="edge" attr.name="weight" attr.type="double"/>
  ///

  LNodeElement := LDocument.DocumentElement.AddChild('key', -1);
  LNodeElement.Attributes['id'] := 'key' + IntToStr(1);
  LNodeElement.Attributes['for'] := 'node';
  LNodeElement.Attributes['attr.name'] := 'color';
  LNodeElement.Attributes['attr.type'] := 'string';

  LNodeElement := LDocument.DocumentElement.AddChild('key', -1);
  LNodeElement.Attributes['id'] := 'key' + IntToStr(2);
  LNodeElement.Attributes['for'] := 'edge';
  LNodeElement.Attributes['attr.name'] := 'weight';
  LNodeElement.Attributes['attr.type'] := 'double';

  LGRAPHelement := LDocument.DocumentElement.AddChild('graph', -1);
  LGRAPHelement.Attributes['id'] := 'GRAPH';
  LGRAPHelement.Attributes['edgedefault'] := 'undirected';



  for i := 1 to 10 do
  begin
    LSubNodeElement := LGRAPHelement.AddChild('node', -1);
    LSubNodeElement.Attributes['id'] := 'N' + IntToStr(i);

    /// <data key="d0">green</data>
    ChildnodeElement := LSubNodeElement.AddChild('data');
    ChildnodeElement.Attributes['key']:='key1';
    ChildnodeElement.Text := ColorstringList[i];

  end;

  for i := 1 to 9 do
  begin
    LSubNodeElement := LGRAPHelement.AddChild('edge', -1);
    LSubNodeElement.Attributes['id'] := 'E' + IntToStr(i);
    LSubNodeElement.Attributes['source'] := 'N' + IntToStr(i);
    LSubNodeElement.Attributes['target'] := 'N' + IntToStr(i + 1);

    // <data key="d0">green</data>
    ChildnodeElement := LSubNodeElement.AddChild('data');
    ChildnodeElement.Attributes['key']:='key2';
    ChildnodeElement.Text := IntToStr(Random(5)+1);

  end;

  LSubNodeElement := LGRAPHelement.AddChild('edge', -1);
  LSubNodeElement.Attributes['id'] := 'E' + IntToStr(10);
  LSubNodeElement.Attributes['source'] := 'N' + IntToStr(10);
  LSubNodeElement.Attributes['target'] := 'N' + IntToStr(1);
  // <data key="d0">green</data>
  ChildnodeElement := LSubNodeElement.AddChild('data');
  ChildnodeElement.Attributes['key']:='key2';
  ChildnodeElement.Text := '1';



  LDocument.SaveToFile('e:\testxmlgraph.graphml');

  LDocument.SaveToFile('e:\testxmlgraph1.txt');

  LDocument.SaveToFile('e:\testxmlgraph2.xml');

  redt1.Lines.LoadFromFile('e:\testxmlgraph1.txt');

end;

Geändert von bernhard_LA (28. Feb 2020 um 15:28 Uhr)
  Mit Zitat antworten Zitat