Delphi 中的 XMLDocument 類詳解(13) – 關於 XML 屬性

來源:互聯網
上載者:User
unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls;type  TForm1 = class(TForm)    XMLDocument1: TXMLDocument;    Memo1: TMemo;    Button1: TButton;    Button2: TButton;    Button3: TButton;    Button4: TButton;    Button5: TButton;    procedure FormCreate(Sender: TObject);    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure Button3Click(Sender: TObject);    procedure Button4Click(Sender: TObject);    procedure Button5Click(Sender: TObject);  end;var  Form1: TForm1;implementation{$R *.dfm}//開啟procedure TForm1.FormCreate(Sender: TObject);begin  XMLDocument1.LoadFromFile('c:\temp\test.xml');  {必須用萬一提供的 xml 測試檔案, 才能有相同的傳回值}end;//XMLDocument1 對象的 XML 屬性procedure TForm1.Button1Click(Sender: TObject);begin  {前面我們經常用這句話, 因為這裡的 Lines 與 XML 同屬於 TStrings}  Memo1.Lines := XMLDocument1.XML;  {如果不怕麻煩, 這樣寫也可以:}  Memo1.Lines.Text := XMLDocument1.XML.Text;  {如果知道了這一點, 那 XML 屬性的功能可就多了, 並且這裡的 XML 屬性是可寫的}  {不過這裡要談的是節點的 XML 屬性}end;//根節點的 XML 屬性procedure TForm1.Button2Click(Sender: TObject);var  node: IXMLNode;begin  {先看根節點: XMLDocument1.DocumentElement}  node := XMLDocument1.DocumentElement;  //Memo1.Lines := node.XML; {這一句會出錯}  {因為節點的 XML 屬性是 WideString 類型的, 應該這樣:}  Memo1.Lines.Text := node.XML; {將會讀出根節點與根節點包含的所有內容}  {還有一個更大的區別: 節點的 XML 是唯讀!}end;//子節點的 XML 屬性procedure TForm1.Button3Click(Sender: TObject);var  node: IXMLNode;begin  node := XMLDocument1.DocumentElement;  node := node.ChildNodes[0];  Memo1.Lines.Text := node.XML; {會顯示一個子節點的全部}end;//屬性的 XML 屬性procedure TForm1.Button4Click(Sender: TObject);var  node: IXMLNode;begin  node := XMLDocument1.DocumentElement;  node := node.AttributeNodes[0]; {屬性也是 IXMLNode 類型的}  ShowMessage(node.NodeName);  {備忘}  ShowMessage(node.NodeValue); {測試}  {用 XML 屬性一次把他們都讀出來:}  ShowMessage(node.XML);       {備忘="測試"}end;//分葉節點的 XML 屬性procedure TForm1.Button5Click(Sender: TObject);var  node: IXMLNode;begin  node := XMLDocument1.DocumentElement;  node := node.ChildNodes[0];  node := node.ChildNodes[0];  node := node.ChildNodes[0]; {這就是分葉節點了}  ShowMessage(node.XML);  {張三}  {這時的 XML 屬性和 Text 屬性一樣了}  ShowMessage(node.Text); {張三}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.