XML学习笔记(五)--Delphi读写xml例子

例子一:

form上加上XMLDocument控件namemXML

从文件读取xml文档:

mXML.LoadFromFile(FILENAME);

 

然后可以用mXml.DocumentElement[NODENAME]读取数据,

或用mXml.DocumentElement[NODENAME]:=...改变或添加数据,

 

最后,将文档保存到文件mXML.SaveToFile(FILENAME);

如果想在程序中直接生成xml文档,

可以参考下面的代码:

 

mXMl.xml:='<xml></xml>';//设置ROOTNAME;

mXMl.active:=true;

mXml.DocumentElement[NODENAME]:=....;//赋值

mXMl.SaveToFile(FILENAME);

 

以上的都是根下的node的值的获得和修改,对于Xml的树形结构,可以参考下面的代码:

 

xmlnode:=mXMl.DocumentElement.ChildNodes.FindNode(NODENAME); //获得nameNODENAMEnode

childnode:=xmlnode.ChildNodes.FindNode(CHILDNAME);  //获得xmlnodenameCHILDNAMEchildnode;

 

然后,TTreeView来显示XML文档的所有树结构:

 

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;

 

 

例子二:

unit   concfg;

 

interface

 

uses

Windows,   Messages,   SysUtils,   Classes,IcXMLStrings,   IcXMLParser;

 

Type

TConcfg   =   class

applicationname:   string;

provider:   string;

connectTimeout:   string;

datasource:   string;

initialCatalog:   string;

userid:   string;

version:   string;

password:   String;

function   GetConnectString:   string;

constructor   Create(AFilename:   string);

end;

 

implementation

 

constructor   TConcfg.Create(AFilename:   string);

var

text:   TIcXMLText;

ele:   TIcXMLElement;

par:   TIcXMLParser;

doc   :   TIcXMLDocument;

begin

par   :=   TIcXMLParser.Create(nil);

doc   :=   TIcXMLDocument.Create;

//doc.SetEncoding('UTF-8');

Try

par.Parse(AFilename,doc);

ele   :=   doc.GetDocumentElement;

version   :=   ele.GetAttribute('version');

ele   :=   ele.GetFirstChild;

while   ele   <>   nil   do

begin

if (ele.TagName   =   'ApplicationName')   then

if   ele.HasCharData   then

begin

text   :=   ele.GetFirstCharData;

applicationname   :=   trim(text.GetValue);

end;

if (ele.TagName   =   'Provider')   then

if   ele.HasCharData   then

begin

text   :=   ele.GetFirstCharData;

provider   :=   trim(text.GetValue);

end;

if (ele.TagName   =   'DataSource')   then

if   ele.HasCharData   then

begin

text   :=   ele.GetFirstCharData;

datasource   :=   trim(text.GetValue);

end;

if (ele.TagName   =   'InitialCatalog')   then

if   ele.HasCharData   then

begin

text   :=   ele.GetFirstCharData;

InitialCatalog   :=   trim(text.GetValue);

end;

if (ele.TagName   =   'UserID')   then

if   ele.HasCharData   then

begin

text   :=   ele.GetFirstCharData;

UserID   :=   trim(text.GetValue);

end;

if (ele.TagName   =   'Password')   then

if   ele.HasCharData   then

begin

text   :=   ele.GetFirstCharData;

Password   :=   trim(text.GetValue);

end;

ele   :=   ele.NextSibling;

end;   //end while

if   length(Trim(UserID))   =   0   then

UserID   :=   'sa';

if   length(Trim(Provider))   =   0   then

Provider   :=   'SQLOLEDB.1';

if   length(Trim(initialcatalog))   =   0   then

initialcatalog   :=   'hszxdata';

if   length(Trim(DataSource))   =   0   then

DataSource   :=   '192.168.0.3';

Finally

doc.clear;

doc.Free;

end;

end;

 

function   TConcfg.GetConnectString:   string;

begin

esult   :=   'Provider='   +   provider   +   ';Password='   +   password   +

';Persist   Security   Info=True;User   ID='   +   userid   +

';Initial   Catalog='   +   initialcatalog   +

';Data   Source='   +   DataSource;

end;

 

end.  

 

你可能感兴趣的:(xml,String,Security,Integer,文档,Constructor)