tcxtreelist的问题(40)

  • 主题发起人 主题发起人 9605201
  • 开始时间 开始时间
9

9605201

Unregistered / Unconfirmed
GUEST, unregistred user!
下面这个是NativeXml自带的例子. NativeXml可在盒子里下载到默认的是XML直接读取显示在listview里,我想显示在tcxtreelist里unit Example3Main;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, NativeXml, ComCtrls, cxStyles, cxGraphics, cxEdit, cxCustomData, cxTL, cxMaskEdit, cxTLdxBarBuiltInMenu, cxInplaceContainer, cxDBTL, cxTLData, cxVGrid, cxDBVGrid, cxControls, cxContainer, cxListView, dxSkinsCore, dxSkinOffice2007Blue, cxLookAndFeels, dxSkinsForm, cxTextEdit, cxTreeView;type // This is the record structure FieldRecord= record FieldId: WideString; FieldReq: WideString; FieldType: WideString; FieldCap: WideString; DependField: WideString; MetaType: WideString; MetaData: WideString; end; TForm1 = class(TForm) mmXML: TMemo; btnImportXML: TButton; lbStatus: TLabel; Label1: TLabel; lvTable: TListView; Label2: TLabel; Label3: TLabel; lst1: TcxListView; lst3: TcxTreeList; lst4: TcxVirtualTreeList; cxtrvw1: TcxTreeView; cxtrlstclmnlst3Column1: TcxTreeListColumn; cxtrlstclmnlst3Column2: TcxTreeListColumn; cxtrlstclmnlst3Column3: TcxTreeListColumn; cxtrlstclmnlst3Column4: TcxTreeListColumn; cxtrlstclmnlst3Column5: TcxTreeListColumn; cxtrlstclmnlst3Column6: TcxTreeListColumn; cxtrlstclmnlst3Column7: TcxTreeListColumn; cxtrlstclmnlst4Column1: TcxTreeListColumn; cxtrlstclmnlst4Column2: TcxTreeListColumn; cxtrlstclmnlst4Column3: TcxTreeListColumn; cxtrlstclmnlst4Column4: TcxTreeListColumn; cxtrlstclmnlst4Column5: TcxTreeListColumn; cxtrlstclmnlst4Column6: TcxTreeListColumn; cxtrlstclmnlst4Column7: TcxTreeListColumn; btn1: TButton; procedure btnImportXMLClick(Sender: TObject); procedure lvTableData(Sender: TObject; Item: TListItem); procedure lst1Data(Sender: TObject; Item: TListItem); procedure btn1Click(Sender: TObject); private public Fields: array of FieldRecord; // This is a dynamic array of records procedure LoadFieldFromNode(var AField: FieldRecord; ANode: TXmlNode); end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.btnImportXMLClick(Sender: TObject);// Read XML from TMemo and convert into record structurevar i: integer; ADoc: TNativeXml; ANode: TXmlNode; AList: TList;begin ADoc := TNativeXml.Create; try // Read from memo ADoc.ReadFromString(mmXML.Text); // A temporary list to hold references to the record elements AList := TList.Create; try ANode := ADoc.Root.NodeByName('fields'); if not assigned(ANode) then exit; // List of nodes that are named "field" ANode.NodesByName('field', AList); // Set dynamic array length SetLength(Fields, AList.Count); // Import each element for i := 0 to AList.Count - 1 do LoadFieldFromNode(Fields, AList); // Show table lvTable.Items.Count := AList.Count; lvTable.Invalidate; lst1.Items.Count := AList.Count; lst1.Invalidate; lst3.Root.Count := AList.Count;//编译的时候这里直接提示[DCC Error] Example3Main.pas(167): E2129 Cannot assign to a read-only property lst3.Invalidate; // Status lbStatus.Caption := Format('Imported %d records', [AList.Count]); finally AList.Free; end; finally ADoc.Free; end;end;procedure TForm1.LoadFieldFromNode(var AField: FieldRecord; ANode: TXmlNode);// Load one field from the XML element ANodevar i: integer; AMeta: TXmlNode; AEnum: TXmlNode; AList: TList;begin with AField, ANode do begin // Initialize record FillChar(AField, SizeOf(AField), 0); // The flat data FieldId := AttributeByName['id']; FieldReq := AttributeByName['required']; FieldType := AttributeByName['type']; FieldCap := ReadString('caption'); DependField := AttributeByName['dependsfield']; // Meta data AMeta := NodeByName('metadata'); if assigned(AMeta) then with AMeta do begin MetaType := AttributeByName['type']; // List that holds enumeration AList := TList.Create; try // "enum" metadata if MetaType = 'enum' then begin AEnum := NodeByName('enumeration'); if assigned(AEnum) then AEnum.NodesByName('enumvalue', AList); // all enumeration values for i := 0 to AList.Count - 1 do with TXmlNode(AList) do MetaData := MetaData + AttributeByName['id'] + ',' + ReadString('caption') + ';'; end; // "hashtable" metadata if MetaType = 'hashtable' then begin AEnum := NodeByName('hashtable'); if assigned(AEnum) then AEnum.NodesByName('code', AList); // all enumeration values for i := 0 to AList.Count - 1 do with TXmlNode(AList) do MetaData := MetaData + AttributeByName['id'] + ',' + ValueAsString + ';'; end; finally AList.Free; end; end; end;end;procedure TForm1.lvTableData(Sender: TObject; Item: TListItem);// Add data to each item in the listview. We use the listview with OwnerDatabegin if (Item.Index >= 0) and (Item.Index < Length(Fields)) then begin with Fields[Item.Index] do begin Item.Caption := FieldID; Item.SubItems.Add(FieldReq); Item.SubItems.Add(FieldType); Item.SubItems.Add(FieldCap); Item.SubItems.Add(DependField); Item.SubItems.Add(MetaType); Item.SubItems.Add(MetaData); end; end;end;end.
 
继续关注。。。。。。。。。[:D]
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
928
SUNSTONE的Delphi笔记
S
后退
顶部