黄聪

论SEO对人类的重要性,请看我的博客:hcsem.com

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
unit Unit1;

interface

uses
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.

出处:http://www.cnblogs.com/del/archive/2008/01/03/1025080.html

posted on 2010-08-27 11:29  黄聪  阅读(486)  评论(0编辑  收藏  举报