Delphi中使用XML一例
【打印文章】
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;
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
result := 'Provider=' + provider + ';Password=' + password +
';Persist Security Info=True;User ID=' + userid +
';Initial Catalog=' + initialcatalog +
';Data Source=' + DataSource;
end;
end.
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;
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
result := 'Provider=' + provider + ';Password=' + password +
';Persist Security Info=True;User ID=' + userid +
';Initial Catalog=' + initialcatalog +
';Data Source=' + DataSource;
end;
end.
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】