草庐IT

xml - 将列表框数据保存到 XML?

coder 2024-06-29 原文

我有 2 个列表框,第一个列表框存储每个项目对象属性的数据指针(由我编写的自定义类定义)。每当我从此列表框中选择一个项目时,我都会通过访问存储在第一个列表框中的一些数据来填充第二个列表框。

一切都很好,但现在我需要知道如何将列表框保存和恢复为 XML。

如果有人可以提供示例或帮助我编写代码来执行此操作,我将不胜感激。

下面是一些示例代码,展示了我如何创建和访问数据:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    ListBox2: TListBox;
    cmdAdd: TButton;
    txtValue1: TEdit;
    txtValue2: TEdit;
    procedure cmdAddClick(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  TMyData = class(TObject)
  private
    FValue1: String;
    FValue2: String;
  public
    constructor Create(Value1, Value2: String);

    property Value1: String read FValue1 write FValue1;
    property Value2: String read FValue2 write FValue2;
end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TMyData }

constructor TMyData.Create(Value1, Value2: String);
begin
  inherited Create;

  FValue1:= Value1;
  FValue2:= Value2;
end;

procedure TForm1.cmdAddClick(Sender: TObject);
var
  Obj: TMyData;
begin
  Obj:= TMyData.Create(txtValue1.Text, txtValue2.Text);
  Listbox1.AddItem(txtValue1.Text, Obj);
end;

procedure TForm1.ListBox1Click(Sender: TObject);
var
  Obj: TMyData;
begin
  ListBox2.Items.Clear;

  Obj:= ListBox1.Items.Objects[ListBox1.ItemIndex] as TMyData;
  ListBox2.Items.Add(Obj.Value2);
end;

end.

最佳答案

tl;dr: Use the XML Data Binding wizard to create interfaces for handling your specific XML file, ét voila.

最简单的实现方法是从 XML 文件开始。例如,按如下方式构建它:

<?xml version="1.0"?>
<masteritems>
    <masteritem>
        <name>Caption1</name>
        <value>123456</value>
        <childitems>
            <childitem>
                <name>Caption1.1</name>
                <value>23452</value>
            </childitem>
            <childitem>
                <name>Caption1.2</name>
                <value>65465</value>
            </childitem>
        </childitems>
    </masteritem>
    <masteritem>
        ...
    </masteritem>
</masteritems>

接下来,使用XML 数据绑定(bind)向导为这种类型的XML 文件创建一个接口(interface)单元,参见文件> 新建> 其他> 新建> XML 数据绑定(bind)。随心所欲地调整,但默认情况下只需单击“确定”传递每个向导页面就可以了。 (请注意,其他 Delphi 版本的默认设置可能与我的不同。)尽管我个人喜欢摆脱的一件事是每种接口(interface)类型的“类型”后缀。 (以及类类型名称,但这不是向导中的一个选项,因此您可以手动执行此操作。)

现在,加载和操作这个 XML 文件:

uses
  Classes, Controls, Forms, StdCtrls,
  Test, xmldom, XMLIntf, msxmldom, XMLDoc;

type
  TForm2 = class(TForm)
    ListBox1: TListBox;
    ListBox2: TListBox;
    XMLDocument1: TXMLDocument; { See tab 'Internet' on component palette }
    SaveButton: TButton;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure SaveButtonClick(Sender: TObject);
  private
    function CurrentMasterItem: IXMLMasterItem;
  end;

...

function TForm2.CurrentMasterItem: IXMLMasterItem;
var
  MasterItems: IXMLMasterItems;
  I: Integer;
begin
  MasterItems := GetMasterItems(XMLDocument1);
  for I := 0 to MasterItems.Count - 1 do
  begin
    Result := MasterItems.Masteritem[I];
    if Result.Name = ListBox1.Items[ListBox1.ItemIndex] then
      Break;
  end;
end;

procedure TForm2.FormCreate(Sender: TObject);
var
  MasterItems: IXMLMasterItems;
  I: Integer;
begin
  XMLDocument1.FileName := 'Test.xml';
  XMLDocument1.NodeIndentStr := '<tab>';
  MasterItems := GetMasterItems(XMLDocument1);
  for I := 0 to MasterItems.Count - 1 do
    ListBox1.Items.Add(MasterItems[I].Name);
  XMLDocument1.Active := False;
end;

procedure TForm2.ListBox1Click(Sender: TObject);
var
  ChildItems: IXMLChildItems;
  I: Integer;
begin
  if ListBox1.ItemIndex > -1 then
  begin
    ChildItems := CurrentMasterItem.Childitems;
    ListBox2.Clear;
    for I := 0 to ChildItems.Count - 1 do
      ListBox2.Items.AddObject(ChildItems[I].Name,
        TObject(ChildItems[I].Value));
    XMLDocument1.Active := False;
  end;
end;

procedure TForm2.SaveButtonClick(Sender: TObject);
var
  ChildItems: IXMLChildItems;
  ChildItem: IXMLChildItem;
  I: Integer;
begin
  if ListBox1.ItemIndex > -1 then
  begin
    ListBox2.Items.AddObject('New item', TObject(543223));
    ChildItems := CurrentMasterItem.Childitems;
    ChildItems.Clear;
    for I := 0 to ListBox2.Count - 1 do
    begin
      ChildItem := ChildItems.Add;
      ChildItem.Name := ListBox2.Items[I];
      ChildItem.Value := Integer(ListBox2.Items.Objects[I]);
    end;
    XMLDocument1.SaveToFile(XMLDocument1.FileName);
    XMLDocument1.Active := False;
  end;
end;

更新:这是向导在这里创建的单位:

{*********************************************************}
{                                                         }
{                    XML Data Binding                     }
{                                                         }
{         Generated on: 10-11-2011 23:25:30               }
{       Generated from: H:\Delphi\Test\XMLTest\Test.xml   }
{   Settings stored in: H:\Delphi\Test\XMLTest\Test.xdb   }
{                                                         }
{*********************************************************}

unit Test;

interface

uses xmldom, XMLDoc, XMLIntf;

type

{ Forward Decls }

  IXMLMasterItems = interface;
  IXMLMasterItem = interface;
  IXMLChildItems = interface;
  IXMLChildItem = interface;

{ IXMLMasterItems }

  IXMLMasterItems = interface(IXMLNodeCollection)
    ['{ACA35986-053A-40AE-92E8-2044BC5DADC6}']
    { Property Accessors }
    function Get_Masteritem(Index: Integer): IXMLMasterItem;
    { Methods & Properties }
    function Add: IXMLMasterItem;
    function Insert(const Index: Integer): IXMLMasterItem;
    property Masteritem[Index: Integer]: IXMLMasterItem
      read Get_Masteritem; default;
  end;

{ IXMLMasterItem }

  IXMLMasterItem = interface(IXMLNode)
    ['{E6481675-AE5B-4166-A977-CA29EC97B78D}']
    { Property Accessors }
    function Get_Name: WideString;
    function Get_Value: Integer;
    function Get_Childitems: IXMLChildItems;
    procedure Set_Name(Value: WideString);
    procedure Set_Value(Value: Integer);
    { Methods & Properties }
    property Name: WideString read Get_Name write Set_Name;
    property Value: Integer read Get_Value write Set_Value;
    property Childitems: IXMLChildItems read Get_Childitems;
  end;

{ IXMLChildItems }

  IXMLChildItems = interface(IXMLNodeCollection)
    ['{CD16D91C-30E5-45A1-AAA1-1C518C84EA5B}']
    { Property Accessors }
    function Get_Childitem(Index: Integer): IXMLChildItem;
    { Methods & Properties }
    function Add: IXMLChildItem;
    function Insert(const Index: Integer): IXMLChildItem;
    property Childitem[Index: Integer]: IXMLChildItem
      read Get_Childitem; default;
  end;

{ IXMLChildItem }

  IXMLChildItem = interface(IXMLNode)
    ['{F7399037-A33F-4E43-ADF9-000EAD71B418}']
    { Property Accessors }
    function Get_Name: WideString;
    function Get_Value: Integer;
    procedure Set_Name(Value: WideString);
    procedure Set_Value(Value: Integer);
    { Methods & Properties }
    property Name: WideString read Get_Name write Set_Name;
    property Value: Integer read Get_Value write Set_Value;
  end;

{ Forward Decls }

  TXMLMasteritemsType = class;
  TXMLMasteritemType = class;
  TXMLChilditemsType = class;
  TXMLChilditemType = class;

{ TXMLMasteritemsType }

  TXMLMasteritemsType = class(TXMLNodeCollection, IXMLMasterItems)
  protected
    { IXMLMasterItems }
    function Get_Masteritem(Index: Integer): IXMLMasterItem;
    function Add: IXMLMasterItem;
    function Insert(const Index: Integer): IXMLMasterItem;
  public
    procedure AfterConstruction; override;
  end;

{ TXMLMasteritemType }

  TXMLMasteritemType = class(TXMLNode, IXMLMasterItem)
  protected
    { IXMLMasterItem }
    function Get_Name: WideString;
    function Get_Value: Integer;
    function Get_Childitems: IXMLChildItems;
    procedure Set_Name(Value: WideString);
    procedure Set_Value(Value: Integer);
  public
    procedure AfterConstruction; override;
  end;

{ TXMLChilditemsType }

  TXMLChilditemsType = class(TXMLNodeCollection, IXMLChildItems)
  protected
    { IXMLChildItems }
    function Get_Childitem(Index: Integer): IXMLChildItem;
    function Add: IXMLChildItem;
    function Insert(const Index: Integer): IXMLChildItem;
  public
    procedure AfterConstruction; override;
  end;

{ TXMLChilditemType }

  TXMLChilditemType = class(TXMLNode, IXMLChildItem)
  protected
    { IXMLChildItem }
    function Get_Name: WideString;
    function Get_Value: Integer;
    procedure Set_Name(Value: WideString);
    procedure Set_Value(Value: Integer);
  end;

{ Global Functions }

function Getmasteritems(Doc: IXMLDocument): IXMLMasterItems;
function Loadmasteritems(const FileName: WideString): IXMLMasterItems;
function Newmasteritems: IXMLMasterItems;

const
  TargetNamespace = '';

implementation

{ Global Functions }

function Getmasteritems(Doc: IXMLDocument): IXMLMasterItems;
begin
  Result := Doc.GetDocBinding('masteritems',
    TXMLMasteritemsType, TargetNamespace) as IXMLMasterItems;
end;

function Loadmasteritems(const FileName: WideString): IXMLMasterItems;
begin
  Result := LoadXMLDocument(FileName).GetDocBinding('masteritems',
    TXMLMasteritemsType, TargetNamespace) as IXMLMasterItems;
end;

function Newmasteritems: IXMLMasterItems;
begin
  Result := NewXMLDocument.GetDocBinding('masteritems',
    TXMLMasteritemsType, TargetNamespace) as IXMLMasterItems;
end;

{ TXMLMasteritemsType }

procedure TXMLMasteritemsType.AfterConstruction;
begin
  RegisterChildNode('masteritem', TXMLMasteritemType);
  ItemTag := 'masteritem';
  ItemInterface := IXMLMasterItem;
  inherited;
end;

function TXMLMasteritemsType.Get_Masteritem(Index: Integer): IXMLMasterItem;
begin
  Result := List[Index] as IXMLMasterItem;
end;

function TXMLMasteritemsType.Add: IXMLMasterItem;
begin
  Result := AddItem(-1) as IXMLMasterItem;
end;

function TXMLMasteritemsType.Insert(const Index: Integer): IXMLMasterItem;
begin
  Result := AddItem(Index) as IXMLMasterItem;
end;

{ TXMLMasteritemType }

procedure TXMLMasteritemType.AfterConstruction;
begin
  RegisterChildNode('childitems', TXMLChilditemsType);
  inherited;
end;

function TXMLMasteritemType.Get_Name: WideString;
begin
  Result := ChildNodes['name'].Text;
end;

procedure TXMLMasteritemType.Set_Name(Value: WideString);
begin
  ChildNodes['name'].NodeValue := Value;
end;

function TXMLMasteritemType.Get_Value: Integer;
begin
  Result := ChildNodes['value'].NodeValue;
end;

procedure TXMLMasteritemType.Set_Value(Value: Integer);
begin
  ChildNodes['value'].NodeValue := Value;
end;

function TXMLMasteritemType.Get_Childitems: IXMLChildItems;
begin
  Result := ChildNodes['childitems'] as IXMLChildItems;
end;

{ TXMLChilditemsType }

procedure TXMLChilditemsType.AfterConstruction;
begin
  RegisterChildNode('childitem', TXMLChilditemType);
  ItemTag := 'childitem';
  ItemInterface := IXMLChildItem;
  inherited;
end;

function TXMLChilditemsType.Get_Childitem(Index: Integer): IXMLChildItem;
begin
  Result := List[Index] as IXMLChildItem;
end;

function TXMLChilditemsType.Add: IXMLChildItem;
begin
  Result := AddItem(-1) as IXMLChildItem;
end;

function TXMLChilditemsType.Insert(const Index: Integer): IXMLChildItem;
begin
  Result := AddItem(Index) as IXMLChildItem;
end;

{ TXMLChilditemType }

function TXMLChilditemType.Get_Name: WideString;
begin
  Result := ChildNodes['name'].Text;
end;

procedure TXMLChilditemType.Set_Name(Value: WideString);
begin
  ChildNodes['name'].NodeValue := Value;
end;

function TXMLChilditemType.Get_Value: Integer;
begin
  Result := ChildNodes['value'].NodeValue;
end;

procedure TXMLChilditemType.Set_Value(Value: Integer);
begin
  ChildNodes['value'].NodeValue := Value;
end;

end.

关于xml - 将列表框数据保存到 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8073148/

有关xml - 将列表框数据保存到 XML?的更多相关文章

  1. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  2. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  3. ruby - RVM 使用列表[0] - 2

    是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论

  4. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  5. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  6. 世界前沿3D开发引擎HOOPS全面讲解——集3D数据读取、3D图形渲染、3D数据发布于一体的全新3D应用开发工具 - 2

    无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD

  7. FOHEART H1数据手套驱动Optitrack光学动捕双手运动(Unity3D) - 2

    本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01  客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02  数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit

  8. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  9. ruby-on-rails - 创建 ruby​​ 数据库时惰性符号绑定(bind)失败 - 2

    我正在尝试在Rails上安装ruby​​,到目前为止一切都已安装,但是当我尝试使用rakedb:create创建数据库时,我收到一个奇怪的错误:dyld:lazysymbolbindingfailed:Symbolnotfound:_mysql_get_client_infoReferencedfrom:/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundleExpectedin:flatnamespacedyld:Symbolnotfound:_mysql_get_client_infoReferencedf

  10. STM32读取串口传感器数据(颗粒物传感器,主动上传) - 2

    文章目录1.开发板选择*用到的资源2.串口通信(个人理解)3.代码分析(注释比较详细)1.主函数2.串口1配置3.串口2配置以及中断函数4.注意问题5.源码链接1.开发板选择我用的是STM32F103RCT6的板子,不过代码大概在F103系列的板子上都可以运行,我试过在野火103的霸道板上也可以,主要看一下串口对应的引脚一不一样就行了,不一样的就更改一下。*用到的资源keil5软件这里用到了两个串口资源,采集数据一个,串口通信一个,板子对应引脚如下:串口1,TX:PA9,RX:PA10串口2,TX:PA2,RX:PA32.串口通信(个人理解)我就从串口采集传感器数据这个过程说一下我自己的理解,

随机推荐