黄聪:Delphi 中的 XMLDocument 类详解(17) 上一个节点、下一个节点、父节点

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls;

type
TForm1
= class(TForm)
XMLDocument1: TXMLDocument;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.FormCreate(Sender: TObject);
begin
XMLDocument1.LoadFromFile(
'c:\temp\test.xml');
{必须用万一提供的 xml 测试文件, 才能有相同的返回值}
end;


//某节点的上一个节点、下一个节点、父节点
procedure TForm1.Button1Click(Sender: TObject);
var
node,nodeX: IXMLNode;
begin
node :
= XMLDocument1.DocumentElement;
node :
= node.ChildNodes[1];
node :
= node.ChildNodes[1];
{现在的节点 node 指向了我们的例子中的第二个人员"李四"}

ShowMessage(node.ChildNodes[
0].NodeValue); {}

//获取该节点的上一个节点, 它上一个节点应该是李四的姓名
nodeX :
= node.PreviousSibling; {PreviousSibling}
ShowMessage(nodeX.NodeValue);
{李四}

//获取该节点的下一个节点, 它下一个节点应该是李四的年龄
nodeX :
= node.NextSibling; {NextSibling}
ShowMessage(nodeX.NodeValue);
{43}

//获取该节点的父节点
nodeX :
= node.ParentNode; {ParentNode}
ShowMessage(nodeX.NodeName);
{人员}
end;

end.

出处:http://www.cnblogs.com/del/archive/2008/01/06/1027605.html

原文地址:https://www.cnblogs.com/huangcong/p/1809958.html