首页 > 代码库 > delphi XML简单处理
delphi XML简单处理
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Xml.XMLDoc, Xml.xmldom, Xml.XMLIntf, Vcl.StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var IDoc: IXMLDocument; Root, N1, N2, N3: IXMLNode; I: Integer; begin IDoc := TXMLDocument.Create(nil); IDoc.Active := True; Root := IDoc.AddChild(‘公司‘); for I := 1 to 9 do begin N1 := Root.AddChild(‘部门‘); N1.Attributes[‘Id‘] := I; N1.AddChild(‘名称‘).NodeValue := ‘部门_‘+I.ToString; N1.AddChild(‘主管‘).NodeValue := ‘主管_‘+I.ToString; end; IDoc.SaveToFile(‘d:\jjw.xml‘); end; procedure TForm1.Button2Click(Sender: TObject); var Doc: IXMLDocument; Node: IXMLNode; I: Integer; begin Doc := TXMLDocument.Create(nil); Doc.LoadFromFile(‘d:\jjw.xml‘); Doc.Active := True; Node := Doc.DocumentElement; for I := 0 to Node.ChildNodes.Count-1 do Node.ChildNodes[I].AddChild(‘人数‘).NodeValue := IntToStr(I+1); Doc.SaveToFile(‘d:\jjw.xml‘); end; procedure TForm1.Button3Click(Sender: TObject); var Doc: IXMLDocument; Node: IXMLNode; I: Integer; begin Doc := TXMLDocument.Create(nil); Doc.LoadFromFile(‘d:\jjw.xml‘); Doc.Active := True; Node := Doc.DocumentElement; for I := 0 to Node.ChildNodes.Count-1 do Node.ChildNodes[I].ChildNodes.Delete(2); Doc.SaveToFile(‘d:\jjw.xml‘); end; procedure TForm1.Button4Click(Sender: TObject); var Doc: IXMLDocument; Root, Node: IXMLNode; I: Integer; begin Doc := TXMLDocument.Create(nil); Doc.LoadFromFile(‘d:\jjw.xml‘); Doc.Active := True; Root := Doc.DocumentElement; for I := 0 to Root.ChildNodes.Count-1 do begin Node := Root.ChildNodes[I]; if Node.Attributes[‘Id‘]=3 then Node.ChildNodes[2].NodeValue := 33333; end; Doc.SaveToFile(‘d:\jjw.xml‘); end; end.
delphi XML简单处理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。