点击treeview中的节点可以在一个memo中显示该节点的相关属性,我现在希望把节点和其属性全部存成xml格式的文件中,应该怎样写代码?

解决方案 »

  1.   

    我能想到的是 用TTreeView+TXMLDocument+循环+递归 来进行处理.
      

  2.   

    具体怎样写啊?TXMLDocument我不熟。各位大侠帮帮忙,多谢了!
      

  3.   

    procedure ParseXML(const XMLNode: IXMLNode; const TreeNode: TTreeNode);
    var
      AXMLNode: IXMLNode;
      ATreeNode: TTreeNode;
    begin
      AXMLNode := XMLNode;
      while Assigned(AXMLNode) do
      begin
        ATreeNode := TreeNode.Items.Add(AXMLNode.NodeValue);
        ParseXML(AXMLRoot.ChildNodes.First, ATreeNode);
        AXMLNode := AXMLNode.NextSibling;
      end;
    end;parseXML(AXMLDocument.Node.ChildNodes.FindNode(XML_ROOT), ATreeRoot);
      

  4.   

    procedure TreeNodeToXML(const XMLNode: IXMLNode; const TreeNode: TTreeNode);
    var
      ATreeNode: TTreeNode;
      AXMLNode: IXMLNode;
    begin
      ATreeNode:= TreeNode;
      while Assigned(ATreeNode) do
      begin
        AXMLNode := AXMLRoot.AddChild(ATreeNode.Text);
        if ATreeNode.HasChild then
          ParseXML(AXMLNode, ATreeNode.Items.GetFirstNode);
        ATreeNode:= ATreeNode.NextSibling;
      end;
    end;
      

  5.   

    thank you!
    我试一下先!
      

  6.   

    budded:上面的代码我不是很明白诶,麻烦你解释一下吧,多谢了。
            难道xml文件的格式不需要自己定义的吗?
            ParseXML是用来干什么的啊!
      

  7.   

    源码如下:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls, xmldom, XMLIntf, msxmldom, XMLDoc;type
      TForm1 = class(TForm)
        TreeView1: TTreeView;
        Button1: TButton;
        XMLDocument1: TXMLDocument;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
        procedure XMLToTree(XMLNode:IXMLNode; ParentNode:TTreeNode; TV:TTreeView);
        procedure TreeToXML(TreeNode:TTreeNode; ParentNode:IXMLNode);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.XMLToTree(XMLNode:IXMLNode; ParentNode:TTreeNode; TV:TTreeView);
    var
      i:integer;
      Node: TTreeNode;
    begin
      Node := TV.Items.AddChild(ParentNode,XMLNode.NodeName);
      for i := 0 to XMLNode.AttributeNodes.Count - 1 do
        TV.Items.AddChild(Node,XMLNode.AttributeNodes[i].NodeName
          + '=' + XMLNode.AttributeNodes[i].NodeValue);
        //属性值以NAME=VALUE格式存储到TREEVIEW中
      for i:=0 to XMLNode.ChildNodes.Count-1 do
        XMLToTree(XMLNode.ChildNodes[i], Node, TV);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      XMLDocument1.FileName := 'E:\tmp\test.xml';
      XMLDocument1.Active := True;
      TreeView1.Items.Clear;
      XMLToTree(XMLDocument1.DocumentElement, nil, TreeView1);
      TreeView1.FullExpand;
    end;procedure TForm1.TreeToXML(TreeNode:TTreeNode; ParentNode:IXMLNode);
    var
      i:integer;
      Node: IXMLNode;
      Name, Value: String;
    begin
      if ParentNode = Nil then
        Node := XMLDocument1.AddChild(TreeNode.Text)
      else
        Node := ParentNode.AddChild(TreeNode.Text);
      for i := 0 to TreeNode.Count - 1 do
        if Pos('=', TreeNode[i].Text) > 0 then
        begin
          //如果TV的NODE中有"=",则说明该结点为属性值
          Name  := Copy(TreeNode[i].Text, 1, Pos('=', TreeNode[i].Text) - 1);
          Value := Copy(TreeNode[i].Text, Pos('=', TreeNode[i].Text) + 1,
            Length(TreeNode[i].Text) - Pos('=', TreeNode[i].Text));
          Node.SetAttributeNS(Name, '=', Value);
        end else
        begin
          //否则是子结点
          TreeToXML(TreeNode[i], Node);
        end;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      XMLDocument1.ChildNodes.Clear;
      XMLDocument1.Encoding := 'GB2312';
      TreeToXML(TreeView1.Items[0], Nil);
      XMLDocument1.SaveToFile('E:\tmp\test2.xml');
    end;end.
      

  8.   

    XML源文件如下:<?xml version="1.0" encoding="GB2312"?>
    <DSTreeRoot text="地球" open="true" href="http://" target="box" treeId="0">
    <DSTree text="中国" open="true" href="http://" target="box" treeId="00">
    <DSTree text="湖北" open="false" href="http://" target="box" treeId="000">
    <DSTree text="武汉" open="false" href="http://" target="box" treeId="0000"/>
    <DSTree text="黄石" open="false" href="http://" target="box" treeId="0001"/>
    <DSTree text="宜昌" href="http://" icon="earth.gif" target="box" treeId="0123"/>
    <DSTree text="十堰" open="false" href="http://十堰" target="box" treeId="0002">
    <DSTree text="城区" open="false" href="http://城区" target="box" treeId="00020">
    <DSTree text="人民路" open="false" href="http://人民路" target="box" treeId="000200"/>
    <DSTree text="公园" open="false" href="http://" target="box" treeId="000201"/>
    <DSTree text="电脑城" open="false" href="http://" target="box" treeId="000202"/>
    </DSTree>
    </DSTree>
    </DSTree>
    <DSTree text="湖南" open="true" href="http://" target="box" treeId="001">
    <DSTree text="岳阳" open="false" href="http://" target="box" treeId="0011">
      <DSTree text="青海" open="true" href="http://" target="box" treeId="002">
    <DSTree text="平安" open="false" href="http://" target="box" treeId="0020"/>
    <DSTree text="玉树" open="false" href="http://" target="box" treeId="0021"/>
    </DSTree>
    </DSTree>
    <DSTree text="长沙" open="false" href="http://" target="box" treeId="0010"/>
    </DSTree>
    </DSTree>
    <DSTree text="外国" open="true" treeId="01">
    <DSTree text="日本" open="false" href="http://" target="box" treeId="011">
    <DSTree text="广岛" href="http://" target="box" treeId="0110" open="false">
      <DSTree text="东京" href="http://" target="box" treeId="0111"/>
    </DSTree>
    </DSTree>
    <DSTree text="韩国" href="http://" target="box" treeId="010" open="true"/>
    <DSTree text="美国" open="false" href="http://" target="box" treeId="012">
    <DSTree text="纽约" href="http://" target="box" treeId="0120"/>
    <DSTree text="华盛顿" href="http://" target="box" treeId="0121"/>
    </DSTree>
    </DSTree>
    </DSTreeRoot>
      

  9.   

    程序可以将XML导入TREEVIEW中,再从TREEVIEW中生成另一XML文件