-2

如何在delphi中将xml文件中的数据打开/加载到tcxgrid中?

4

1 回答 1

1

首先,您没有提供任何 XML 数据,所以我使用了这个:

<?xml version="1.0"?>
<Tests>
  <Test TestId="0001" TestType="CMD">
    <Name>Convert number to string</Name>
    <CommandLine>Examp1.EXE</CommandLine>
    <Input>1</Input>
    <Output>One</Output>
  </Test>
  <Test TestId="0002" TestType="CMD">
    <Name>Find succeeding characters</Name>
    <CommandLine>Examp2.EXE</CommandLine>
    <Input>abc</Input>
    <Output>def</Output>
  </Test>
  <Test TestId="0003" TestType="GUI">
    <Name>Convert multiple numbers to strings</Name>
    <CommandLine>Examp2.EXE /Verbose</CommandLine>
    <Input>123</Input>
    <Output>One Two Three</Output>
  </Test>
  <Test TestId="0004" TestType="GUI">
    <Name>Find correlated key</Name>
    <CommandLine>Examp3.EXE</CommandLine>
    <Input>a1</Input>
    <Output>b1</Output>
  </Test>
  <Test TestId="0005" TestType="GUI">
    <Name>Count characters</Name>
    <CommandLine>FinalExamp.EXE</CommandLine>
    <Input>This is a test</Input>
    <Output>14</Output>
  </Test>
  <Test TestId="0006" TestType="GUI">
    <Name>Another Test</Name>
    <CommandLine>Examp2.EXE</CommandLine>
    <Input>Test Input</Input>
    <Output>10</Output>
  </Test>
</Tests>

Tha 数据与名为 test.xml 的 exe 文件一起保存

我使用一个属性来获取名称:

property XMLFileName: String read GetXMLFileName;

和实施:

function TForm8.GetXMLFileName: String;
begin
  Result := ExtractFilePath(Application.ExeName) + 'test.xml';
end;

然后将 XML 文件转换为数据集:

procedure TForm8.DomToDataset(XMLNode: IXMLNode; Dataset: TDataset);
var
  AttrNode: IXMLNode;

  procedure CreateFields;
  var
    StringField: TStringField;
    i: Integer;
    ChildNode: IXMLNode;
  begin
    for i := 0 to AttrNode.ChildNodes.Count - 1 do
    begin
      ChildNode := AttrNode.ChildNodes.Get(i);
      StringField := TStringField.Create(Dataset);
      StringField.Size := 50;
      StringField.FieldName := ChildNode.NodeName;
      StringField.DisplayLabel := ChildNode.NodeName;
      StringField.Dataset := Dataset;
      StringField.Name := 'StringField' + IntToStr(Dataset.Fields.Count + 1);
    end;
  end;

var
  i, J: Integer;
begin
  Dataset.Close;
  Dataset.Fields.Clear;

  for i := 0 to XMLNode.ChildNodes.Count - 1 do
  begin
    AttrNode := XMLNode.ChildNodes.Get(i);

    if Dataset.Fields.Count = 0 then
      CreateFields;

    Dataset.Open;

    Dataset.Append;
    try
      for J := 0 to AttrNode.ChildNodes.Count - 1 do
        Dataset.Fields[J].Value := AttrNode.ChildNodes.Get(J).NodeValue;

      Dataset.Post;
    except
      Dataset.Cancel;
    end;
  end;
end;

它远非完美,但它为您提供了图片。最后,我们需要将它们绑定在一起:

procedure TForm8.FillGrid;
var
  XMLDocument: TXMLDocument;
  i: Integer;
begin
  XMLDocument := TXMLDocument.Create(nil);
  XMLDocument.LoadFromFile(XMLFileName);
  XMLDocument.Active := True;

  DomToDataset(XMLDocument.DocumentElement, dxMemData1);
  FreeAndNil(XMLDocument);
  dxMemData1.Active := True;

  cxGrid1DBTableView1.DataController.CreateAllItems;
  for i := 0 to cxGrid1DBTableView1.ColumnCount - 1 do
    cxGrid1DBTableView1.Columns[i].ApplyBestFit();
end;

这是一个好的开始。因为我没有你的消息,所以我不能再靠近了。

于 2014-10-02T12:55:58.453 回答