Delphi 中的 XMLDocument 类详解关于 XML 属性
2008-07-07 20:07
615 查看
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.
[/code]
相关文章推荐
- 黄聪:Delphi 中的 XMLDocument 类详解(13) - 关于 XML 属性
- Delphi 中的 XMLDocument 类详解关于 XML 属性
- Delphi 中的 XMLDocument 类详解(13) - 关于 XML 属性
- 黄聪:Delphi 中的 XMLDocument 类详解(7) - 获取节点名称与节点的属性名称
- 黄聪:Delphi 中的 XMLDocument 类详解(11) - 一个多余的属性: Nodes
- Delphi 中的 XMLDocument 类详解(11) - 一个多余的属性: Nodes
- Delphi 中的 XMLDocument 类详解(11) - 一个多余的属性: Nodes
- Delphi 中的 XMLDocument 类详解(7) - 获取节点名称与节点的属性名称
- 黄聪:Delphi 中的 XMLDocument 类详解(9) - 关于 HasChildNodes 与 IsTextElement
- Delphi 中的 XMLDocument 类详解(9) - 关于 HasChildNodes 与 IsTextElement
- c++ builder 中的 XMLDocument 类详解(12) -关于 XML 属性
- c++ builder 中的 XMLDocument 类详解(12) -关于 XML 属性
- 黄聪:Delphi 中的 XMLDocument 类详解(6) - 访问节点属性
- Delphi 中的 XMLDocument 类详解(6) - 访问节点属性
- 黄聪:Delphi 中的 XMLDocument 类详解(4) - 获取根目录下的元素数
- 黄聪:Delphi 中的 XMLDocument 类详解(19) - NodeValue 与 NodeName 的读写区别
- c++ builder 中的 XMLDocument 类详解(9) -关于 HasChildNodes 与 IsTextElement
- c++ builder 中的 XMLDocument 类详解(9) -关于 HasChildNodes 与 IsTextElement
- Delphi 中的 XMLDocument 类详解(1) - 等待研究的内容
- 黄聪:Delphi 中的 XMLDocument 类详解(12) - 读取或设置 xml 的编码与版本号