DelphiのXMLDocumentクラスの詳細(7)-ノード名とノードの属性名を取得

1481 ワード



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;

    Button2: TButton;

    procedure FormCreate(Sender: TObject);

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  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: IXMLNode;

begin

  node := XMLDocument1.DocumentElement;

  ShowMessage(node.NodeName); { }



  node := node.ChildNodes[0]; {XMLDocument1.DocumentElement.ChildNodes[0]}

  ShowMessage(node.NodeName); { }



  node := node.ChildNodes[0]; {XMLDocument1.DocumentElement.ChildNodes[0].ChildNodes[0]}

  ShowMessage(node.NodeName); { }

end;





// 

procedure TForm1.Button2Click(Sender: TObject);

var

  node: IXMLNode;

begin

  node := XMLDocument1.DocumentElement;

  ShowMessage(node.AttributeNodes[0].NodeName); { }



  node := node.ChildNodes[0]; {XMLDocument1.DocumentElement.ChildNodes[0]}

  ShowMessage(node.AttributeNodes[0].NodeName); { }

end;



end.