Delphi直接读写XML修改版

来源:互联网 发布:win7修复linux引导 编辑:程序博客网 时间:2024/05/03 06:06
unit Unit3;    interface  uses SysUtils, Variants, Classes, Forms, Windows, msxmldom, XMLDoc, XMLIntf;  type    TOperateXml = class(Tobject)      public      function getnodefromIXMLNodeList(childnodes: IXMLNodeList; nodename:        string): IXMLNode;      function GetXMLNodeSpecialValue(strEntityEngineFile: string;        XMLNodePath: string;        const XMLAttrName: string = ''; const XMLSpecialName: string = ''; const        XMLSpecialValue: string = ''; const dep: Char = '.'): string;      function SetXMLNodeSpecialValue(strEntityEngineFile: string; xmlNodePath:        string; const xmlattrname: string = '';        const value: string = '';        const XMLSpecialName: string = '';        const XMLSpecialValue: string = '';        const dep: Char = '.'): boolean;      end;    var    OperateXml: TOperateXml;    implementation  { TOperateXml }    function TOperateXml.getnodefromIXMLNodeList(childnodes: IXMLNodeList; nodename: string):    IXMLNode;  var    i: Integer;  begin    for i := 1 to childnodes.Count do    begin      if (childnodes.Get(i - 1).NodeName = nodename) then      begin        result := childnodes[i - 1];        exit;      end;    end;  end;      {------------------------------------------------------------------------------- Fun/Pro:     GetXMLNodeSpecialValue @Date:     2004.12.11 @Param:   xmlFile xml文件 @Param:   xmlnodepath 节点 @Param:   xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。 @Param:   XMLSpecialName 要查找的节点中属性名 @Param:   XMLSpecialValue 要查找的节点中某属性对应的值 @Param:   dep 节点的参数的分隔符,默认为. @Return:     某属性的值 -------------------------------------------------------------------------------}    function TOperateXml.GetXMLNodeSpecialValue(strEntityEngineFile: string; XMLNodePath:    string;    const XMLAttrName: string = ''; const XMLSpecialName: string = ''; const    XMLSpecialValue: string = ''; const dep: Char = '.'): string;  var    xmlDocument: IXMLDocument;    node: IXMLNode;    xmlnodeList: TStrings;    i: Integer;    urlcount: Integer;  begin    //xml节点路径     xmlnodeList := TStringList.Create;    xmlnodeList.Delimiter := dep;    xmlnodeList.DelimitedText := xmlnodepath;    urlcount := xmlnodeList.Count;    //xml对象     xmlDocument := TXMLDocument.Create(nil);    xmlDocument.LoadFromFile(strEntityEngineFile);    xmlDocument.Active := true;    try      node := xmlDocument.DocumentElement;      if (node.NodeName = xmlnodeList[0]) then      begin        //扫描节点         for i := 1 to urlcount - 1 do        begin          if (node <> nil) then          begin            node := getnodefromIXMLNodeList(node.ChildNodes, xmlnodeList[i]);          end          else            Break;        end;        if (node = nil) then        begin          result := '';        end        else        begin          //判断是取属性还是取节点内容           if (Trim(xmlattrname) = '') then            result := node.Text          else          begin            result := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue;            //这里不想再声明一个临时变量了,就用result来比较,可能有隐患。             while ((result <> XMLSpecialValue)) do            begin              node := node.NextSibling;              while (node.NodeName = '#comment') do              begin                node := node.NextSibling;              end;              result := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue;            end;            result := node.AttributeNodes.Nodes[XMLAttrName].NodeValue;          end;        end;      end      else      begin        result := '';      end;      except      result := 'error';    end;    xmlDocument.Active := false;  end;      {------------------------------------------------------------------------------- Fun/Pro:     SetXMLNodeSpecialValue @Date:     2004.12.11 @Param:   xmlFile xml文件 @Param:   xmlnodepath 节点 @Param:   xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。 @Param:   XMLSpecialName 要查找的节点中属性名 @Param:   XMLSpecialValue 要查找的节点中某属性对应的值 @Param:   dep 节点的参数的分隔符,默认为. @Return:     操作成功与否 -------------------------------------------------------------------------------}    function TOperateXml.SetXMLNodeSpecialValue(strEntityEngineFile: string; xmlNodePath:    string;    const xmlattrname: string = ''; const value: string = ''; const    XMLSpecialName: string = ''; const XMLSpecialValue: string = ''; const dep:    Char = '.'): boolean;  var    xmlDocument: IXMLDocument;    node: IXMLNode;    xmlnodeList: TStrings;    i: Integer;    urlcount: Integer;    CMPValue: string;  begin    //xml节点路径     xmlnodeList := TStringList.Create;    xmlnodeList.Delimiter := dep;    xmlnodeList.DelimitedText := xmlnodepath;    urlcount := xmlnodeList.Count;    //xml对象     xmlDocument := TXMLDocument.Create(nil);    xmlDocument.LoadFromFile(strEntityEngineFile);    xmlDocument.Active := true;    try      node := xmlDocument.DocumentElement;      if (node.NodeName = xmlnodeList[0]) then      begin        //扫描节点         for i := 1 to urlcount - 1 do        begin          if (node <> nil) then            node := getnodefromIXMLNodeList(node.ChildNodes, xmlnodeList[i])          else            Break;        end;          if (node <> nil) then        begin          {if(Trim(xmlattrname)='') then           node.Text:=value         else           node.AttributeNodes.Nodes[xmlattrname].NodeValue:=value;         }          if (Trim(XMLAttrName) = '') then            node.Text := value          else          begin            CMPValue := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue;            while (CMPValue <> XMLSpecialValue) do            begin              node := node.NextSibling;              while (node.NodeName = '#comment') do              begin                node := node.NextSibling;              end;              CMPValue := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue;            end;            node.AttributeNodes.Nodes[XMLAttrName].NodeValue := value;          end;          xmlDocument.SaveToFile(strEntityEngineFile);        end;      end;      result := true;    except      result := false;    end;    xmlDocument.Active := false;  end;            end.  

原创粉丝点击