-2

我正在尝试将 xml 文件填充到 Delphi ListView1 中,然后通过在 Category 部分下的 xml 文件中定义的 [tag] “分组”所有 [items and subitems],

我试图让我的程序有一个“标签”系统,所以类似的软件将按一个通用标签分组,如果 listview 不可能,我愿意接受建议,然后我可以使用其他组件来喜欢 VirtualStringTree,easylistview ..等等,这个程序是供我个人使用的,我只是从学习的角度很好奇,因为我没有看到其他关于如何完成这个的例子。

所以对于下面列出的 xml,它应该看起来像

ListView1
TAG1 [Each tag name in the category, this would be a group]
- New Item 1 <- Category name
   -Copy of File <- subitem software name
- bin [This is another category but it has TAG1 so group it with TAG1 items]
TAG4 [Each tag name in the category, this would be a group]
- New Item 1 <-this is the same as TAG1, add it to the TAG4 group since its in tags]
  -Copy of File <- subitem software of category

这是我的 xml 文件的示例。

file.xml
<Category>
<Category name="New Item 1" Tags="TAG1 TAG4"/>
 <Software name="Copy of File" Tags="">
  <PathCache>data\cache\945.ico</PathCache>
  <PathExe>$Drive\Development\file.exe</PathExe>
 </Software>
<Category name="bin" Tags="TAG1">
 <Software name="Copy of File" Tags="">
  <PathCache>data\cache\947.ico</PathCache>
 <PathExe>$Drive\Development\file.exe</PathExe>
</Software>
<Software name="softwaretitle" Tags="">
    <PathCache>data\cache\946.ico</PathCache>
    <PathExe>$Drive\Development\test.exe</PathExe>
 </Software>
 </Category>
 </Category>

我将 tags="" 部分添加到 xml 文件中的 Category 部分,如果需要我可以重写它

 <Category name="New Item 1"/>
  <Tags>Tag1 Tag4</tag>
4

1 回答 1

0

首先你应该更好地组织你的 XML 文件,至少这样(我建议你使用 Notepad++ 来缩进你的 XML):

<Categories>
  <Category name="New Item 1" Tags="TAG1 TAG4">
    <Software name="Copy of File" Tags="">
      <PathCache>data\cache\945.ico</PathCache>
      <PathExe>$Drive\Development\file.exe</PathExe>
    </Software>
    <Software name="Copy of File" Tags="">
      <PathCache>data\cache\945.ico</PathCache>
      <PathExe>$Drive\Development\file.exe</PathExe>
    </Software>
  </Category>
  <Category name="bin" Tags="TAG1">
    <Software name="Copy of File" Tags="">
      <PathCache>data\cache\947.ico</PathCache>
      <PathExe>$Drive\Development\file.exe</PathExe>
    </Software>
    <Software name="softwaretitle" Tags="">
      <PathCache>data\cache\946.ico</PathCache>
      <PathExe>$Drive\Development\test.exe</PathExe>
    </Software>
  </Category>
</Categories>

获得规范化的 XML 后,您可以使用 XMLDataBinding (File/New/Other/XML/XMLDataBinding) 将其转换为 Delphi 类,选择您的 Categories.xml 文件。

通过将 XML 映射到 Delphi 类,您可以轻松地浏览 XML 数据。

示例(德尔福 XE):

type
  TForm1 = class(TForm)
    btnPopulate: TButton;
    lvCategories: TListView;
    procedure btnPopulateClick(Sender: TObject);
  private
    Categories: IXMLCategoriesType;
    procedure LoadCategories(const FileName: string);
    procedure PopulateCategoriesList;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.LoadCategories(const FileName: string);
var
  XMLDoc1: TXMLDocument;
begin
  if Assigned(Categories) then
    Exit;

  XMLDoc1 := TXMLDocument.Create(Self);
  XMLDoc1.FileName := FileName;
  XMLDoc1.Active := True;
  Categories := GetCategories(XMLDoc1);
end;

procedure TForm1.PopulateCategoriesList;
  i, j: Integer;
  Column: TListColumn;
  Group: TListGroup;
  Item: TListItem;
begin
  lvCategories.ViewStyle := vsReport;
  lvCategories.GroupView := True;
  Column := lvCategories.Columns.Add;
  Column.Caption := 'Software name';
  Column.AutoSize := True;

  Column := lvCategories.Columns.Add;
  Column.Caption := 'Exe path';
  Column.AutoSize := True;

  for i := 0 to Categories.Count - 1do
  begin
    Group := lvCategories.Groups.Add;
    Group.Header := Copy(Categories.Category[i].Tags, 1, Pos(' ', Categories.Category[i].Tags) - 1);
    for j := 0  to Categories.Category[i].Count - 1 do
    begin
      Item := lvCategories.Items.Add;
      Item.Caption := Categories.Category[i].Software[j].Name;
      Item.GroupID := Group.GroupID;
      Item.SubItems.Add(Categories.Category[i].Software[j].PathExe)
    end;
  end;
end;

procedure TForm1.btnPopulateClick(Sender: TObject);
begin
  LoadCategories(ExtractFilePath(Application.ExeName) + '..\..\Categories.xml');
  PopulateCategoriesList;
end;

希望这对你有帮助!

于 2012-05-11T23:00:27.777 回答