Delphi读取XML

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, XMLIntf, XMLDoc, StdCtrls; type TStudent = Class(TObject) public Name: string; Sex: string; Age: Integer; Phone: string; end; TForm1 = class(TForm) Button1: TButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } FXMLDoc: IXMLDocument; // XML文档 FDocElement: IXMLNode; // 根节点 FList: TList; public { Public declarations } end; var Form1: TForm1; implementation uses Student; {$R *.dfm} const XMLString = '<?xml version="1.0" encoding="GB2312"?>' + '<Students>' + ' <Student Sex = "男">' + ' <Name>文文</Name>' + ' <Age>18</Age>' + ' <Phone>4745723</Phone>' + ' </Student>' + ' <Student Sex = "女">' + ' <Name>喃喃</Name>' + ' <Age>17</Age>' + ' <Phone>4644563</Phone>' + ' </Student>' + '</Students>'; procedure TForm1.FormCreate(Sender: TObject); begin FXMLDoc := TXMLDocument.Create(nil); FXMLDoc.XML.Text := XMLString; FXMLDoc.Active := True; FList := TList.Create; FDocElement := FXMLDoc.DocumentElement; end; procedure TForm1.FormDestroy(Sender: TObject); var i: Integer; begin FXMLDoc.Active := False; for i := 0 to Flist.Count - 1 do TStudent(FList.Items[i]).Free; FList.Free; end; procedure TForm1.Button1Click(Sender: TObject); var i: Integer; Stu: TStudent; StuNode: IXMLNode; begin for i := 0 to FDocElement.ChildNodes.Count - 1 do begin StuNode := FDocElement.ChildNodes[i]; Stu := TStudent.Create; Stu.Name := StuNode.ChildValues['Name']; Stu.Sex := StuNode.Attributes['Sex']; Stu.Age := StuNode.ChildValues['Age']; Stu.Phone := StuNode.ChildValues['Phone']; FList.Add(Stu); end; end; end.

你可能感兴趣的:(String,Integer,button,Delphi,encoding,Forms)