Delphi 中的 XMLDocument 類詳解(15) – 建立與儲存 xml

來源:互聯網
上載者:User
unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, xmldom, XMLIntf, StdCtrls, msxmldom, XMLDoc;type  TForm1 = class(TForm)    XMLDocument1: TXMLDocument;    Button1: TButton;    Button2: TButton;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);  end;var  Form1: TForm1;implementation{$R *.dfm}//利用 XML 屬性建立 xml 檔案procedure TForm1.Button1Click(Sender: TObject);begin  XMLDocument1.XML.Clear;  XMLDocument1.XML.Add('');  XMLDocument1.XML.Add('');  XMLDocument1.XML.Add('');  XMLDocument1.XML.Add('張三姓名>');  XMLDocument1.XML.Add('男性別>');  XMLDocument1.XML.Add('34年齡>');  XMLDocument1.XML.Add('人員>');  XMLDocument1.XML.Add('科室名單>');  {查看}  ShowMessage(XMLDocument1.XML.Text);  {儲存}  XMLDocument1.Active := True;  XMLDocument1.SaveToFile('c:\temp\1.xml');end;//建立 xml 檔案的標準方法procedure TForm1.Button2Click(Sender: TObject);var  pNode,cNode: IXMLNode; {定義兩個節點: 父節點、子節點}begin  XMLDocument1.XML.Clear;  XMLDocument1.Active := True;                {必須先啟用}  XMLDocument1.Version := '1.0';              {設定版本}  XMLDocument1.Encoding := 'GB2312';          {設定語言}  pNode := XMLDocument1.AddChild('科室名單'); {添加的第一個節點是根節點, 現在的 pNode 是根節點}  pNode.SetAttribute('備忘', '測試');         {為根節點設定屬性}  pNode := pNode.AddChild('人員');            {為根節點添加子節點, 現在的 pNode 是 "人員" 節點}  pNode.SetAttribute('職務', '科長');         {設定屬性}  pNode.SetAttribute('備忘', '正局級');  cNode := pNode.AddChild('姓名');  {為 pNode 添加子節點, 傳回值 cNode 指向了新添加的節點}  cNode.Text := '張三';  cNode := pNode.AddChild('性別');  cNode.Text := '男';  cNode := pNode.AddChild('年齡');  cNode.Text := '34';  {查看}  ShowMessage(XMLDocument1.XML.Text);  {儲存}  XMLDocument1.SaveToFile('c:\temp\2.xml');end;end.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.