操作XML文件
unit UMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,XMLDoc, XMLIntf, xmldom,msxmldom, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Memo1: TMemo; Button4: TButton; Button5: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); private myXml:IXMLDocument; XmlRoot:IXMLNode; NodePos:IXMLNode; procedure SetMemo(value:String); public published Property FMemo:String Write SetMemo; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.SetMemo(value:String); begin Memo1.Lines.Add(value); Memo1.Lines.Add(''); end; //初始化 procedure TForm1.Button1Click(Sender: TObject); var filePath:String; begin myXml:=TXMLDocument.Create(nil); //创建时owner不能为nil,否则无法正常释放 myXml.Active:=true; myXml.Version:='1.0'; myXml.Encoding:='GB2312'; filePath:=ExtractFilePath(Application.ExeName)+'MyXml.xml'; myXml.LoadFromFile(filePath); FMemo:='初始化完成'; {关于保存缩进 myXml.NodeIndentStr:=#9;//指定缩进符,这里我用的是tab,也可以换成空格' ' 这里是4个空格 myXml.Options:=xml.Options + [doNodeAutoIndent];//这一段是关键,如果不指定为doNodeAutoIndent,缩进是不会生效的 } end; procedure TForm1.Button2Click(Sender: TObject); begin myXml._Release; end; //获得节点及属性 procedure TForm1.Button3Click(Sender: TObject); var str:String; nodeNum:Integer; i:Integer; noderef: IXMLDOMNodeRef; begin //得到根节点 XmlRoot:= myXml.DocumentElement; FMemo:='根节点的名字='+XmlRoot.NodeName; //当前节点下的子节点的数目 nodeNum:=XmlRoot.ChildNodes.Count; str:='当前节点下的子点节数目为: '+IntToStr(nodeNum); for i:=0 to nodeNum-1 do begin str:=str+#13#10'子节点'+IntToStr(i+1)+'的名称='+XmlRoot.ChildNodes.Nodes[i].NodeName; end; FMemo:=str; //读某一节点的值 NodePos:=XmlRoot.ChildNodes['hdr'].ChildNodes['sessionid']; FMemo:='hdr-->sessionid='+NodePos.NodeValue; //判断节点属性是否存在,并得到节点属性的值 NodePos:=XmlRoot.ChildNodes['slia']; if NodePos.HasAttribute('ver1') then FMemo:='slia节点的ver属性为: '+NodePos.Attributes['ver'] else FMemo:='节点属性不存在'; //判断节点内容是否存在,及得到值 NodePos:=XmlRoot.ChildNodes['hdr'].ChildNodes['sessionid']; if NodePos.IsTextElement then FMemo:='Text='+NodePos.Text else FMemo:='F'; //遍历某节点的属性名称及值 str:=''; NodePos:=XmlRoot.ChildNodes['slia'].ChildNodes['pos'].ChildNodes['msid']; nodeNum:=NodePos.AttributeNodes.Count; for i:=0 to nodeNum-1 do begin str:=str+' '+nodePos.AttributeNodes.Nodes[i].NodeName+'='+nodePos.AttributeNodes.Nodes[i].NodeValue; end; FMemo:=str; //根据属性值查找节点 noderef := myXml.DocumentElement.DOMNode as IXMLDOMNodeRef; FMemo:='属性ID为101的节点内容:'+noderef.GetXMLDOMNode.selectSingleNode('slia/pos/NewNode[@id=''102'']').text; // FMemo:='属性ID为101的节点内容:'+NodePos.NodeValue; end; //添加节点及属性 procedure TForm1.Button4Click(Sender: TObject); var filePath:String; begin //为某一节点添加子节点 XmlRoot:= myXml.DocumentElement.ChildNodes['slia'].ChildNodes['pos'].AddChild('NewNode33333'); //添加属性并赋初值 XmlRoot.Attributes['id']:='100'; XmlRoot.Text:='测试'; //添加完保存 filePath:=ExtractFilePath(Application.ExeName)+'MyXml.xml'; myXml.SaveToFile(filePath); end; //删除节点 procedure TForm1.Button5Click(Sender: TObject); var filePath:String; begin XmlRoot:= myXml.DocumentElement; NodePos:= XmlRoot.ChildNodes['slia'].ChildNodes['pos'].ChildNodes['NewNode33333']; NodePos.ParentNode.ChildNodes.Remove(NodePos); //删除完保存 filePath:=ExtractFilePath(Application.ExeName)+'MyXml.xml'; myXml.SaveToFile(filePath); end; end.