Einzelnen Beitrag anzeigen

Benutzerbild von cookie22
cookie22

Registriert seit: 28. Jun 2006
Ort: Düsseldorf
936 Beiträge
 
Delphi XE2 Professional
 
#1

Xml in Treeview + Listview

  Alt 11. Feb 2009, 13:50
hallo,

ich mochte folgende datei in einen treeview und listview einlesen.

Delphi-Quellcode:
<?xml version='1.0' encoding='UTF-8'?>
<document>
   <group>
      <name>Windows</name>
     <index>2</index>
         <file>
            <title>file1</title>
            <user>user1</user>
            <info>info1</info>
         </file>
         <group>
            <name>System</name>
         <index>1</index>
               <file>
                  <title>file2</title>
                  <user>user2</user>
                  <info>info2</info>
               </file>
               <file>
                  <title>file3</title>
                  <user>user3</user>
                  <info>info3</info>
               </file>
               <file>
                  <title>file4</title>
                  <user>user4</user>
                  <info>info4</info>
               </file>
               <group>
                  <name>E-Mail</name>
              <index>3</index>
                     <file>
                        <title>file2</title>
                        <user>user2</user>
                        <info>info2</info>
                     </file>
                     <file>
                        <title>file3</title>
                        <user>user3</user>
                        <info>info3</info>
                     </file>
               </group>
         </group>
   </group>
   <group>
     <name>Internet</name>
    <index>3</index>
        <file>
           <title>file4</title>
           <user>user4</user>
           <info>info4</info>
        </file>
   </group>
</document>

die daten in den treeview zu bekommen ist dank marabus beispiel kein problem aber ich bekomm die jeweiligen daten (titel, user, info) nicht in den list view. Ich poste hier mal den code.



Delphi-Quellcode:
unit DemoFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, ComCtrls, MSXML2_Tlb, ActnList, StdActns,
  XPMan, ExtCtrls, TntComCtrls, TntStdCtrls, ActiveX;

type
  TDemoForm = class(TForm)
    TreeView: TTntTreeview;
    ActionList: TActionList;
    FileOpen: TFileOpen;
    XPManifest: TXPManifest;
    ActionPanel: TPanel;
    Button: TButton;
    Splitter: TSplitter;
    RightPanel: TPanel;
    Splitter1: TSplitter;
    Memo: TTntMemo;
    StatusBar: TStatusBar;
    TntListView1: TTntListView;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TreeViewChange(Sender: TObject; Node: TTreeNode);
    procedure FileOpenAccept(Sender: TObject);
  private
    XmlStream: TMemoryStream;
    function TreeNodes: TTntTreeNodes;
    procedure ShowContent(e: IXMLDOMElement);
    procedure ShowFolder(e: IXMLDOMElement; nParent: TTntTreeNode);
  end;

var
  DemoForm: TDemoForm;
  
implementation

{$R *.dfm}

uses
  StrUtils;

type
  TReference = class
  public
    Element: IXMLDOMElement;
    constructor Create(e: IXMLDOMElement);
  end;

var
  doc: IXMLDOMDocument;

constructor TReference.Create(e: IXMLDOMElement);
begin
  inherited Create;
  Element := e;
end;

function TDemoForm.TreeNodes: TTntTreeNodes;
begin
  Result := TreeView.Items;
end;

procedure TDemoForm.ShowContent(e: IXMLDOMElement);
var
  i: Integer;
  nl: IXMLDOMNodeList;
  s: String;
  n: IXMLDOMNode;
begin
  s := '';
  nl := e.selectNodes('* | text()');
  for i := 0 to Pred(nl.length) do
  begin
    n := nl.item[i];
    if n.nodeType = NODE_TEXT
      then s := s + n.text
      else s := s + Format('(%s)', [n.text]);
  end;
  Memo.Text := s;
end;

procedure TDemoForm.ShowFolder(e: IXMLDOMElement; nParent: TTntTreeNode);
var
  i: Integer;
  nl: IXMLDOMNodeList;
  n: TTntTreeNode;
  eChild, eName: IXMLDOMElement;
  s: WideString;
begin
  nl := e.selectNodes('*');
  for i := 0 to Pred(nl.length) do
  begin
    eChild := nl.item[i] as IXMLDOMElement;
    eName := eChild.selectSingleNode('name') as IXMLDOMElement;
    if eChild.tagName = 'groupthen
    begin
      s := (eName.text);
      n := TreeNodes.AddChildObject(nParent, s, TReference.Create(eChild));
      ShowFolder(eChild, n);
    end;
  end;
end;

procedure TDemoForm.FileOpenAccept(Sender: TObject);
begin
  with Sender as TFileOpen do
  begin
    doc := CoDomDocument.Create;
    doc.async := false;
    XmlStream := TMemoryStream.Create;
    XmlStream.LoadFromFile(Dialog.FileName);
    doc.Load(TStreamAdapter.Create(XmlStream, soReference) as IStream);
    begin
      StatusBar.SimpleText := Dialog.FileName;
      TreeNodes.Clear;
      ShowFolder(doc.documentElement, nil);
    end;
  end;
end;



procedure TDemoForm.TreeViewChange(Sender: TObject; Node: TTreeNode);
   var
  ref: TReference;
begin
  ref := TReference(Node.Data);
  ShowContent(ref.Element);
end;

procedure TDemoForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
XmlStream.Free;
end;

end.


dann kommt dazu das ich diese daten auch noch editieren mus, als neue gruppen im tree hinzufügen und neue files im listview bzw. diese editieren oder löschen. bin für jeden tip dankbar, hab noch nie wirklich mit xml gearbeitet und irgendwie hab ich total den überblick verloren.

danke für jede hilfe im voraus.

gruss,
cookie
  Mit Zitat antworten Zitat