-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmmObjectToXML.pas
More file actions
189 lines (170 loc) · 4.43 KB
/
Copy pathmmObjectToXML.pas
File metadata and controls
189 lines (170 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
unit mmObjectToXML;
{$mode objfpc}{$H+}
interface
uses
SysUtils,
Classes,
TypInfo,
DOM,
XMLWrite,
XMLRead,
Variants;
type
TObjectToXML = class(TObject)
private
FDoc: TXMLDocument;
FObject: TObject;
FStream: TMemoryStream;
FXML: WideString;
procedure FormatXML;
function GetXML: WideString;
procedure WriteListItems(AList: TList);
procedure WriteObject(AProperty: string; AObject: TObject);
procedure WriteProperty(AProperty, AValue: string);
public
constructor Create(AObject: TObject);
destructor Destroy; override;
property Obj: TObject read FObject write FObject;
property XML: WideString read GetXML;
end;
{ TObjectToXML }
implementation
{
********************************* TObjectToXML *********************************
}
constructor TObjectToXML.Create(AObject: TObject);
begin
inherited Create;
FDoc:=TXMLDocument.Create;
FObject := AObject;
FStream := TMemoryStream.Create;
end;
destructor TObjectToXML.Destroy;
begin
FDoc.Free;
FStream.Free;
inherited;
end;
procedure TObjectToXML.FormatXML;
var
XSize: Cardinal;
XString: string;
begin
XString := UTF8Encode(FXML);
XSize := Length(XString);
FStream.Clear;
FStream.Write(XString[1], XSize * SizeOf(XString[1]));
FStream.Seek(0, soFromBeginning);
XSize := FStream.Size;
FStream.Seek(0, soFromBeginning);
ReadXMLFile(FDoc, FStream);
FDoc.XMLVersion := '1.0';
FStream.Seek(0, soFromBeginning);
WriteXML(FDoc.DocumentElement, FStream);
FStream.Seek(0, soFromBeginning);
FStream.Write(FXML, FStream.Size);
end;
function TObjectToXML.GetXML: WideString;
begin
FXML := '';
if (FObject <> nil) then
begin
WriteObject('Object', FObject);
FormatXML;
end;
Result := FXML;
end;
procedure TObjectToXML.WriteListItems(AList: TList);
var
X: Cardinal;
begin
FXML := UTF8Decode(Format('%s<%s>', [FXML, 'Items']));
if (AList.Count > 0) then
begin
for X := 0 to AList.Count - 1 do
begin
WriteObject('Item', TObject(AList[X]));
end;
end;
FXML := UTF8Decode(Format('%s</%s>', [FXML, 'Items']));
end;
procedure TObjectToXML.WriteObject(AProperty: string; AObject: TObject);
var
XCount: Integer;
XSize: Integer;
X: Integer;
XList: PPropList;
XPropInfo: PPropInfo;
XPropValue: string;
XObject: TObject;
begin
FXML := UTF8Decode(Format('%s<%s %s="%s">',
[FXML, AProperty, 'ClassName', AObject.ClassName]));
XCount := GetPropList(AObject.ClassInfo, tkAny, nil);
XSize := XCount * SizeOf(Pointer);
GetMem(XList, XSize);
try
XCount := GetPropList(AObject.ClassInfo, tkAny, XList);
if (XCount > 0) then
begin
for X := 0 to XCount - 1 do
begin
XPropInfo := XList^[X];
case XPropInfo^.PropType^.Kind of
tkClass:
begin
XObject := GetObjectProp(AObject, XPropInfo);
if (Assigned(XObject)) then
begin
WriteObject(XPropInfo^.Name, XObject);
end;
end;
tkArray:
XPropValue := GetPropValue(AObject, XPropInfo^.Name);
tkRecord:
XPropValue := GetPropValue(AObject, XPropInfo^.Name);
tkDynArray:
XPropValue := GetPropValue(AObject, XPropInfo^.Name);
else
begin
XPropValue := GetPropValue(AObject, XPropInfo^.Name);
WriteProperty(XPropInfo^.Name, XPropValue);
end
end;
end;
end;
{ if (AObject.InheritsFrom(TCollection)) then
begin
WriteCollectionItems(AObject as TCollection);
end; }
if (AObject.InheritsFrom(TList)) then
begin
WriteListItems(AObject as TList);
end;
finally
FreeMem(XList);
end;
FXML := UTF8Decode(Format('%s</%s>', [FXML, AProperty]));
end;
procedure TObjectToXML.WriteProperty(AProperty, AValue: string);
begin
FXML := UTF8Decode(Format('%s<%s>%s</%s>', [FXML, AProperty, AValue,
AProperty]));
end;
{
procedure TObjectToXML.WriteCollectionItems(ACollection: TCollection);
var
X: Cardinal;
begin
FXML := Format('%s<%s>', [FXML, 'Items']);
if (ACollection.Count > 0) then
begin
for X := 0 to ACollection.Count - 1 do
begin
WriteObject('Item', ACollection.Items[X]);
end;
end;
FXML := Format('%s</%s>', [FXML, 'Items']);
end;
}
end.