The DFM file is the stored form control information in Delphi. Sometimes it is often not too convenient to operate a large number of DFM files (such as quantity replacement controls, checking, etc.). XML is very strong to the structured data reading and writing, and the DFM of the text type is turned to XML, and then the DFM storage is greatly convenient.
// DFM to XML
Procedure DFM2XML (ADFMSTRINGS: TSTRINGS; AXML: IDSXMLDOCUMENT);
VAR
i: integer;
MSTR: STRING;
MparentNode, Mnode: IDSXMLNode;
MobjName, McLAS, MOBJTYPE: STRING
MPropName, MPropValue: String;
MinItem: boolean; // There will be END, so you have to separate with Object End.
Begin
MinItem: = false;
Mparentnode: = AXML;
For i: = 0 to adfmstrings.count - 1 do
Begin
MSTR: = Trim (Adfmstrings.Strings [i]);
// object start
IF (Copy (MSTR, 1, 6) = 'Object') OR (Copy (MSTR, 1, 9) = 'inherited') THEN
Begin
// write into attributes - if there is still attributes
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) .SetaTRBUTE ('Objtype', MobjType);
(Mnode as idsxmlelement) .SetaTRibute ('Class', McLASS);
MparentNode.Appendchild (Mnode);
Mparentnode: = mnode;
end
// The object ends
Else IF (MSTR = 'end') and (not minitym) THEN
Begin
// Prevent the attribute
IF mpropname <> '' THEN
Begin
Mnode: = axml.createElement (MPropName);
Mnode.text: = MPropValue;
MparentNode.Appendchild (Mnode);
MPropName: = '';
END;
Mparentnode: = mparentnode.parentNode;
end
// Property processing
Else
Begin
// = Description is a new start IF (POS ('=', mstr)> 0) and (not minitym) THEN
Begin
// Prevent the attribute
IF mpropname <> '' THEN
Begin
Mnode: = axml.createElement (MPropName);
Mnode.text: = MPropValue;
MparentNode.Appendchild (Mnode);
END;
MPropName: = CUTTOKEN (MSTR, '=');
MPropValue: = CUTTOKEN (MSTR, '=');
end
ELSE // Description This property may have multiple lines
MPropValue: = MPROPVALUE '# 13 # 10' mstr;
IF mstr = 'item' Then MinItem: = true;
IF mstr = 'end' Then minitym: = false;
END;
END;
END;
// XML to DFM
Procedure XML2DFM (AXML: IDSXMLDocument; Adfmstrings: Tstrings);
Procedure node2dfm (anode: idsxmlnode; ostring: tstrings; idession: string);
VAR
MobjName, McLAS, 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) .GetaTribute ('objtype');
// Write Object Begin
Ostring.add (Format ('% s% s:% s: [IdentSpace, MobjType, MobjName, McLASS]);
// Write properties
For i: = 0 to anode.childnodes.length - 1 do
Begin
Mnode: = anode.childnodes.Item [i];
// Has Child Description is a new object
MVAR: = (Mnode as idsxmlelement) .GetaTribute ('objtype');
IF not varisnull (mvar) THEN
Node2DFM (Mnode, Ostring, Identspace ')
// is the property, just write it.
Else
Begin
MPropname: = mnode.nodeename;
MPropValue: = mnode.text;
MPropValue: = StringReplace (MPropValue, '# 13 # 10', # 13 # 10, [RFREPLACEALL]);
// Write: indent
Ostring.add (Format ('% s% s =% s', [Identspace '', MPropName, MPropValue]);
END;
// Write END
Ostring.add (Format ('% send', [identspace]));
END;
Begin
Node2DFM (Axml.get_Documentelement, Adfmstrings, '');
END;
Used type:
Uses
MSXML2_TLB;
// Type yourself redefined
Type
Idsxmldocument = ixmldomdocument2;
IDSXMLNODE = IXMLDOMNODE;
Idsxmlelement = ixmldomelement;
Idsxmlnodelist = ixmldomnodelist;