Delphi操作XML

  Delphi操作XML是很方便的,主要有两种方法;
     1.用TClientDataSet操作XML;TClientDataSet是个好东西,用它操作XML是很简单的事,不过缺点是只能操作固定格式的 XML,它适合操作表结构的数据,如果你需要把数据表导出成XML那用TClientDataSet是个好主意,比如下面是一个数据集导出成XML的方 法:
procedure ExportToXML(SrcDataSet:TDataSet;const XMLFileName:String);
var tmpCds:TClientDataSet;
    i:integer;
    NewField:TFieldDef;
begin
SrcDataSet.DisableControls;
tmpCds:=TClientDataSet.Create(nil);
try
    for i:=0 to SrcDataSet.FieldCount-1 do
    begin
      NewField:=tmpCds.FieldDefs.AddFieldDef;
      NewField.Name:=SrcDataSet.Fields[i].FieldName;
      NewField.DataType:=SrcDataSet.fields[i].DataType;
      NewField.Size:=SrcDataSet.Fields[i].Size;
    end;
    tmpCds.CreateDataSet;
    if tmpCds.Active then tmpCds.LogChanges:=False;

    SrcDataSet.First;
    while not SrcDataSet.Eof do
    begin
      tmpCds.Append;
      for i:=0 to SrcDataSet.FieldCount-1 do
        tmpCds.FieldByName(SrcDataSet.Fields[i].FieldName).Value:=SrcDataSet.Fields[i].Value;
      tmpCds.Post;

      SrcDataSet.Next;
    end;
    tmpCds.SaveToFile(XMLFileName);
finally
    SrcDataSet.EnableControls;
    tmpCds.Free;
end;
end;


2.还有一种方法就是用TXMLDocument了,TXMLDocument很灵活,因此操作起来有点麻烦,特别是XML树很深的时候。不过 Delphi给我们提供了更方便的方法,使我们更加简单的操作XML,这个方法就是XML Data Binding向导,XML Data Binding向导会把XML的节点映射成对象,从而使我们更方便的操作它们。下面是一个XML Data Binding使用的例子。

     比如我有一个叫Config.xml的配置文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<Config>
<ProductName></ProductName>
<DB>
    <Connection Host="" DbName="" UserName="" PassWord=""/>
</DB>
</Config>

      以Delphi7为例,点new->Other->XML Data Binding,然后出现XML Data Binding向导对话框,选择自己的XML文件,点"Next"....,完成后就会生成一个代码单元,比如上面的XML就会生成这样的代码:


{***************************************}
{                                       }
{           XML Data Binding            }
{                                       }
{         Generated on: 2009-7-26 1:31:14 }
{       Generated from: D:/Config.xml   }
{                                       }
{***************************************}

unit Config;

interface

uses xmldom, XMLDoc, XMLIntf;

type

{ Forward Decls }

IXMLConfigType = interface;
IXMLDBType = interface;
IXMLConnectionType = interface;

{ IXMLConfigType }

IXMLConfigType = interface(IXMLNode)
    ['{F78E0752-5D0C-4350-A59C-7743CB844322}']
    { Property Accessors }
    function Get_ProductName: WideString;
    function Get_DB: IXMLDBType;
    procedure Set_ProductName(Value: WideString);
    { Methods & Properties }
    property ProductName: WideString read Get_ProductName write Set_ProductName;
    property DB: IXMLDBType read Get_DB;
end;

{ IXMLDBType }

IXMLDBType = interface(IXMLNode)
    ['{1CB67B0A-92B4-4B50-AB64-167605EA6789}']
    { Property Accessors }
    function Get_Connection: IXMLConnectionType;
    { Methods & Properties }
    property Connection: IXMLConnectionType read Get_Connection;
end;

{ IXMLConnectionType }

IXMLConnectionType = interface(IXMLNode)
    ['{6976B41B-28C5-407F-8D19-B6B6E153265F}']
    { Property Accessors }
    function Get_Host: WideString;
    function Get_DbName: WideString;
    function Get_UserName: WideString;
    function Get_PassWord: WideString;
    procedure Set_Host(Value: WideString);
    procedure Set_DbName(Value: WideString);
    procedure Set_UserName(Value: WideString);
    procedure Set_PassWord(Value: WideString);
    { Methods & Properties }
    property Host: WideString read Get_Host write Set_Host;
    property DbName: WideString read Get_DbName write Set_DbName;
    property UserName: WideString read Get_UserName write Set_UserName;
    property PassWord: WideString read Get_PassWord write Set_PassWord;
end;

{ Forward Decls }

TXMLConfigType = class;
TXMLDBType = class;
TXMLConnectionType = class;

{ TXMLConfigType }

TXMLConfigType = class(TXMLNode, IXMLConfigType)
protected
    { IXMLConfigType }
    function Get_ProductName: WideString;
    function Get_DB: IXMLDBType;
    procedure Set_ProductName(Value: WideString);
public
    procedure AfterConstruction; override;
end;

{ TXMLDBType }

TXMLDBType = class(TXMLNode, IXMLDBType)
protected
    { IXMLDBType }
    function Get_Connection: IXMLConnectionType;
public
    procedure AfterConstruction; override;
end;

{ TXMLConnectionType }

TXMLConnectionType = class(TXMLNode, IXMLConnectionType)
protected
    { IXMLConnectionType }
    function Get_Host: WideString;
    function Get_DbName: WideString;
    function Get_UserName: WideString;
    function Get_PassWord: WideString;
    procedure Set_Host(Value: WideString);
    procedure Set_DbName(Value: WideString);
    procedure Set_UserName(Value: WideString);
    procedure Set_PassWord(Value: WideString);
end;

{ Global Functions }

function GetConfig(Doc: IXMLDocument): IXMLConfigType;
function LoadConfig(const FileName: WideString): IXMLConfigType;
function NewConfig: IXMLConfigType;

const
TargetNamespace = '';

implementation

{ Global Functions }

function GetConfig(Doc: IXMLDocument): IXMLConfigType;
begin
Result := Doc.GetDocBinding('Config', TXMLConfigType, TargetNamespace) as IXMLConfigType;
end;

function LoadConfig(const FileName: WideString): IXMLConfigType;
begin
Result := LoadXMLDocument(FileName).GetDocBinding('Config', TXMLConfigType, TargetNamespace) as IXMLConfigType;
end;

function NewConfig: IXMLConfigType;
begin
Result := NewXMLDocument.GetDocBinding('Config', TXMLConfigType, TargetNamespace) as IXMLConfigType;
end;

{ TXMLConfigType }

procedure TXMLConfigType.AfterConstruction;
begin
RegisterChildNode('DB', TXMLDBType);
inherited;
end;

function TXMLConfigType.Get_ProductName: WideString;
begin
Result := ChildNodes['ProductName'].Text;
end;

procedure TXMLConfigType.Set_ProductName(Value: WideString);
begin
ChildNodes['ProductName'].NodeValue := Value;
end;

function TXMLConfigType.Get_DB: IXMLDBType;
begin
Result := ChildNodes['DB'] as IXMLDBType;
end;

{ TXMLDBType }

procedure TXMLDBType.AfterConstruction;
begin
RegisterChildNode('Connection', TXMLConnectionType);
inherited;
end;

function TXMLDBType.Get_Connection: IXMLConnectionType;
begin
Result := ChildNodes['Connection'] as IXMLConnectionType;
end;

{ TXMLConnectionType }

function TXMLConnectionType.Get_Host: WideString;
begin
    Result := AttributeNodes['Host'].Text;
end;

procedure TXMLConnectionType.Set_Host(Value: WideString);
begin
    SetAttribute('Host', Value);
end;

function TXMLConnectionType.Get_DbName: WideString;
begin
    Result := AttributeNodes['DbName'].Text;
end;

procedure TXMLConnectionType.Set_DbName(Value: WideString);
begin
    SetAttribute('DbName', Value);
end;

function TXMLConnectionType.Get_UserName: WideString;
begin
    Result := AttributeNodes['UserName'].Text;
end;

procedure TXMLConnectionType.Set_UserName(Value: WideString);
begin
    SetAttribute('UserName', Value);
end;

function TXMLConnectionType.Get_PassWord: WideString;
begin
    Result := AttributeNodes['PassWord'].Text;
end;

procedure TXMLConnectionType.Set_PassWord(Value: WideString);
begin
    SetAttribute('PassWord', Value);
end;

end.

 

    这个单元会生成三个GetXXX,LoadXXX,NewXXX的函数,用这几个函数就可以操作我们的XML了。例如:

uses XMLDoc,XMLIntf,Config;//Config是我上面XML Data Binding 生成的单元

var Config:IXMLConfigType;
    XMLDoc:IXMLDocument;
    XMLFileName:String;
begin
XMLFileName:=ExtractFilePath(ParamStr(0))+'Config.xml';
if FileExists(XMLFileName) then
    XMLDoc:=LoadXMLDocument(XMLFileName)
else begin
    XMLDoc:=NewXMLDocument;
    XMLDoc.Encoding:='UTF-8';
end;
Config:=GetConfig(XMLDoc);
//写
Config.ProductName:='软件名称';
Config.DB.Connection.Host:='127.0.0.1';
Config.DB.Connection.DbName:='test';
Config.DB.Connection.UserName:='sa';
Config.DB.Connection.PassWord:='123';
//读
showmessage(Config.ProductName);

XMLDoc.SaveToFile(XMLFileName);
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值