dfm文件是Delphi中存儲窗體控件信息的,有時為了對大批量的dfm文件操作(如:批量替換控件、作檢查等),往往不是太方便。
XML對結構化的數據讀寫則很強,將文本類型的dfm轉為XML後進行操作,再轉回dfm存儲大大方便了操作。
//DFM to XML procedure Dfm2Xml(ADfmStrings: TStrings; AXml: IDsXmlDocument); Var i: integer; mStr : string; mParentNode, mNode : IDsXMLNode; mObjName, mClass, mObjType: string; mPropName, mPropValue: string; mInItem: Boolean; //有Item的也會有End相對,所以要和Object end分開 begin mInItem := False; mParentNode := AXML; for i:=0 to ADfmStrings.Count - 1 do begin mStr := trim(ADfmStrings.Strings[i]); //對象開始 if (copy(mStr, 1, 6) = 'object') or (copy(mStr, 1, 9) = 'inherited') then begin //先寫入屬性 - 如果還有屬性沒有寫 if mPropName <> '' then begin mNode := AXml.createElement(mPropName); mNode.text := mPropValue; mParentNode.appendChild(mNode); mPropName := ''; end; mObjType := CutToken(mStr, ' '); //Type: object or inherited mObjName := CutToken(mStr, ': '); //ObjName: Ex. frmCmsI03 mClass := mStr; //ClassName: Ex. TButton mNode := AXml.createElement(mObjName); (mNode as IDsXmlElement).setAttribute('ObjType', mObjType); (mNode as IDsXmlElement).setAttribute('Class', mClass); mParentNode.appendChild(mNode); mParentNode := mNode; end //對象結束 else if (mStr = 'end') and (not mInItem) then begin //先寫入屬性 if mPropName <> '' then begin mNode := AXml.createElement(mPropName); mNode.text := mPropValue; mParentNode.appendChild(mNode); mPropName := ''; end; mParentNode := mParentNode.parentNode; end //屬性處理 else begin //有 = 說明是一個屬性的新的開始 if (Pos(' = ', mStr) > 0) and (not mInItem) then begin //先寫入屬性 if mPropName <> '' then begin mNode := AXml.createElement(mPropName); mNode.text := mPropValue; mParentNode.appendChild(mNode); end; mPropName := CutToken(mStr, ' = '); mPropValue := CutToken(mStr, ' = '); end else //說明這個屬性可能有多行 mPropValue := mPropValue + '#13#10' + mStr; if mStr = 'item' then mInItem := True; if mStr = 'end' then mInItem := False; end; end; end; //XML to DFM procedure Xml2Dfm(AXml: IDsXmlDocument; ADfmStrings: TStrings); procedure Node2Dfm(ANode: IDsXMLNode; oString: TStrings; IdentSpace: string); Var mObjName, mClass, mObjType: string; mPropName, mPropValue: string; mNode : IDsXMLNode; i: integer; mVar : Variant; begin mObjName := (ANode as IDsXmlElement).NodeName; mClass := (ANode as IDsXmlElement).getAttribute('Class'); mObjType := (ANode as IDsXmlElement).getAttribute('ObjType'); //寫入Object begin oString.Add(Format('%s%s %s: %s', [IdentSpace, mObjType, mObjName, mClass])); //寫入屬性 for i:= 0 to ANode.childNodes.length - 1 do begin mNode := ANode.childNodes.item[i]; //Has Child 說明是一個新的對象 mVar := (mNode as IDsXmlElement).getAttribute('ObjType'); if not varIsNull(mVar) then Node2Dfm(mNode, oString, IdentSpace + ' ') //是屬性,寫進去就好了. else begin mPropName := mNode.nodeName; mPropValue := mNode.text; mPropValue := StringReplace(mPropValue, '#13#10', #13#10, [rfReplaceAll]); //寫入:縮進 oString.Add(Format('%s%s = %s', [IdentSpace+' ', mPropName, mPropValue])); end; end; //寫入End oString.Add(Format('%send', [IdentSpace])); end; begin Node2Dfm(AXML.Get_documentElement, ADfmStrings, ''); end;
用到的類型:
uses MSXML2_TLB; //類型自己重新定義過 type IDsXmlDocument = IXMLDOMDocument2; IDsXmlNode = IXMLDOMNode; IDsXmlElement = IXMLDOMElement; IDsXmlNodeList = IXMLDOMNodeList;