请贴一个Delphi 6关于XML应用的例子!!! 急急,快帮帮忙啊!XML....怎么做,随便一个小程序... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我转载一篇:Delphi6 中XML 文档的应用 XML文档是新一代的Web数据格式。它可以用树的形式储存一切数据。下面介绍一下TXMLDocument控件的一些用法:已定义:XMLDoc: TXMLDocument;EncoderMIME: TIdEncoderMIME;DecoderMIME: TIdDecoderMIME;xn:IXMLNode;f:TFileStream;EncoderMIME和DecoderMIME是Indy控件中的Mime工具,用来把所有类型的字符串转换为Mime(Base64)的ASCII码,你用其他的编码方式也可以的,比如:UUE,XXE。XN是XML文档的树结点。F是文件流。首先,加载XML文档 xmldoc.LoadFromFile(‘XML文档的文件名'); xmldoc.Active:=true; //使XML文档变为活动再次,定位Xn在XML文档中的位置(假设是的二层的第四个节点) xn:=xmldoc.Node; xn:=xn.ChildNodes.Nodes[1]; //节点数是从零开始的,所以要减一 xn:=xn.ChildNodes.Nodes[3];接着,增加下一级节点 xn:=xn.AddChild(‘新的节点名字’) xn.Text:=’节点的文本内容’ xn.Attributes['节点的第一个属性']:=’属性值’xn.Attributes['节点的第二个属性']:=’属性值’ 修改节点,(节点的名字是不能改的) xn.Text:=’新的文本内容’ xn.Attributes['节点的第二个属性']:=’ 新的属性值’ 删除节点Xn xn.ParentNode.ChildNodes.Delete(xn.ParentNode.ChildNodes.IndexOf(xn));利用Xn.Text存放文件, f:=TFileStream.Create('要存放的文件名',fmOpenRead); xn.Text:=EncoderMime.Encode(f); //转换格式,文件编码 f.Free;解出Xn.Text中的文件, f:=TFileStream.Create(‘目标文件名’,fmCreate); DecoderMime.DecodeToStream(xn.Text,f); f.Free;最好利用Xn.attribute来增加一个属性来表示文件的编码方式,比如 xn.Attributes['Type']='Base64'这样改上面的两个片段为: xn.Attributes['Type']:='Base64'; f:=TFileStream.Create('要存放的文件名',fmOpenRead); xn.Text:=EncoderMime.Encode(f); f.Free;if xn.IsTextElement //Xn中存在Text then if xn.HasAttribute('Type') then //Xn中存在属性’Type’ if xn.Attributes['Type']='Base64' then begin f:=TFileStream.Create(‘目标文件名’,fmCreate); DecoderMime.DecodeToStream(xn.Text,f); f.Free; end;最后给出一个利用TTreeView来显示XML文档的所有树结构XMLTree是TTreeView类procedure TForm1.ReadXMLTree(RootTreeNode:TTreeNode; XMLTreeNode:IXMLNode);var i:integer; c:TTreeNode;begin for i:=0 to XMLTreeNode.ChildNodes.Count-1 do begin c:=xmltree.Items.AddChild(RootTreeNode,XMLTreeNode.ChildNodes.Nodes[i].NodeName); ReadXMLTree(c,XMLTreeNode.ChildNodes.Nodes[i]); end;end;使用方法 ReadXMLTree(nil,xmldoc.Node);当然要先加载XML文档 J ^_^-------------------------------------------Power by Liu Yang 2002-2-8My email: [email protected] page: http://lyhome.3322.net一个利用XML的例子:LY FTP Explorer中的Site Manager文件可以从我的主页下载。 实战Delphi6/Kylix2 SOAP/Web Service 程序设计篇附书源码,自己下载:http://delphi.mychangshu.com/dispdoc.asp?id=723 打开XML:procedure TFormMain.ButtonOpenXMLClick(Sender: TObject);begin if OpenDialog1.Execute then begin XMLDocument1.LoadFromFile(OpenDialog1.FileName); IsOpenXml := True; end;end;--------------------------------------------------------对XML的插入操作:procedure TFormMain.ButtonAddClick(Sender: TObject);var DocElementName,NewNodeName,NewNodeText: String; ValueNode: IXMLNode;begin if IsOpenXml = False then begin Application.MessageBox('请先打开XML文件','提示',MB_ICONINFORMATION + MB_OK); Exit; end else begin XmlDocument1.Active := True; DocElementName := Edit2.Text; NewNodeName := Edit3.Text; NewNodeText := Edit4.Text; XmlTreeNode:=XMLDocument1.Node; XmlTreeNode := XMLDocument1.DocumentElement.AddChild(DocElementName); Try ValueNode := XmlTreeNode.AddChild(NewNodeName); ValueNode.Text := NewNodeText; XMLDocument1.SaveToFile(OpenDialog1.FileName); Application.MessageBox('插入成功!','提示',MB_ICONINFORMATION + MB_OK); except end; end;end;--------------------------------------------------------------对XML文件中ROWDATA进行删除操作:procedure TFormMain.ButtonDeleteClick(Sender: TObject);begin if IsOpenXml = False then begin Application.MessageBox('请先打开XML文件','提示',MB_ICONINFORMATION + MB_OK); Exit; end else begin try XmlDocument1.Active := True; XmlTreeNode.ParentNode.ChildNodes.Delete(XmlTreeNode.ParentNode.ChildNodes.IndexOf(XmlTreeNode)); XMLDocument1.SaveToFile(OpenDialog1.FileName); Application.MessageBox('删除成功!','提示',MB_ICONINFORMATION + MB_OK); except Application.MessageBox('请先进行插入操作!','提示',MB_ICONINFORMATION + MB_OK); end; end;end; DLL 参数为BYTE数组时,在DELPHI怎么调用 webservices 的发布问题! 谁有或者谁知道哪有这样的控件下载? 控件中的只读IPictureDisp*属性导致控件无法在delphi下实例化,内祥! 有方便同时发给多个接收者的smtp发邮件的控件么? 求DELPHI的编译器函数大全 怎样读取RES中的SWF文件,然后用Flash控件播放,请指点! 有关DBGrid的问题! 关于ADO的问题!!! 关于c/s开发中的一个难题,保证送100分 如何使数据表中的特殊符号去除? 有会编Delphi控件之高手请进¬ *_*,100分!!
http://delphi.mychangshu.com/dispdoc.asp?id=723
procedure TFormMain.ButtonOpenXMLClick(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
XMLDocument1.LoadFromFile(OpenDialog1.FileName);
IsOpenXml := True;
end;
end;
--------------------------------------------------------
对XML的插入操作:
procedure TFormMain.ButtonAddClick(Sender: TObject);
var
DocElementName,NewNodeName,NewNodeText: String;
ValueNode: IXMLNode;
begin
if IsOpenXml = False then
begin
Application.MessageBox('请先打开XML文件','提示',MB_ICONINFORMATION + MB_OK);
Exit;
end
else
begin
XmlDocument1.Active := True;
DocElementName := Edit2.Text;
NewNodeName := Edit3.Text;
NewNodeText := Edit4.Text;
XmlTreeNode:=XMLDocument1.Node;
XmlTreeNode := XMLDocument1.DocumentElement.AddChild(DocElementName);
Try
ValueNode := XmlTreeNode.AddChild(NewNodeName);
ValueNode.Text := NewNodeText;
XMLDocument1.SaveToFile(OpenDialog1.FileName);
Application.MessageBox('插入成功!','提示',MB_ICONINFORMATION + MB_OK);
except
end;
end;
end;
--------------------------------------------------------------
对XML文件中ROWDATA进行删除操作:
procedure TFormMain.ButtonDeleteClick(Sender: TObject);
begin
if IsOpenXml = False then
begin
Application.MessageBox('请先打开XML文件','提示',MB_ICONINFORMATION + MB_OK);
Exit;
end
else
begin
try
XmlDocument1.Active := True;
XmlTreeNode.ParentNode.ChildNodes.Delete(XmlTreeNode.ParentNode.ChildNodes.IndexOf(XmlTreeNode));
XMLDocument1.SaveToFile(OpenDialog1.FileName);
Application.MessageBox('删除成功!','提示',MB_ICONINFORMATION + MB_OK);
except
Application.MessageBox('请先进行插入操作!','提示',MB_ICONINFORMATION + MB_OK);
end;
end;
end;