Menu

[r3166]: / trunk / Src / UXMLDocumentEx.pas  Maximize  Restore  History

Download this file

454 lines (399 with data), 16.3 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2008-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements extensions to TXMLDocument and IXMLDocument that provide some
* helper methods. Also provides a simple list object that can contain XML
* nodes with enumerator.
}
unit UXMLDocumentEx;
interface
uses
// Delphi
SysUtils,
// Project
Generics.Collections, XMLIntf, XMLDoc, XMLDom;
type
/// <summary>
/// Interface to a simple list of XML nodes. This interface is designed to
/// enable creation and manipulation of a list of IXMLNode objects without
/// reference to XML document..
/// </summary>
IXMLSimpleNodeList = interface(IInterface)
['{93ABE85F-FBAC-4FDA-B071-5DD1C8537844}']
/// <summary>Retrieves a node from the list by index.</summary>
function GetItem(Idx: Integer): IXMLNode;
/// <summary>Adds a given node to the list and returns index of new node
/// in list.</summary>
function Add(const Node: IXMLNode): Integer;
/// <summary>Returns number of items in list.</summary>
function Count: Integer;
/// <summary>Creates list enumerator.</summary>
/// <remarks>This method is mainly for use by the compiler and permits the
/// list to be used in for..in loops.</remarks>
function GetEnumerator: TEnumerator<IXMLNode>;
/// <summary>Indexed access to list items.</summary>
property Items[Idx: Integer]: IXMLNode read GetItem; default;
end;
type
/// <summary>
/// Extension of XMLIntf.IXMLDocument.
/// </summary>
/// <remarks>
/// Provides some methods that help with searching for nodes in the document,
/// creation of new nodes and loading and saving documents from and to byte
/// arrays.
/// </remarks>
IXMLDocumentEx = interface(IXMLDocument)
['{2BE340ED-DD60-4DC4-B3F7-EF9C1C46DA6D}']
/// <summary>Finds a node specified by a path from, and including the
/// document's root node.</summary>
/// <param name="PathToNode">DOMString [in] Path to node from root node.
/// </param>
/// <returns>IXMLNode - Reference to required node or nil if node not
/// found.</returns>
/// <remarks>Node in path are separated by backslash, e.g.
/// root\node1\node2</remarks>
function FindNode(PathToNode: DOMString): IXMLNode;
/// <summary>Finds all child nodes of a parent node that are elements and
/// have a specified name.</summary>
/// <param name="ParentNode">IXMLNode [in] Node containing child nodes to
/// be found.</param>
/// <param name="NodeName">DOMString [in] Name of required element nodes.
/// </param>
/// <returns>IXMLSimpleNodeList - List of matching nodes.</returns>
function FindChildNodes(const ParentNode: IXMLNode;
const NodeName: DOMString): IXMLSimpleNodeList;
overload;
/// <summary>Finds all child nodes of a parent node that are elements, have
/// a specified name and have a specified attribute value.</summary>
/// <param name="ParentNode">IXMLNode [in] Node containing child nodes to
/// be found.</param>
/// <param name="NodeName">DOMString [in] Name of required element nodes.
/// </param>
/// <param name="AttribName">DOMString [in] Name of required attribute.
/// </param>
/// <param name="AttribValue">OleVariant [in] Required value of AttribName.
/// </param>
/// <returns>IXMLSimpleNodeList - List of matching nodes.</returns>
function FindChildNodes(const ParentNode: IXMLNode;
const NodeName, AttribName: DOMString;
const AttribValue: OleVariant): IXMLSimpleNodeList;
overload;
/// <summary>Finds first child node that has a given name.</summary>
/// <param name="ParentNode">IXMLNode [in] Node containing child nodes to
/// be searched.</param>
/// <param name="NodeName">DOMString [in] Name of required element node.
/// </param>
/// <returns>IXMLNode - Reference to found node or nil if no node found.
/// </returns>
function FindFirstChildNode(const ParentNode: IXMLNode;
const NodeName: DOMString): IXMLNode;
overload;
/// <summary>Finds first child node that has a given name and attribute
/// value.</summary>
/// <param name="ParentNode">IXMLNode [in] Node containing child nodes to
/// be found.</param>
/// <param name="NodeName">DOMString [in] Name of required element node.
/// </param>
/// <param name="AttribName">DOMString [in] Name of required attribute.
/// </param>
/// <param name="AttribValue">OleVariant [in] Required value of AttribName.
/// </param>
/// <returns>IXMLNode - Reference to found node or nil if no node found.
/// </returns>
function FindFirstChildNode(const ParentNode: IXMLNode;
const NodeName, AttribName: DOMString;
const AttribValue: OleVariant): IXMLNode;
overload;
/// <summary>Creates a new parented XML node in document.</summary>
/// <param name="Parent">IXMLNode [in] Parent node of new node.</param>
/// <param name="NodeName">DOMString [in] Name of new node.</param>
/// <returns>IXMLNode - Requied new node.</returns>
function CreateElement(const Parent: IXMLNode;
const NodeName: DOMString): IXMLNode; overload;
/// <summary>Creates a new parented XML text node in document.</summary>
/// <param name="Parent">IXMLNode [in] Parent node of new node.</param>
/// <param name="NodeName">DOMString [in] Name of new node.</param>
/// <param name="Text">DOMString [in] Text stored in new node.</param>
/// <returns>IXMLNode - Requied new node.</returns>
function CreateElement(const Parent: IXMLNode;
const NodeName, Text: DOMString): IXMLNode; overload;
/// <summary>Loads XML document from data stored in byte array.</summary>
/// <param name="Bytes">TBytes [in] Array of bytes to load.</param>
procedure LoadFromBytes(const Bytes: TBytes);
/// <summary>Stores content of XML document in byte array using encoding
/// specified in inherited Encoding property.</summary>
/// <returns>TBytes - Array of bytes storing XML document.</returns>
function SaveToBytes: TBytes;
end;
type
/// <summary>
/// Implementation of a simple list of XML nodes that can be created and
/// manipulated without reference to XML document.
/// </summary>
TXMLSimpleNodeList = class(TInterfacedObject,
IXMLSimpleNodeList
)
strict private
var
/// <summary>List of nodes.</summary>
fList: TList<IXMLNode>;
public
/// <summary>Object constructor. Creates object with empty list.</summary>
constructor Create;
/// <summary>Object destructor. Tears down object.</summary>
destructor Destroy; override;
{ IXMLSimpleNodeList methods }
/// <summary>Retrieves a node from the list by index.</summary>
function GetItem(Idx: Integer): IXMLNode;
/// <summary>Adds a given node to the list and returns index of new node
/// in list.</summary>
function Add(const Node: IXMLNode): Integer;
/// <summary>Returns number of items in list.</summary>
function Count: Integer;
/// <summary>Creates list enumerator.</summary>
function GetEnumerator: TEnumerator<IXMLNode>;
end;
type
/// <summary>
/// Extension of TXMLDocument from XMLDoc unit that implements the methods of
/// IXMLDocumentEx.
/// </summary>
TXMLDocumentEx = class(TXMLDocument,
IXMLDocument, IXMLDocumentEx
)
public
{ IXMLDocumentEx methods }
/// <summary>Finds a node specified by a path from, and including the
/// document's root node.</summary>
/// <param name="PathToNode">DOMString [in] Path to node from root node.
/// </param>
/// <returns>IXMLNode - Reference to required node or nil if node not
/// found.</returns>
/// <remarks>Node in path are separated by backslash, e.g.
/// root\node1\node2</remarks>
function FindNode(PathToNode: DOMString): IXMLNode;
/// <summary>Finds all child nodes of a parent node that are elements and
/// have a specified name.</summary>
/// <param name="ParentNode">IXMLNode [in] Node containing child nodes to
/// be found.</param>
/// <param name="NodeName">DOMString [in] Name of required element nodes.
/// </param>
/// <returns>IXMLSimpleNodeList - List of matching nodes.</returns>
function FindChildNodes(const ParentNode: IXMLNode;
const NodeName: DOMString): IXMLSimpleNodeList;
overload;
/// <summary>Finds all child nodes of a parent node that are elements, have
/// a specified name and have a specified attribute value.</summary>
/// <param name="ParentNode">IXMLNode [in] Node containing child nodes to
/// be found.</param>
/// <param name="NodeName">DOMString [in] Name of required element nodes.
/// </param>
/// <param name="AttribName">DOMString [in] Name of required attribute.
/// </param>
/// <param name="AttribValue">OleVariant [in] Required value of AttribName.
/// </param>
/// <returns>IXMLSimpleNodeList - List of matching nodes.</returns>
function FindChildNodes(const ParentNode: IXMLNode;
const NodeName, AttribName: DOMString;
const AttribValue: OleVariant): IXMLSimpleNodeList;
overload;
/// <summary>Finds first child node that has a given name.</summary>
/// <param name="ParentNode">IXMLNode [in] Node containing child nodes to
/// be searched.</param>
/// <param name="NodeName">DOMString [in] Name of required element node.
/// </param>
/// <returns>IXMLNode - Reference to found node or nil if no node found.
/// </returns>
function FindFirstChildNode(const ParentNode: IXMLNode;
const NodeName: DOMString): IXMLNode;
overload;
/// <summary>Finds first child node that has a given name and attribute
/// value.</summary>
/// <param name="ParentNode">IXMLNode [in] Node containing child nodes to
/// be found.</param>
/// <param name="NodeName">DOMString [in] Name of required element node.
/// </param>
/// <param name="AttribName">DOMString [in] Name of required attribute.
/// </param>
/// <param name="AttribValue">OleVariant [in] Required value of AttribName.
/// </param>
/// <returns>IXMLNode - Reference to found node or nil if no node found.
/// </returns>
function FindFirstChildNode(const ParentNode: IXMLNode;
const NodeName, AttribName: DOMString;
const AttribValue: OleVariant): IXMLNode;
overload;
/// <summary>Creates a new parented XML node in document.</summary>
/// <param name="Parent">IXMLNode [in] Parent node of new node.</param>
/// <param name="NodeName">DOMString [in] Name of new node.</param>
/// <returns>IXMLNode - Requied new node.</returns>
function CreateElement(const Parent: IXMLNode;
const NodeName: DOMString): IXMLNode; overload;
/// <summary>Creates a new parented XML text node in document.</summary>
/// <param name="Parent">IXMLNode [in] Parent node of new node.</param>
/// <param name="NodeName">DOMString [in] Name of new node.</param>
/// <param name="Text">DOMString [in] Text stored in new node.</param>
/// <returns>IXMLNode - Requied new node.</returns>
function CreateElement(const Parent: IXMLNode;
const NodeName, Text: DOMString): IXMLNode; overload;
/// <summary>Loads XML document from data stored in byte array.</summary>
/// <param name="Bytes">TBytes [in] Array of bytes to load.</param>
procedure LoadFromBytes(const Bytes: TBytes);
/// <summary>Stores content of XML document in byte array using encoding
/// specified in inherited Encoding property.</summary>
/// <returns>TBytes - Array of bytes storing XML document.</returns>
function SaveToBytes: TBytes;
end;
implementation
uses
// Delphi
Classes,
// Project
UIStringList;
{ TXMLDocumentEx }
function TXMLDocumentEx.FindChildNodes(const ParentNode: IXMLNode;
const NodeName: DOMString): IXMLSimpleNodeList;
var
NodeList: IXMLNodeList; // list of child nodes of parent
Node: IXMLNode; // a node from NodeList
Idx: Integer; // loops thru nodes of NodeList.
begin
Assert(Assigned(ParentNode),
ClassName + '.FindChildNodes: ParentNode is nil');
Result := TXMLSimpleNodeList.Create;
NodeList := ParentNode.ChildNodes;
for Idx := 0 to Pred(NodeList.Count) do
begin
Node := NodeList[Idx];
if (Node.NodeType = ntElement) and (Node.NodeName = NodeName) then
Result.Add(Node);
end;
end;
function TXMLDocumentEx.CreateElement(const Parent: IXMLNode;
const NodeName: DOMString): IXMLNode;
begin
Result := CreateNode(NodeName);
Parent.ChildNodes.Add(Result);
end;
function TXMLDocumentEx.CreateElement(const Parent: IXMLNode; const NodeName,
Text: DOMString): IXMLNode;
begin
Result := CreateElement(Parent, NodeName);
Result.Text := Text;
end;
function TXMLDocumentEx.FindChildNodes(const ParentNode: IXMLNode;
const NodeName, AttribName: DOMString;
const AttribValue: OleVariant): IXMLSimpleNodeList;
var
NodeList: IXMLNodeList; // list of child nodes of parent
Node: IXMLNode; // a node from NodeList
Idx: Integer; // loops thru nodes of NodeList.
begin
Assert(Assigned(ParentNode), ClassName + 'FindChildNodes: ParentNode is nil');
Result := TXMLSimpleNodeList.Create;
NodeList := ParentNode.ChildNodes;
for Idx := 0 to Pred(NodeList.Count) do
begin
Node := NodeList[Idx];
if (Node.NodeType = ntElement) and (Node.NodeName = NodeName)
and (Node.Attributes[AttribName] = AttribValue) then
Result.Add(Node);
end;
end;
function TXMLDocumentEx.FindFirstChildNode(const ParentNode: IXMLNode;
const NodeName: DOMString): IXMLNode;
begin
Result := ParentNode.ChildNodes.FindNode(NodeName);
end;
function TXMLDocumentEx.FindFirstChildNode(const ParentNode: IXMLNode;
const NodeName, AttribName: DOMString;
const AttribValue: OleVariant): IXMLNode;
var
Nodes: IXMLSimpleNodeList; // list of all matching nodes
begin
Nodes := FindChildNodes(ParentNode, NodeName, AttribName, AttribValue);
if Nodes.Count > 0 then
Result := Nodes[0]
else
Result := nil;
end;
function TXMLDocumentEx.FindNode(PathToNode: DOMString): IXMLNode;
var
NodeNames: IStringList; // list of names of nodes in path
NodeName: string; // name of a node in list
NodeList: IXMLNodeList; // list of child nodes of a node in path
Node: IXMLNode; // refers to each node in path
begin
Node := nil;
NodeList := Self.ChildNodes;
NodeNames := TIStringList.Create(PathToNode, '\', False);
for NodeName in NodeNames do
begin
Node := NodeList.FindNode(NodeName);
if not Assigned(Node) then
Break;
NodeList := Node.ChildNodes;
end;
Result := Node;
end;
procedure TXMLDocumentEx.LoadFromBytes(const Bytes: TBytes);
var
Stm: TBytesStream; // stream used to perform actual loading
begin
Stm := TBytesStream.Create(Bytes);
try
LoadFromStream(Stm);
finally
Stm.Free;
end;
end;
function TXMLDocumentEx.SaveToBytes: TBytes;
var
Stm: TBytesStream; // stream to which XML doc writes data
begin
Stm := TBytesStream.Create;
try
SaveToStream(Stm);
Result := Stm.Bytes;
// we need to set length because Stm.Bytes may have many trailing 0 bytes
SetLength(Result, Stm.Size);
finally
Stm.Free;
end;
end;
{ TXMLSimpleNodeList }
function TXMLSimpleNodeList.Add(const Node: IXMLNode): Integer;
begin
Result := fList.Add(Node);
end;
function TXMLSimpleNodeList.Count: Integer;
begin
Result := fList.Count;
end;
constructor TXMLSimpleNodeList.Create;
begin
inherited;
fList := TList<IXMLNode>.Create;
end;
destructor TXMLSimpleNodeList.Destroy;
begin
fList.Free;
inherited;
end;
function TXMLSimpleNodeList.GetEnumerator: TEnumerator<IXMLNode>;
begin
Result := fList.GetEnumerator;
end;
function TXMLSimpleNodeList.GetItem(Idx: Integer): IXMLNode;
begin
Result := fList[Idx];
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.