XML文件读写示例

写XML文件:

 1 var
 2   IXMLDoc : IXMLDocument;
 3   XMLNode, xNodeItem: IXMLNode;
 4 begin
 5   IXMLDoc := NewXMLDocument();
 6   try
 7     XMLNode := IXMLDoc.AddChild('ConfigInfo');
 8 
 9     xNodeItem := XMLNode.AddChild('VideoDB');
10     xNodeItem.Attributes['Host'] := VideoSQLConfig.strAddress;
11     xNodeItem.Attributes['UserName'] := VideoSQLConfig.strUserName;
12     xNodeItem.Attributes['PassWord'] := VideoSQLConfig.strPassWord;
13     xNodeItem.Attributes['DBName'] := VideoSQLConfig.strDBName;
14       
15 
16     IXMLDoc.SaveToFile(strFileName);
17   finally
18     IXMLDoc := nil;
19   end;
20 end;

读取XML文件:

var
  IXMLDoc:IXMLDocument;
  xNodeRoot,xNodeItem:IXMLNode;
  nIndex: Integer;
  strNodeName: string;
begin
  if not FileExists(strFileName) then Exit;
  IXMLDoc := NewXMLDocument();
  try
    IXMLDoc.LoadFromFile(strFileName);
    xNodeRoot := IXMLDoc.ChildNodes.Nodes['ConfigInfo'];
    for nIndex := 0 to xNodeRoot.ChildNodes.Count - 1 do
    begin
      xNodeItem := xNodeRoot.ChildNodes[nIndex];
      strNodeName := UpperCase(xNodeItem.NodeName);
      if strNodeName = UpperCase('VideoDB') then
      begin   
        VideoSQLConfig.strAddress := xNodeItem.Attributes['Host'];
        VideoSQLConfig.strUserName := xNodeItem.Attributes['UserName'];
        VideoSQLConfig.strPassWord := xNodeItem.Attributes['PassWord'];
        VideoSQLConfig.strDBName := xNodeItem.Attributes['DBName'];
        Continue;
      end;   
    end;     
  finally
    IXMLDoc:=nil;
  end

var  IXMLDoc : IXMLDocument;  XMLNode, xNodeItem: IXMLNode;begin  IXMLDoc := NewXMLDocument();  try    XMLNode := IXMLDoc.AddChild('ConfigInfo');
    xNodeItem := XMLNode.AddChild('VideoDB');    xNodeItem.Attributes['Host'] := VideoSQLConfig.strAddress;    xNodeItem.Attributes['UserName'] := VideoSQLConfig.strUserName;    xNodeItem.Attributes['PassWord'] := VideoSQLConfig.strPassWord;    xNodeItem.Attributes['DBName'] := VideoSQLConfig.strDBName;
    xNodeItem := XMLNode.AddChild('VideoFTP');    xNodeItem.Attributes['Host'] := VideoFtpConfig.strHost;    xNodeItem.Attributes['UserName'] := VideoFtpConfig.strUserName;    xNodeItem.Attributes['PassWord'] := VideoFtpConfig.strPassWord;    xNodeItem.Attributes['Dir'] := VideoFtpConfig.strDir;    xNodeItem.Attributes['Port'] := IntToStr(VideoFtpConfig.nPort);    if VideoFtpConfig.bPassive then           xNodeItem.Attributes['Passive'] := '1'    else      xNodeItem.Attributes['Passive']  := '0';          xNodeItem := XMLNode.AddChild('RecordFTP');    xNodeItem.Attributes['Host'] := RecordFTPConfig.strHost;    xNodeItem.Attributes['UserName'] := RecordFTPConfig.strUserName;    xNodeItem.Attributes['PassWord'] := RecordFTPConfig.strPassWord;    xNodeItem.Attributes['Dir'] := RecordFTPConfig.strDir;    xNodeItem.Attributes['Port'] := IntToStr(RecordFTPConfig.nPort);    if RecordFTPConfig.bPassive then      xNodeItem.Attributes['Passive'] := '1'    else      xNodeItem.Attributes['Passive']  := '0';
    xNodeItem := XMLNode.AddChild('VideoServer');    xNodeItem.Attributes['ServerUrl'] := VideoServerUrl;
    xNodeItem := XMLNode.AddChild('AudioServer');    xNodeItem.Attributes['ServerUrl'] := AudioServerUrl;
    xNodeItem := XMLNode.AddChild('LKJ');      xNodeItem.Attributes['LKJType'] := IntToStr(Ord(LKJType));
    xNodeItem.Attributes['Host'] := LKJFTPConfig.strHost;    xNodeItem.Attributes['UserName'] := LKJFTPConfig.strUserName;    xNodeItem.Attributes['PassWord'] := LKJFTPConfig.strPassWord;    xNodeItem.Attributes['Dir'] := LKJFTPConfig.strDir;    if LKJFTPConfig.bPassive then           xNodeItem.Attributes['Passive'] := '1'    else      xNodeItem.Attributes['Passive']  := '0';        xNodeItem.Attributes['SharePath'] := LKJSharePath;
    xNodeItem := XMLNode.AddChild('LKJSQL');    xNodeItem.Attributes['Host'] := LKJSQLConfig.strAddress;    xNodeItem.Attributes['UserName'] := LKJSQLConfig.strUserName;    xNodeItem.Attributes['PassWord'] := LKJSQLConfig.strPassWord;    xNodeItem.Attributes['DBName'] := LKJSQLConfig.strDBName;
    xNodeItem := XMLNode.AddChild('LKJOracle');    xNodeItem.Attributes['SvrName'] := LKJOracleConfig.SvrName;    xNodeItem.Attributes['User'] := LKJOracleConfig.User;    xNodeItem.Attributes['PWD'] := LKJOracleConfig.PWD;                IXMLDoc.SaveToFile(strFileName);  finally    IXMLDoc := nil;  end;

猜你喜欢

转载自www.cnblogs.com/tsolarboy/p/9854567.html
今日推荐