以前没接触过,请大家帮忙

解决方案 »

  1.   

    用Internet标签页的TXMLDocument组件,很方便,跟TTreeview差不多,看看就会了
      

  2.   

    用XMLDocument操作,或者XMLDocument1.DOMDocument进行操作;
    如果xml文档格式固定,推荐用new-〉xml data binding;形成新的类;
    更好的操作特定的文档
      

  3.   

    unit MainUnt;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, XPMan, ActnMan, ActnColorMaps, CustomizeDlg, xmldom, XMLIntf,
      msxmldom, XMLDoc, StdCtrls, ExtCtrls, ComCtrls;type  TMainFrm = class(TForm)
        XMLDocument1: TXMLDocument;
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        Button5: TButton;
        Button6: TButton;
        Button7: TButton;
        Button8: TButton;
        RadioGroup1: TRadioGroup;
        Button9: TButton;
        Panel1: TPanel;
        PageControl1: TPageControl;
        TabSheet1: TTabSheet;
        Memo1: TMemo;
        TabSheet2: TTabSheet;
        TreeView1: TTreeView;
        Splitter1: TSplitter;
        Panel2: TPanel;
        Splitter2: TSplitter;
        ListBox1: TListBox;
        RichEdit1: TRichEdit;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure RadioGroup1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button9Click(Sender: TObject);
      private
        FXMLNodeName : Array of String;
        Procedure InitInf;
        Function ExistsXMLDoc(XMLDocValue : TXMLDocument) : Boolean;
        Procedure XMLToTree(XMLDocValue : TXMLDocument);
        { Private declarations }
      public
         FXMLName : String;
         FXMLDocument : TXMLDocument;
         _RootValue : String;
         //FXMLName : String;
        { Public declarations }
      end;var
      MainFrm: TMainFrm;implementation
    uses
      SetParams;{$R *.dfm}procedure TMainFrm.Button1Click(Sender: TObject);
    var
      vOpenDialog : TOpenDialog;
    begin
      vOpenDialog := TOpenDialog.Create(Nil);
      vOpenDialog.Filter := 'XML文档|*.XML';
      if vOpenDialog.Execute then
      begin
        FXMLDocument.LoadFromFile(vOpenDialog.FileName);
        FXMLName := vOpenDialog.FileName;
        Memo1.Lines := FXMLDocument.XML;
      end;
    end;procedure TMainFrm.InitInf;
    begin
      //..
      FXMLDocument := TXMLDocument.Create(Application);
      PageControl1.ActivePageIndex := 0;
    end;procedure TMainFrm.FormCreate(Sender: TObject);
    begin
      InitInf;
    end;function TMainFrm.ExistsXMLDoc(XMLDocValue: TXMLDocument): Boolean;
    begin
      Result := True;
      if XMLDocValue.IsEmptyDoc then
        Result := False;
    end;procedure TMainFrm.XMLToTree(XMLDocValue: TXMLDocument);
    var
      Root ,iRoot,Root_ : IXMLNode;
      vIndex : Integer;
      TNode ,PNode , CNode : TTreeNode;
      vMaxCount : Integer;  //
    begin
      vMaxCount := 0;
      Root := XMLDocValue.DocumentElement;
      TNode := TreeView1.Items.AddChild(Nil,Root.DOMNode.NodeName);
      {&DocumentElement: IXMLNode read GetDocumentElement write SetDocumentElement;}
      iRoot := Root.ChildNodes.First;
      SetLength(FXMLNodeName,iRoot.ChildNodes.Count);
      while iRoot <> Nil do
      begin
        PNode := TreeView1.Items.AddChild(TNode,iRoot.DOMNode.NodeName);
        _RootValue := iRoot.DOMNode.NodeName;
        for vIndex := 0 to iRoot.ChildNodes.Count - 1 do
        begin
          //..
          TreeView1.Items.AddChild(PNode,iRoot.ChildNodes[vIndex].Text);
          if iRoot = Root.ChildNodes.First then
          begin
            FXMLNodeName[vIndex] := iRoot.ChildNodes[vIndex].DOMNode.NodeName;
          end;
        end;
        iRoot := iRoot.NextSibling;
      end;
    end;procedure TMainFrm.RadioGroup1Click(Sender: TObject);
    var
      Root , iRoot : IXMLNode;
    begin
      PageControl1.ActivePageIndex := RadioGroup1.ItemIndex;
      if Not ExistsXMLDoc(FXMLDocument) then
        Exit;  
      if TreeView1.Items.Count = 0 then
        XMLToTree(FXMLDocument) else
        Exit;
    end;procedure TMainFrm.Button2Click(Sender: TObject);
    var
      SetParamsFrm: TSetParamsFrm;
      vIndex : Integer;
    begin
      //..
      SetParamsFrm := TSetParamsFrm.Create(Application);
      SetParamsFrm.ParamGrid.RowCount := High(FXMLNodeName) - Low(FXMLNodeName) + 2;
      SetParamsFrm.ParamGrid.Cells[0,0] := 'Parameters';
      SetParamsFrm.ParamGrid.Cells[1,0] := 'Value';
      For vIndex := Low(FXMLNodeName) to High(FXMLNodeName) do
      begin
        SetParamsFrm.ParamGrid.Cells[0,vIndex + 1] := FXMLNodeName[vIndex];
      end;
      SetParamsFrm.StatuInfo.Panels[0].Text := 'Append XML Node';
      SetParamsFrm.StatuInfo.Panels[1].Text := _RootValue;
      SetParamsFrm.ShowModal;
    end;procedure TMainFrm.Button9Click(Sender: TObject);
    begin
      SetParamsFrm := TSetParamsFrm.Create(Application);
      SetParamsFrm.ParamGrid.RowCount := 10;
      SetParamsFrm.ParamGrid.FixedCols := 0;
      SetParamsFrm.ParamGrid.ColCount := 2;  SetParamsFrm.ParamGrid.Cells[0,0] := 'Parameters';
      SetParamsFrm.ParamGrid.Cells[1,0] := 'Value';
      {For vIndex := Low(FXMLNodeName) to High(FXMLNodeName) do
      begin
        SetParamsFrm.ParamGrid.Cells[0,vIndex + 1] := FXMLNodeName[vIndex];
      end;  }
      SetParamsFrm.StatuInfo.Panels[0].Text := 'New XML File';
      //SetParamsFrm.StatuInfo.Panels[1].Text := _RootValue;
      SetParamsFrm.ShowModal;
    end;end.
      

  4.   

    unit SetParams;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Buttons, ExtCtrls,  xmldom, XMLIntf,msxmldom, XMLDoc,Grids,MainUnt,
      ComCtrls;type
      TSetParamsFrm = class(TForm)
        Panel1: TPanel;
        SpeedButton1: TSpeedButton;
        SpeedButton2: TSpeedButton;
        PageControl1: TPageControl;
        TabSheet1: TTabSheet;
        TabSheet2: TTabSheet;
        ParamGrid: TStringGrid;
        StringGrid1: TStringGrid;
        StatuInfo: TStatusBar;
        vXMLDocument1: TXMLDocument;
        procedure SpeedButton2Click(Sender: TObject);
        procedure SpeedButton1Click(Sender: TObject);
      private
        Procedure NewXMLFile;
        Function GetParam : Integer;
        { Private declarations }
      public
        { Public declarations }
      end;var
      SetParamsFrm: TSetParamsFrm;implementation{$R *.dfm}procedure TSetParamsFrm.SpeedButton2Click(Sender: TObject);
    begin
      Close;
    end;procedure TSetParamsFrm.SpeedButton1Click(Sender: TObject);
      Function CheckArray(Name,Value: array of String): Boolean;
      var
        AIndex : Integer;
      begin
        Result := True;
        if ( High(Name) - Low(Name)) <> (High(Value) - Low(Value)) then
          Result := False;
      end;  procedure SetParameters(Root_Name:  String ;Name , Value : Array of String) ;
      var
        Root ,iRoot,jRoot,Root_ : IXMLNode;
        vIndex : Integer;
      begin
        if Not CheckArray(Name , Value) then
          Exit;
        Root := MainFrm.FXMLDocument.DocumentElement;
        jRoot := Root.ChildNodes.First;
        Root_ := Root.AddChild(Root_Name);
        //Root_.Attributes[Name_Value] := Name_Text;
        For vIndex := Low(Name) to High(Name) do
        begin
          iRoot := Root_.AddChild(Name[vIndex]);
          Root_.ChildNodes[Root_.ChildNodes.IndexOf(Name[vIndex])].Text := Value[vIndex];
        end;
        MainFrm.FXMLDocument.SaveToFile(MainFrm.FXMLName);
      end;
    var
      Root ,iRoot,jRoot,Root_ : IXMLNode;
      vIndex : Integer;
      vName , vValue : Array of String;
    begin
      if ParamGrid.RowCount < 1 then
        Exit;
      if StatuInfo.Panels[0].Text = 'New XML File' then
      begin
        NewXMLFile;
        Exit;
      end;
      if StatuInfo.Panels[0].Text = 'Edit XML Node' then
      begin
        //..
        Exit;
      end;  SetLength(vName,ParamGrid.RowCount - 1);
      SetLength(vValue,ParamGrid.RowCount - 1);
      For vIndex := 0 to ParamGrid.RowCount - 2 do
      begin
        //..
        vName[vIndex] := ParamGrid.Cells[0,vIndex + 1];
        vValue[vIndex] := ParamGrid.Cells[1,vIndex + 1];
      end;
      Root := MainFrm.FXMLDocument.DocumentElement;
      jRoot := Root.ChildNodes.First;
      SetParameters(StatuInfo.Panels[1].Text,vName,vValue);
      MainFrm.FXMLDocument.SaveToFile(MainFrm.FXMLName);
      Close;
    end;procedure TSetParamsFrm.NewXMLFile;
    Const
      CondStr = ' <?xml version="1.0" encoding="GB2312" ?> ';
    var
      vXMLDocument : TXMLDocument;
      Root , _Root ,iRoot : IXMLNode;
      vIndex : Integer;
      vOpenDialog : TOpenDialog;
    begin
      //..
      if GetParam = 0 then
        Exit;
      //vOpenDialog := TOpenDialog.Create(Nil);
     // vOpenDialog.Filter := 'XML文档|*.XML';
     // if vOpenDialog.Execute then
     // begin
        //vXMLDocument.LoadFromFile(vOpenDialog.FileName);
        //FXMLName := vOpenDialog.FileName;
        //Memo1.Lines := FXMLDocument.XML;
     // end;  vXMLDocument := TXMLDocument.Create(Nil);
      //vXMLDocument.LoadFromFile('Temp.XML');
      vXMLDocument.CreateElement(CondStr,'http://www.w3c.com');
      //vXMLDocument.Encoding := CondStr;
      Root := vXMLDocument.CreateNode('xxxxxxxxxxx');
      Root := Root.AddChild('学生花名册');
      Root.Attributes['xx'] := 'dsds';
      Root.AddChild('aaaa');
      _Root := Root.AddChild('aaaaa');
      vXMLDocument.SaveToFile('xxx.XML');
    end;function TSetParamsFrm.GetParam: Integer;
    var
      vIndex , vCount : Integer;
    begin
      vCount := 0;
      For vIndex := 0 to ParamGrid.RowCount - 1 do
      begin
        if Trim(ParamGrid.Cells[0,vIndex + 1]) = '' then
        begin
          Result := vCount;
          Exit;
        end;
        vCount := vIndex ;
      end;
      Result := vCount;
    end;end.
      

  5.   

    ihihonline(小卒)
    兄弟,给的代码太长了,不直观,不能体现xml的操作方法,你看那个demo代码给的那么长
    只是示意性的写上几段,不一定非的要能执行,伪代码也不要紧;负责楼主根本抓不住主次提点意见,不只对不对,仅供参考
      

  6.   

    我用控制台写的:
    program Project1;{$APPTYPE CONSOLE}uses
      SysUtils,WIndows,Classes,msxml,xmldom,xmlintf,activex,comObj;var xmlDoc:IXMLDOMDocument;    xmlRoot,xmlNode:IXMLDOMNode;
        xmlAttr:IXMLDOMAttribute;
    s:string;
    begin
      { TODO -oUser -cConsole Main : Insert code here }
        CoInitialize(nil);{初始化}
        xmlDoc:=CreateOleObject('MSXML.DOMDocument') as IXMLDOMDocument;{创建一个MSXML的DOM对象}
        xmlDoc.appendChild(xmlDoc.createProcessingInstruction('xml','version="1.0"'));{创建并添加一个XMLParser处理器指令}
        xmlRoot:=xmlDoc.createElement('Root');{创建一个根节点}
        xmlDoc.appendChild(xmlRoot);{将根节点添加到XML文档对象中}
        xmlAttr:=xmlDoc.createAttribute('test');{创建一个test属性}
        xmlAttr.nodeValue :='Value';{设置属性值}
        xmlRoot.attributes.setNamedItem(xmlAttr);{将属性添加到根节点上}
        xmlNode:=xmlDoc.createElement('Element1');{创建一个节点}
        xmlNode.text :='ABCDEFG';{设置属性值}
        xmlRoot.appendChild(xmlNode);{将创建的新节点添加到根节点上}
        xmlDoc.save('c:\test.xml');{将生成的XML文档保存下来}
        writeln(xmlDoc.xml);{输出XML内容}
        readln(s);
        CoUninitialize;
    end.
      

  7.   

    这个是输出的内容:
    <?xml version="1.0"?>
    <Root test="Value"><Element1>ABCDEFG</Element1></Root>
      

  8.   

    相应的,读取XML的例子:
    program Project1;{$APPTYPE CONSOLE}uses
      SysUtils,WIndows,Classes,msxml,xmldom,xmlintf,activex,comObj;var xmlDoc:IXMLDOMDocument;    xmlRoot,xmlNode,xmlAttr:IXMLDOMNode;s:string;
    begin
      { TODO -oUser -cConsole Main : Insert code here }
        CoInitialize(nil);{初始化}
        xmlDoc:=CreateOleObject('MSXML.DOMDocument') as IXMLDOMDocument;{创建一个MSXML的DOM对象}
        xmlDoc.load('c:\test.xml');
        xmlRoot:=xmlDoc.selectSingleNode('/Root');
        xmlAttr:=xmlDoc.selectSingleNode('/Root/@test');
        xmlNode:=xmlDoc.selectSingleNode('/Root/Element1');
        Writeln('根节点XML内容:'#13#10+xmlRoot.xml);
        Writeln('根节点的test属性:'+xmlAttr.text);
        Writeln('根节点下的子节点Element1的XML内容:'#13#10+xmlNode.xml);
        readln(s);
        CoUninitialize;
    end.