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

时间:2014-09-25 15:28:00

标签: xml delphi tcxgrid

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

1 个答案:

答案 0 :(得分: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数据与exefile一起保存在名称test.xml

我使用属性来获取名称:

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;

这是一个好的开始。由于我没有您的信息,我无法接近。