xml和delhi的结合在数据库连接上的应用和数据传输等方面有着很重要的作用!unit concfg;interfaceuses 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;implementationconstructor 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; 刚刚看到的。
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;implementationconstructor 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;
刚刚看到的。
unit test;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, StdCtrls, msxmldom, XMLDoc, Grids;type
TForm1 = class(TForm)
add: TButton;
XMLDocument1: TXMLDocument;
Memo1: TMemo;
view: TButton;
addchild: TButton;
order: TButton;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Edit3: TEdit;
StringGrid1: TStringGrid;
Button2: TButton;
procedure addClick(Sender: TObject);
procedure viewClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure addchildClick(Sender: TObject);
procedure orderClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
var
current:Integer;procedure TForm1.addClick(Sender: TObject);
var Clinic: IXMLNode;
begin
current := 0;
Clinic := XMLDocument1.DocumentElement.ChildNodes[current];
Memo1.Lines.Clear;
Clinic.ChildNodes['Title'].Text := 'HOT: ' + Clinic.ChildNodes['Title'].Text;
Memo1.Lines.Add(Clinic.ChildNodes['Title'].Text);
Memo1.Lines.Add(Clinic.ChildNodes['Date'].Text);
Memo1.Lines.Add(Clinic.ChildNodes['Topics'].Text);
end;
procedure TForm1.viewClick(Sender: TObject);
var Clinic: IXMLNode;beginInc(current);
try
Clinic := XMLDocument1.DocumentElement.ChildNodes[current];
Memo1.Lines.Clear;
Memo1.Lines.Add(Clinic.ChildNodes['Title'].Text);
Memo1.Lines.Add(Clinic.ChildNodes['Date'].Text);
Memo1.Lines.Add(Clinic.ChildNodes['Topics'].Text);
except
on E: Exception do
Memo1.Lines.Add(E.Message)
end
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
current:=-1;
end;procedure TForm1.addchildClick(Sender: TObject);
var Clinic: IXMLNode;
begin
Clinic := XMLDocument1.DocumentElement.AddChild('Clinic');
Clinic.ChildNodes['Title'].Text := 'Title';
Clinic.ChildNodes['Date'].Text := 'Date';
Clinic.ChildNodes['Topics'].Text := 'Topics';
end;procedure TForm1.orderClick(Sender: TObject);
var
ValueNode: IXMLNode;
XmlTreeNode: IXMLNode;
begin
//XMLDocument1.LoadFromFile('Test.xml');
//XMLDocument1.Active := True;
XMLDocument1.Create('test.xml');
// XMLDocument1.s
XmlTreeNode:=XMLDocument1.Node;
XmlTreeNode := XMLDocument1.DocumentElement.AddChild('order');
ValueNode := XmlTreeNode.AddChild('orderID');
ValueNode.Text := Edit1.Text;
ValueNode := XmlTreeNode.AddChild('orderName');
ValueNode.Text := Edit1.Text;
ValueNode := XmlTreeNode.AddChild('DigitalSign');
ValueNode.Text := Edit2.Text;
ValueNode := XmlTreeNode.AddChild('ApplyId');
ValueNode.Text := Edit2.Text;
// XMLDocument1.Node.
XMLDocument1.SaveToFile('Test.xml'); XmlTreeNode.ParentNode.ChildNodes.Delete(XmlTreeNode.ParentNode.ChildNodes.IndexOf(XmlTreeNode));
XMLDocument1.SaveToFile('Test2.xml');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
ChildCount: integer;
xn: IXMLNode;//XMLÎĵµµÄÊ÷½áµã
xnOrder: IXMLNode;
begin
// try
XMLDocument1.LoadFromFile('test.xml');
ChildCount := XMLDocument1.DocumentElement.ChildNodes.Count;
//¼ÆËã×Ó½Úµã¸öÊý
for i := 0 to ChildCount - 1 do
begin
xn := XMLDocument1.DocumentElement.ChildNodes[i];
//¼ÙÉèx.xmlÖÐÓÐOrderId¡¢orderName¡¢ApplyIdÈý¸ö×ӽڵ㡣
//ÏÂÃæµÄÓï¾äΪȡ¸Ã×Ó½ÚµãµÄÄÚÈÝ...
Edit1.text := xn.ChildNodes['OrderId'].Text;
Edit2.text := xn.ChildNodes['orderName'].Text;
Edit3.text := xn.ChildNodes['ApplyId'].Text;
end;
for i:=0 to ChildCount - 1 do
begin
xn := XMLDocument1.DocumentElement.ChildNodes[i];
StringGrid1.Cols[0].Add(xn.ChildNodes['bianhao'].Text);
StringGrid1.Cols[1].Add(xn.ChildNodes['name'].Text);
StringGrid1.RowCount := StringGrid1.RowCount + 1;
end;
end;procedure TForm1.Button2Click(Sender: TObject);
var
NewStock: IXMLNode;
ValueNode: IXMLNode;
begin
NewStock := XMLDocument1.DocumentElement.AddChild('stock');
NewStock.Attributes['exchange'] := 'NASDAQ';
ValueNode := NewStock.AddChild('name');
ValueNode.Text := 'Cisco Systems';
ValueNode:= NewStock.AddChild('price');
ValueNode.Text := '62.375';
ValueNode := NewStock.AddChild('symbol');
ValueNode.Text := 'CSCO';
ValueNode := NewStock.AddChild('shares');
ValueNode.Text := '25';
XMLDocument1.SaveToFile('Test2.xml');
end;end.
要用IXMLNode应该要装哪个东西啊