本站首页    管理页面    写新日志    退出


«September 2025»
123456
78910111213
14151617181920
21222324252627
282930


公告
================

注会练习软件
http://www.cpasoft.com.cn
我的注会软件官网

http://blog.163.com/abc7105@126/

 

 


哈哈,热爱快“过气”的DELPHI


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:注册会计师(注会)练习软件
日志总数:398
评论数量:116
留言数量:27
访问次数:3273483
建立时间:2005年6月6日




[delpih编程]delphi写的一个读写xml格式配置文件的帮助类 【转贴】
软件技术

吕向阳 发表于 2009/4/20 21:26:14

delphi写的一个读写xml格式配置文件的帮助类 unit uSystemParams; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, ExtCtrls, MSXML2_TLB, uUtility; type   TSystemParams = class   private     FXmlDocument: IXMLDOMDocument;     FXmlFileName : String;   protected   public     constructor Create(const xmlfilename : String); overload;virtual;     constructor Create; overload;virtual;     destructor Destroy; override;     procedure loadFromFile(const xmlfilename : String); virtual;     procedure saveToFile; overload;virtual;     procedure saveToFile(const xmlFileName : String); overload;virtual;     function getXmlDocument: IXMLDOMDocument; virtual;     function getElementText(const nodePath: string; const defaultValue : String = ''): string; virtual;     procedure setElementText(const nodepath, value: string); virtual;     function getElementTextAttribute(const nodePath, attrName : string; const defaultValue : String = '') : string;     procedure setElementTextAttribute(const nodePath, attrName, attrValue : string);     function getElementBooleanAttribute(const nodePath, attrName : String; const default : boolean = false) : boolean;     procedure setElementBooleanAttribute(const nodePath, attrName : String; const value : boolean);     function getElementIntAttribute(const nodePath, attrName : String; const default : Integer = -1) : integer;     procedure setElementIntegerAttribute(const nodePath, attrName : String; const value : integer);     function getElemenetDateTimeAttribute(const nodePath, attrName : String; const default : TDateTime = 0) : TDateTime;     procedure setElemenetDatetimeAttribute(const nodePath, attrName : String; const value : TDateTime);     procedure setElementBoolValue(const nodeName : String; value : boolean);     function getElementBoolValue(const nodeName : String; const defaultValue : boolean =false) : boolean;     procedure setElementIntValue(const nodeName : String; value : Integer);     function getElementIntValue(const nodeName : String; const defaultValue : integer = -1) : Integer;     procedure setElementDatetimeValue(const nodeName : String; value : TDateTime);     function getDatetimeValue(const nodeName : String; const defaultValue : TDateTime = 0) : TDateTime;     function loadXmlElement(const nodePath : string) : IXMLDOMElement; virtual;   end; implementation constructor TSystemParams.Create(const xmlfilename : String); begin   FXmlDocument := CoDOMDocument60.Create();   loadFromFile(xmlfilename); end; constructor TSystemParams.Create; begin   FXmlDocument := CoDOMDocument60.Create(); end; destructor TSystemParams.Destroy; begin   FXmlDocument := nil;   inherited; end; function TSystemParams.getElementBooleanAttribute(const nodePath, attrName: String; const default: boolean = false): boolean; begin   result := StrToBoolDef(getElementTextAttribute(nodePath, attrName, BoolToStr(default, true)), default); end; function TSystemParams.getElementBoolValue(const nodeName: String; const defaultValue : boolean =false): boolean; begin   Result := StrToBool(getElementText(nodeName, BoolToStr(defaultValue, defaultValue))); end; function TSystemParams.getDatetimeValue(const nodeName: String; const defaultValue : TDateTime = 0): TDateTime; begin   Result := StrToDateTime(getElementText(nodeName, DateTimeToStr(defaultValue))); end; function TSystemParams.getElementIntAttribute(const nodePath, attrName: String; const default: Integer = -1): integer; begin   Result := strToInt(getElementTextAttribute(nodePath, attrName, IntToStr(default))); end; function TSystemParams.getElementIntValue(const nodeName: String; const defaultValue : integer = -1): Integer; begin   Result := StrToInt(getElementText(nodeName, IntToStr(defaultValue))); end; function TSystemParams.getElemenetDateTimeAttribute(const nodePath, attrName: String; const default: TDateTime = 0): TDateTime; begin   Result := StrToDateTime(getElementTextAttribute(nodePath, attrName, datetoStr(default))); end; function TSystemParams.getElementTextAttribute(const nodePath, attrName: string; const defaultValue : String = ''): string; var   element : IXMLDOMElement;   node : IXMLDOMNode; begin   element := loadXmlElement(nodePath);   node := element.attributes.getNamedItem(attrName);   if node <> nil then     Result := node.text   else     Result := defaultValue; end; function TSystemParams.getElementText(const nodePath: string; const defaultValue : String = ''): string; var   node: IXMLDOMNode;   i: Integer; begin   result := '';   node := FXmlDocument.selectSingleNode(nodePath);   if assigned(node) then     Result := node.text   else     Result := defaultValue; end; function TSystemParams.getXmlDocument: IXMLDOMDocument; begin   result := FXmlDocument; end; function TSystemParams.loadXmlElement(const nodePath: string): IXMLDOMElement; var   slist : TStrings;   i : Integer;   parent, temp : IXMLDOMElement;   xName : string; begin   result := nil;   slist := TStringList.Create;   try     StrToKenToStrings(nodePath, '/', slist);     if (sList[0] ='/') or (sList[0] = '') then     begin       slist.Delete(0);     end;     parent := FXmlDocument.documentElement;     if (parent <> nil) and (parent.nodeName <> slist[0]) then       raise Exception.CreateFmt('已经有一个根元素%s了,不能再加一个不同的根%s', [parent.nodeName, slist[0]]);     for i := 0 to slist.Count - 1 do     begin       xName := xName + '/' + slist[i];       result := IXMLDOMElement(FXmlDocument.selectSingleNode(xName));       if result = nil then       begin         result := FXmlDocument.createElement(slist[i]);         if i = 0 then         begin           parent := Result;           FXmlDocument.appendChild(parent);         end         else         begin           parent.appendChild(result);         end;         parent := result;       end;     end;//i   finally     freeandnil(sList);   end;//finally end; procedure TSystemParams.loadFromFile(const xmlfilename : String); var   AHasDocument: Boolean; begin   FXmlFileName := xmlfilename;   if FileExists(FXmlFileName) then     FXmlDocument.load(FXmlFileName)   else     raise Exception.Create(FXmlFileName + ' 文件没有找到,加载xml失败');   FXmlDocument.createProcessingInstruction('xml','version="1.0" encoding="UTF-8"'); end; procedure TSystemParams.saveToFile(); begin   FXmlDocument.save(FXmlFileName); end; procedure TSystemParams.saveToFile(const xmlFileName: String); begin   FXmlDocument.save(xmlFileName); end; procedure TSystemParams.setElementBooleanAttribute(const nodePath, attrName: String; const value: boolean); begin   setElementTextAttribute(nodePath, attrName, boolToStr(value,  true)); end; procedure TSystemParams.setElementBoolValue(const nodeName: String; value: boolean); begin   setElementText(nodeName, BoolToStr(value, true)); end; procedure TSystemParams.setElementDatetimeValue(const nodeName: String; value: TDateTime); begin   setElementText(nodeName, DateTimeToStr(value)); end; procedure TSystemParams.setElementIntegerAttribute(const nodePath, attrName: String; const value: integer); begin   setElementTextAttribute(nodePath, attrName, IntToStr(value)); end; procedure TSystemParams.setElementIntValue(const nodeName: String; value: Integer); begin   setElementText(nodeName, IntToStr(value)); end; procedure TSystemParams.setElemenetDatetimeAttribute(const nodePath, attrName: String; const value: TDateTime); begin   setElementTextAttribute(nodePath, attrName, DateToStr(value)); end; procedure TSystemParams.setElementTextAttribute(const nodePath, attrName, attrValue: string); var   node : IXMLDOMElement;   attr : IXMLDOMAttribute; begin   node := IXMLDOMElement(FXmlDocument.selectSingleNode(nodePath));   if node = nil then     node := loadXmlElement(nodePath);   attr := FXmlDocument.createAttribute(attrName);   attr.text := attrValue;   node.attributes.setNamedItem(attr); end; procedure TSystemParams.setElementText(const nodepath, value: string); var   node, temp: IXMLDOMNode; begin   temp := FXmlDocument.selectSingleNode(nodepath);   if assigned(temp) then     temp.text := value   else   begin     temp := loadXmlElement(nodepath);     temp.text := value;   end; end; end. 写元素和属性的范例代码 procedure TForm1.Button1Click(Sender: TObject); var   sysParams : TSystemParams; begin   sysParams := TSystemParams.Create();   try     sysParams.setElementText('/config/title', '配置文件');     sysParams.setElementTextAttribute('/config/user', 'name', '张飞');     sysParams.setElementIntegerAttribute('/config/user', 'age', 22);     sysParams.setElementBooleanAttribute('/config/user', 'isadmin', true);     sysParams.setElemenetDatetimeAttribute('/config/user', 'date', Now);     mmLogs.Lines.Add(sysParams.getXmlDocument.xml);     sysParams.saveToFile('systemParams.xml');   finally     freeandnil(sysParams);   end;//finally end; 输出结果如下: <config><title>配置文件</title><user name="张飞" age="22" isadmin="True" date="2008-8-19"/></config>读元素和属性的范例代码 procedure TForm1.Button2Click(Sender: TObject); var   sysParams : TSystemParams;   sTemp : String;   bTemp : boolean;   iTemp : Integer;   dTemp : TDatetime; begin   sysParams := TSystemParams.Create('systemParams.xml');   try     sTemp := sysParams.getElementTextAttribute('/config/user', 'name');     bTemp := sysParams.getElementBooleanAttribute('/config/user', 'isadmin');     iTemp := sysParams.getElementIntAttribute('/config/user', 'age');     dTemp := sysParams.getElemenetDateTimeAttribute('/config/user', 'date');     mmLogs.lines.Add('title = ' + sysParams.getElementText('/config/title'));     mmLogs.Lines.Add('name = ' + ' = ' + sTemp);     mmLogs.Lines.Add('isadmin' + ' = ' + BoolToStr(bTemp, true));     mmLogs.Lines.Add('age' + ' = ' + IntToStr(iTemp));     mmLogs.Lines.Add('date' + ' = ' + DateTimeToStr(dTemp));   finally     freeandnil(sysParams);   end;//finally end; 输出结果如下: title = 配置文件name = = 张飞isadmin = Trueage = 22date = 2008-8-19 发贴


阅读全文(1413) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144786186 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号