-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleXML.pas
4479 lines (3982 loc) · 119 KB
/
SimpleXML.pas
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{************************************************************
SimpleXML - Áèáëèîòåêà äëÿ ñèíòàêñè÷åñêîãî ðàçáîðà òåêñòîâ XML
è ïðåîáðàçîâàíèÿ â èåðàðõèþ XML-îáúåêòîâ.
È íàîáîðîò: ìîæíî ñôîðìèðîâàòü èåðàðõèþ XML-îáúåêòîâ, è
óæå èç íåå ïîëó÷èòü òåêñò XML.
Äîñòîéíàÿ çàìåíà äëÿ MSXML. Ïðè èñïîëüçîâàíèè Ansi-ñòðîê
ðàáîòàåò áûñòðåå è êóøàåò ìåíüøå ïàìÿòè.
(ñ) Àâòîðñêèå ïðàâà 2002,2003 Ìèõàèë Âëàñîâ.
Áèáëèîòåêà áåñïëàòíàÿ è ìîæåò áûòü èñïîëüçîâàíà ïî ëþáîìó íàçíà÷åíèþ.
Ðàçðåøàåòñÿ âíåñåíèå ëþáûõ èçìåíåíèé è èñïîëüçîâàíèå èçìåíåííûõ
áèáëèîòåê áåç îãðàíè÷åíèé.
Åäèíñòâåííîå òðåáîâàíèå: Äàííûé òåêñò äîëæåí ïðèñóòñòâîâàòü
áåç èçìåíåíèé âî âñåõ ìîäèôèêàöèÿõ áèáëèîòåêè.
Âñå ïîæåëàíèÿ ïðèâåòñòâóþ ïî àäðåñó [email protected]
Òàê æå ðåêîìåíäóþ ïîñåòèòü ìîþ ñòðàíè÷êó: http://mv.rb.ru
Òàì Âû âñåãäà íàéäåòå ñàìóþ ïîñëåäíþþ âåðñèþ áèáëèîòåêè.
Æåëàþ ïðèÿòíîãî ïðîãðàììèðîâàíèÿ, Ìèõàèë Âëàñîâ.
Òåêóùàÿ âåðñèÿ: 1.0.1
*************************************************************}
unit SimpleXML;
interface
uses
Windows, SysUtils, Classes;
const
BinXmlSignatureSize = Length('<binary-castle-client-file>');
BinXmlSignature: String = '<binary-castle-client-file>';
BINXML_USE_WIDE_CHARS = 1;
BINXML_COMPRESSED = 2;
XSTR_NULL = '{{null}}';
NODE_INVALID = $00000000;
NODE_ELEMENT = $00000001;
NODE_ATTRIBUTE = $00000002;
NODE_TEXT = $00000003;
NODE_CDATA_SECTION = $00000004;
NODE_ENTITY_REFERENCE = $00000005;
NODE_ENTITY = $00000006;
NODE_PROCESSING_INSTRUCTION = $00000007;
NODE_COMMENT = $00000008;
NODE_DOCUMENT = $00000009;
NODE_DOCUMENT_TYPE = $0000000A;
NODE_DOCUMENT_FRAGMENT = $0000000B;
NODE_NOTATION = $0000000C;
type
// TXmlString - òèï ñòðîêîâûõ ïåðåìåííûõ, èñïîëüçóåìûõ â SimpleXML.
// Ìîæåò áûòü String èëè WideString.
{ $DEFINE XML_WIDE_CHARS}
{$IFDEF XML_WIDE_CHARS}
PXmlChar = PWideChar;
TXmlChar = WideChar;
TXmlString = WideString;
{$ELSE}
PXmlChar = PChar;
TXmlChar = Char;
TXmlString = String;
{$ENDIF}
IXmlDocument = interface;
IXmlElement = interface;
IXmlText = interface;
IXmlCDATASection = interface;
IXmlComment = interface;
IXmlProcessingInstruction = interface;
// IXmlBase - áàçîâûé èíòåðôåéñ äëÿ âñåõ èíòåðôåéñîâ SimpleXML.
IXmlBase = interface
// GetObject - âîçâðàùàåò ññûëêó íà îáúåêò, ðåàëèçóþùèé èíòåðôåéñ.
function GetObject: TObject;
end;
// IXmlNameTable - òàáëèöà èìåí. Êàæäîìó èìåíè ñîïîñòàâëÿåòñÿ íåêèé
// óíèêàëüíûé ÷èñëîâîé èäåíòèôèêàòîð. Èñïîëüçóåòñÿ äëÿ õðàíåíèÿ
// íàçâàíèé òýãîâ è àòðèáóòîâ.
IXmlNameTable = interface(IXmlBase)
// GetID - âîçâðàùàåò ÷èñëîâîé èäåíòèôèêàòîð óêàçàííîé ñòðîêè.
function GetID(const aName: TXmlString): Integer;
// GetID - âîçâðàùàåò ñòðîêó, ñîîòâåòñòâóþùóþ óêàçàííîìó ÷èñëîâîìó
// èäåíòèôèêàòîðó.
function GetName(anID: Integer): TXmlString;
end;
IXmlNode = interface;
// IXmlNodeList - ñïèñîê óçëîâ. Ñïèñîê îðãàíèçîâàí â âèäå ìàññèâà.
// Äîñòóï ê ýëåìåíòàì ñïèñêà ïî èíäåêñó
IXmlNodeList = interface(IXmlBase)
// Get_Count - êîëè÷åñòâî óçëîâ â ñïèñêå
function Get_Count: Integer;
// Get_Item - ïîëó÷èòü óçåë ïî èíäåêñó
function Get_Item(anIndex: Integer): IXmlNode;
// Get_XML - âîçâðàùàåò ïðåäñòàâëåíèå ýëåìåíòîâ ñïèñêà â ôîðìàòå XML
function Get_XML: TXmlString;
property Count: Integer read Get_Count;
property Item[anIndex: Integer]: IXmlNode read Get_Item; default;
property XML: TXmlString read Get_XML;
end;
// IXmlNode - óçåë XML-äåðåâà
IXmlNode = interface(IXmlBase)
// Get_NameTable - òàáëèöà èìåí, èñïîëüçóåìàÿ äàííûì óçëîì
function Get_NameTable: IXmlNameTable;
// Get_NodeName - âîçâðàùàåò íàçâàíèå óçëà. Èíòåðïðåòàöèÿ íàçâàíèÿ óçëà
// çàâèñèò îò åãî òèïà
function Get_NodeName: TXmlString;
// Get_NodeNameID - âîçâðàùàåò êîä íàçâàíèÿ óçëà
function Get_NodeNameID: Integer;
// Get_NodeType - âîçâðàùàåò òèï óçëà
function Get_NodeType: Integer;
// Get_Text - âîçâðàùàåò òåêñò óçëà
function Get_Text: TXmlString;
// Set_Text - èçìåíÿåò òåêñò óçëà
procedure Set_Text(const aValue: TXmlString);
// Get_DataType - âîçàðàùàåò òèï äàííûõ óçëà â òåðìèíàõ âàðèàíòîâ
function Get_DataType: Integer;
// Get_TypedValue - âîçâðàùàåò
function Get_TypedValue: Variant;
// Set_TypedValue - èçìåíÿåò òåêñò óçëà íà òèïèçèðîâàííîå çíà÷åíèå
procedure Set_TypedValue(const aValue: Variant);
// Get_XML - âîçâðàùàåò ïðåäñòàâëåíèå óçëà è âñåõ âëîæåííûõ óçëîâ
// â ôîðìàòå XML.
function Get_XML: TXmlString;
// CloneNode - ñîçäàåò òî÷íóþ êîïèþ äàííîãî óçëà
// Åñëè çàäàí ïðèçíàê aDeep, òî ñîçäàñòñÿ êîïèÿ
// âñåé âåòâè èåðàðõèè îò äàííîãî óçëà.
function CloneNode(aDeep: Boolean = True): IXmlNode;
// Get_ParentNode - âîçâðàùàåò ðîäèòåëüñêèé óçåë
function Get_ParentNode: IXmlNode;
// Get_OwnerDocument - âîçâðàùàåò XML-äîêóìåíò,
// â êîòîðîì ðàñïîëîæåí äàííûé óçåë
function Get_OwnerDocument: IXmlDocument;
// Get_ChildNodes - âîçâðàùàåò ñïèñîê äî÷åðíèõ óçëîâ
function Get_ChildNodes: IXmlNodeList;
// AppendChild - äîáàâëÿåò óêàçàííûé óçåë â êîíåö ñïèñêà äî÷åðíèõ óçëîâ
procedure AppendChild(const aChild: IXmlNode);
// InsertBefore - äîáàâëÿåò óêàçàííûé óçåë â óêàçàííîå ìåñòî ñïèñêà äî÷åðíèõ óçëîâ
procedure InsertBefore(const aChild, aBefore: IXmlNode);
// ReplaceChild - çàìåíÿåò óêàçàííûé óçåë äðóãèì óçëîì
procedure ReplaceChild(const aNewChild, anOldChild: IXmlNode);
// RemoveChild - óäàëÿåò óêàçàííûé óçåë èç ñïèñêà äî÷åðíèõ óçëîâ
procedure RemoveChild(const aChild: IXmlNode);
// AppendElement - ñîçäàåò ýëåìåíò è äîáàâëÿåò åãî â êîíåö ñïèñêà
// â êîíåö ñïèñêà äî÷åðíèõ îáúåêòîâ
function AppendElement(aNameID: Integer): IXmlElement; overload;
function AppendElement(const aName: TxmlString): IXmlElement; overload;
// AppendText - ñîçäàåò òåêñòîâûé óçåë è äîáàâëÿåò åãî
// â êîíåö ñïèñêà äî÷åðíèõ îáúåêòîâ
function AppendText(const aData: TXmlString): IXmlText;
// AppendCDATA - ñîçäàåò ñåêöèþ CDATA è äîáàâëÿåò åå
// â êîíåö ñïèñêà äî÷åðíèõ îáúåêòîâ
function AppendCDATA(const aData: TXmlString): IXmlCDATASection;
// AppendComment - ñîçäàåò êîììåíòàðèé è äîáàâëÿåò åãî
// â êîíåö ñïèñêà äî÷åðíèõ îáúåêòîâ
function AppendComment(const aData: TXmlString): IXmlComment;
// AppendProcessingInstruction - ñîçäàåò èíñòðóêöèþ è äîáàâëÿåò å¸
// â êîíåö ñïèñêà äî÷åðíèõ îáúåêòîâ
function AppendProcessingInstruction(aTargetID: Integer;
const aData: TXmlString): IXmlProcessingInstruction; overload;
function AppendProcessingInstruction(const aTarget: TXmlString;
const aData: TXmlString): IXmlProcessingInstruction; overload;
// GetChildText - âîçâðàùàåò çíà÷åíèå äî÷åðíåãî óçëà
// SetChildText - äîáàâëÿåò èëè èçìåíÿåò çíà÷åíèå äî÷åðíåãî óçëà
function GetChildText(const aName: TXmlString; const aDefault: TXmlString = ''): TXmlString; overload;
function GetChildText(aNameID: Integer; const aDefault: TXmlString = ''): TXmlString; overload;
procedure SetChildText(const aName, aValue: TXmlString); overload;
procedure SetChildText(aNameID: Integer; const aValue: TXmlString); overload;
// NeedChild - âîçâðàùàåò äî÷åðíèé óçåë ñ óêàçàííûì èìåíåì.
// Åñëè óçåë íå íàéäåí, òî ãåíåðèðóåòñÿ èñêëþ÷åíèå
function NeedChild(aNameID: Integer): IXmlNode; overload;
function NeedChild(const aName: TXmlString): IXmlNode; overload;
// EnsureChild - âîçâðàùàåò äî÷åðíèé óçåë ñ óêàçàííûì èìåíåì.
// Åñëè óçåë íå íàéäåí, òî îí áóäåò ñîçäàí
function EnsureChild(aNameID: Integer): IXmlNode; overload;
function EnsureChild(const aName: TXmlString): IXmlNode; overload;
// RemoveAllChilds - óäàëÿåò âñå äî÷åðíèå óçëû
procedure RemoveAllChilds;
// SelectNodes - ïðîèçâîäèò âûáîðêó óçëîâ, óäîâëåòâîðÿþùèõ
// óêàçàííûì êðèòåðèÿì
function SelectNodes(const anExpression: TXmlString): IXmlNodeList;
// SelectSingleNode - ïðîèçâîäèò ïîèñê ïåðâîãî óçëà, óäîâëåòâîðÿþùåãî
// óêàçàííûì êðèòåðèÿì
function SelectSingleNode(const anExpression: TXmlString): IXmlNode;
// FindElement - ïðîèçâîäèò ïîèñê ïåðâîãî óçëà, óäîâëåòâîðÿþùåãî
// óêàçàííûì êðèòåðèÿì
function FindElement(const anElementName, anAttrName: String; const anAttrValue: Variant): IXmlElement;
// Get_AttrCount - âîçâðàùàåò êîëè÷åñòâî àòðèáóòîâ
function Get_AttrCount: Integer;
// Get_AttrNameID - âîçâðàùàåò êîä íàçâàíèÿ àòðèáóòà
function Get_AttrNameID(anIndex: Integer): Integer;
// Get_AttrName - âîçâðàùàåò íàçâàíèå àòðèáóòà
function Get_AttrName(anIndex: Integer): TXmlString;
// RemoveAttr - óäàëÿåò àòðèáóò
procedure RemoveAttr(const aName: TXmlString); overload;
procedure RemoveAttr(aNameID: Integer); overload;
// RemoveAllAttrs - óäàëÿåò âñå àòðèáóòû
procedure RemoveAllAttrs;
// AttrExists - ïðîâåðÿåò, çàäàí ëè óêàçàííûé àòðèáóò.
function AttrExists(aNameID: Integer): Boolean; overload;
function AttrExists(const aName: TXmlString): Boolean; overload;
// GetAttrType - âîçàðàùàåò òèï äàííûõ àòðèáóòà â òåðìèíàõ âàðèàíòîâ
function GetAttrType(aNameID: Integer): Integer; overload;
function GetAttrType(const aName: TXmlString): Integer; overload;
// GetAttrType - âîçâðàùàåò òèï àòðèáóòà
// Result
// GetVarAttr - âîçâðàùàåò òèïèçèðîâàííîå çíà÷åíèå óêàçàííîãî àòðèáóòà.
// Åñëè àòðèáóò íå çàäàí, òî âîçâðàùàåòñÿ çíà÷åíèå ïî óìîë÷àíèþ
// SetAttr - èçìåíÿåò èëè äîáàâëÿåò óêàçàííûé àòðèáóò
function GetVarAttr(aNameID: Integer; const aDefault: Variant): Variant; overload;
function GetVarAttr(const aName: TXmlString; const aDefault: Variant): Variant; overload;
procedure SetVarAttr(aNameID: Integer; const aValue: Variant); overload;
procedure SetVarAttr(const aName: TXmlString; aValue: Variant); overload;
// NeedAttr - âîçâðàùàåò ñòðîêîâîå çíà÷åíèå óêàçàííîãî àòðèáóòà.
// Åñëè àòðèáóò íå çàäàí, òî ãåíåðèðóåòñÿ èñêëþ÷åíèå
function NeedAttr(aNameID: Integer): TXmlString; overload;
function NeedAttr(const aName: TXmlString): TXmlString; overload;
// GetAttr - âîçâðàùàåò ñòðîêîâîå çíà÷åíèå óêàçàííîãî àòðèáóòà.
// Åñëè àòðèáóò íå çàäàí, òî âîçâðàùàåòñÿ çíà÷åíèå ïî óìîë÷àíèþ
// SetAttr - èçìåíÿåò èëè äîáàâëÿåò óêàçàííûé àòðèáóò
function GetAttr(aNameID: Integer; const aDefault: TXmlString = ''): TXmlString; overload;
function GetAttr(const aName: TXmlString; const aDefault: TXmlString = ''): TXmlString; overload;
procedure SetAttr(aNameID: Integer; const aValue: TXmlString); overload;
procedure SetAttr(const aName, aValue: TXmlString); overload;
// GetBoolAttr - âîçâðàùàåò öåëî÷èñëåííîå çíà÷åíèå óêàçàííîãî àòðèáóòà
// SetBoolAttr - èçìåíÿåò èëè äîáàâëÿåò óêàçàííûé àòðèáóò öåëî÷èñëåííûì
// çíà÷åíèåì
function GetBoolAttr(aNameID: Integer; aDefault: Boolean = False): Boolean; overload;
function GetBoolAttr(const aName: TXmlString; aDefault: Boolean = False): Boolean; overload;
procedure SetBoolAttr(aNameID: Integer; aValue: Boolean = False); overload;
procedure SetBoolAttr(const aName: TXmlString; aValue: Boolean); overload;
// GetIntAttr - âîçâðàùàåò öåëî÷èñëåííîå çíà÷åíèå óêàçàííîãî àòðèáóòà
// SetIntAttr - èçìåíÿåò èëè äîáàâëÿåò óêàçàííûé àòðèáóò öåëî÷èñëåííûì
// çíà÷åíèåì
function GetIntAttr(aNameID: Integer; aDefault: Integer = 0): Integer; overload;
function GetIntAttr(const aName: TXmlString; aDefault: Integer = 0): Integer; overload;
procedure SetIntAttr(aNameID: Integer; aValue: Integer); overload;
procedure SetIntAttr(const aName: TXmlString; aValue: Integer); overload;
// GetDateTimeAttr - âîçâðàùàåò öåëî÷èñëåííîå çíà÷åíèå óêàçàííîãî àòðèáóòà
// SetDateTimeAttr - èçìåíÿåò èëè äîáàâëÿåò óêàçàííûé àòðèáóò öåëî÷èñëåííûì
// çíà÷åíèåì
function GetDateTimeAttr(aNameID: Integer; aDefault: TDateTime = 0): TDateTime; overload;
function GetDateTimeAttr(const aName: TXmlString; aDefault: TDateTime = 0): TDateTime; overload;
procedure SetDateTimeAttr(aNameID: Integer; aValue: TDateTime); overload;
procedure SetDateTimeAttr(const aName: TXmlString; aValue: TDateTime); overload;
// GetFloatAttr - âîçâðàùàåò çíà÷åíèå óêàçàííîãî àòðèáóòà â âèäå
// âåùåñòâåííîãî ÷èñëà
// SetFloatAttr - èçìåíÿåò èëè äîáàâëÿåò óêàçàííûé àòðèáóò âåùåñòâåííûì
// çíà÷åíèåì
function GetFloatAttr(aNameID: Integer; aDefault: Double = 0): Double; overload;
function GetFloatAttr(const aName: TXmlString; aDefault: Double = 0): Double; overload;
procedure SetFloatAttr(aNameID: Integer; aValue: Double); overload;
procedure SetFloatAttr(const aName: TXmlString; aValue: Double); overload;
// GetHexAttr - ïîëó÷åíèå çíà÷åíèÿ óêàçàííîãî àòðèáóòà â öåëî÷èñëåííîì âèäå.
// Ñòðîêîâîå çíà÷åíèå àòðèáóòà ïðåîáðàçóåòñÿ â öåëîå ÷èñëî. Èñõîäíàÿ
// ñòðîêà äîëæíà áûòü çàäàíà â øåñòíàäöàòèðè÷íîì âèäå áåç ïðåôèêñîâ
// ("$", "0x" è ïð.) Åñëè ïðåîáðàçîâàíèå íå ìîæåò áûòü âûïîëíåíî,
// ãåíåðèðóåòñÿ èñêëþ÷åíèå.
// Åñëè àòðèáóò íå çàäàí, âîçâðàùàåòñÿ çíà÷åíèå ïàðàìåòðà aDefault.
// SetHexAttr - èçìåíåíèå çíà÷åíèÿ óêàçàííîãî àòðèáóòà íà ñòðîêîâîå
// ïðåäñòàâëåíèå öåëîãî ÷èñëà â øåñòíàäöàòèðè÷íîì âèäå áåç ïðåôèêñîâ
// ("$", "0x" è ïð.) Åñëè ïðåîáðàçîâàíèå íå ìîæåò áûòü âûïîëíåíî,
// ãåíåðèðóåòñÿ èñêëþ÷åíèå.
// Åñëè àòðèáóò íå áûë çàäàí, äî îí áóäåò äîáàâëåí.
// Åñëè áûë çàäàí, òî áóäåò èçìåíåí.
function GetHexAttr(const aName: TXmlString; aDefault: Integer = 0): Integer; overload;
function GetHexAttr(aNameID: Integer; aDefault: Integer = 0): Integer; overload;
procedure SetHexAttr(const aName: TXmlString; aValue: Integer; aDigits: Integer = 8); overload;
procedure SetHexAttr(aNameID: Integer; aValue: Integer; aDigits: Integer = 8); overload;
// GetEnumAttr - èùåò çíà÷åíèå àòðèáóòà â óêàçàííîì ñïèñêå ñòðîê è
// âîçâðàùàåò èíäåêñ íàéäåííîé ñòðîêè. Åñëè àòðèáóò çàäàí íî íå íàéäåí
// â ñïèñêå, òî ãåíåðèðóåòñÿ èñêëþ÷åíèå.
// Åñëè àòðèáóò íå çàäàí, âîçâðàùàåòñÿ çíà÷åíèå ïàðàìåòðà aDefault.
function GetEnumAttr(const aName: TXmlString;
const aValues: array of TXmlString; aDefault: Integer = 0): Integer; overload;
function GetEnumAttr(aNameID: Integer;
const aValues: array of TXmlString; aDefault: Integer = 0): Integer; overload;
function NeedEnumAttr(const aName: TXmlString;
const aValues: array of TXmlString): Integer; overload;
function NeedEnumAttr(aNameID: Integer;
const aValues: array of TXmlString): Integer; overload;
function Get_Values(const aName: String): Variant;
procedure Set_Values(const aName: String; const aValue: Variant);
function AsElement: IXmlElement;
function AsText: IXmlText;
function AsCDATASection: IXmlCDATASection;
function AsComment: IXmlComment;
function AsProcessingInstruction: IXmlProcessingInstruction;
property NodeName: TXmlString read Get_NodeName;
property NodeNameID: Integer read Get_NodeNameID;
property NodeType: Integer read Get_NodeType;
property ParentNode: IXmlNode read Get_ParentNode;
property OwnerDocument: IXmlDocument read Get_OwnerDocument;
property NameTable: IXmlNameTable read Get_NameTable;
property ChildNodes: IXmlNodeList read Get_ChildNodes;
property AttrCount: Integer read Get_AttrCount;
property AttrNames[anIndex: Integer]: TXmlString read Get_AttrName;
property AttrNameIDs[anIndex: Integer]: Integer read Get_AttrNameID;
property Text: TXmlString read Get_Text write Set_Text;
property DataType: Integer read Get_DataType;
property TypedValue: Variant read Get_TypedValue write Set_TypedValue;
property XML: TXmlString read Get_XML;
property Values[const aName: String]: Variant read Get_Values write Set_Values; default;
end;
IXmlElement = interface(IXmlNode)
// ReplaceTextByCDATASection - óäàëÿåò âñå òåêñòîâûå ýëåìåíòû è äîáàâëÿåò
// îäíó ñåêöèþ CDATA, ñîäåðæàùóþ óêàçàííûé òåêñò
procedure ReplaceTextByCDATASection(const aText: TXmlString);
// ReplaceTextByBynaryData - óäàëÿåò âñå òåêñòîâûå ýëåìåíòû è äîáàâëÿåò
// îäèí òåêñòîâûé ýëåìåíò, ñîäåðæàùèé óêàçàííûå äâîè÷íûå äàííûå
// â ôîðìàòå "base64".
// Åñëè ïàðàìåòð aMaxLineLength íå ðàâåí íóëþ, òî ïðîèçâîäèòñÿ ðàçáèâêà
// ïîëó÷åíîé ñòðîêè íà ñòðîêè äëèíîé aMaxLineLength.
// Ñòðîêè ðàçäåëÿþòñÿ ïàðîé ñèìâîëîâ #13#10 (CR,LF).
// Ïîñëå ïîñëåäíåé ñòðîêè óêàçàííûå ñèìâîëû íå âñòàâëÿþòñÿ.
procedure ReplaceTextByBynaryData(const aData; aSize: Integer;
aMaxLineLength: Integer);
// GetTextAsBynaryData - cîáèðàåò âñå òåêñòîâûå ýëåìåíòû â îäíó ñòðîêó è
// ïðîèçâîäèò ïðåîáðàçîâàíèå èç ôîðìàòà "base64" â äâîè÷íûå äàííûå.
// Ïðè ïðåîáðàçîâàíèè èãíîðèðóþòñÿ âñå ïðîáåëüíûå ñèìâîëû (ñ êîäîì <= ' '),
// ñîäåðæàùèåñÿ â èñõîäíîé ñòðîêå.
function GetTextAsBynaryData: TXmlString;
end;
IXmlCharacterData = interface(IXmlNode)
end;
IXmlText = interface(IXmlCharacterData)
end;
IXmlCDATASection = interface(IXmlCharacterData)
end;
IXmlComment = interface(IXmlCharacterData)
end;
IXmlProcessingInstruction = interface(IXmlNode)
end;
IXmlDocument = interface(IXmlNode)
function Get_DocumentElement: IXmlElement;
function Get_BinaryXML: String;
function Get_PreserveWhiteSpace: Boolean;
procedure Set_PreserveWhiteSpace(aValue: Boolean);
function NewDocument(const aVersion, anEncoding: TXmlString;
aRootElementNameID: Integer): IXmlElement; overload;
function NewDocument(const aVersion, anEncoding,
aRootElementName: TXmlString): IXmlElement; overload;
function CreateElement(aNameID: Integer): IXmlElement; overload;
function CreateElement(const aName: TXmlString): IXmlElement; overload;
function CreateText(const aData: TXmlString): IXmlText;
function CreateCDATASection(const aData: TXmlString): IXmlCDATASection;
function CreateComment(const aData: TXmlString): IXmlComment;
function CreateProcessingInstruction(const aTarget,
aData: TXmlString): IXmlProcessingInstruction; overload;
function CreateProcessingInstruction(aTargetID: Integer;
const aData: TXmlString): IXmlProcessingInstruction; overload;
procedure LoadXML(const aXML: TXmlString);
procedure LoadBinaryXML(const aXML: String);
procedure Load(aStream: TStream); overload;
procedure Load(const aFileName: TXmlString); overload;
procedure LoadResource(aType, aName: PChar);
procedure Save(aStream: TStream); overload;
procedure Save(const aFileName: TXmlString); overload;
procedure SaveBinary(aStream: TStream; anOptions: LongWord = 0); overload;
procedure SaveBinary(const aFileName: TXmlString; anOptions: LongWord = 0); overload;
property PreserveWhiteSpace: Boolean read Get_PreserveWhiteSpace write Set_PreserveWhiteSpace;
property DocumentElement: IXmlElement read Get_DocumentElement;
property BinaryXML: String read Get_BinaryXML;
end;
function CreateNameTable(aHashTableSize: Integer = 4096): IXmlNameTable;
function CreateXmlDocument(
const aRootElementName: String = '';
const aVersion: String = '1.0';
const anEncoding: String = ''; // SimpleXmlDefaultEncoding
const aNames: IXmlNameTable = nil): IXmlDocument;
function CreateXmlElement(const aName: TXmlString; const aNameTable: IXmlNameTable = nil): IXmlElement;
function LoadXmlDocumentFromXML(const aXML: TXmlString): IXmlDocument;
function LoadXmlDocumentFromBinaryXML(const aXML: String): IXmlDocument;
function LoadXmlDocument(aStream: TStream): IXmlDocument; overload;
function LoadXmlDocument(const aFileName: TXmlString): IXmlDocument; overload;
function LoadXmlDocument(aResType, aResName: PChar): IXmlDocument; overload;
var
DefaultNameTable: IXmlNameTable = nil;
DefaultPreserveWhiteSpace: Boolean = False;
DefaultEncoding: String = 'windows-1251';
DefaultIndentText: String = ^I;
resourcestring
SSimpleXmlError1 = 'Îøèáêà ïîëó÷åíèÿ ýëåìåíòà ñïèñêà: èíäåêñ âûõîäèò çà ïðåäåëû';
SSimpleXmlError2 = 'Íå çàâåðøåíî îïðåäåëåíèå ýëåìåíòà';
SSimpleXmlError3 = 'Íåêîððåòíûé ñèìâîë â èìåíè ýëåìåíòà';
SSimpleXmlError4 = 'Îøèáêà ÷òåíèÿ äâîè÷íîãî XML: íåêîððåêòíûé òèï óçëà';
SSimpleXmlError5 = 'Îøèáêà çàïèñè äâîè÷íîãî XML: íåêîððåêòíûé òèï óçëà';
SSimpleXmlError6 = 'Íåâåðíîå çíà÷åíèå àòðèáóòà "%s" ýëåìåíòà "%s".'^M^J +
'Äîïóñòèìûå çíà÷åíèÿ:'^M^J +
'%s';
SSimpleXmlError7 = 'Íå íàéäåí àòðèáóò "%s"';
SSimpleXmlError8 = 'Íå çàäàí àòðèáóò "%s"';
SSimpleXmlError9 = 'Äàííàÿ âîçìîæíîñòü íå ïîääåðæèâàåòñÿ SimpleXML';
SSimpleXmlError10 = 'Îøèáêà: íå íàéäåí äî÷åðíèé ýëåìåíò "%s".';
SSimpleXmlError11 = 'Èìÿ äîëæíî íà÷èíàòüñÿ ñ áóêâû èëè "_"';
SSimpleXmlError12 = 'Îæèäàåòñÿ ÷èñëî';
SSimpleXmlError13 = 'Îæèäàåòñÿ øåñòíàäöàòåðè÷íîå ÷èñëî';
SSimpleXmlError14 = 'Îæèäàåòñÿ "#" èëè èìÿ óïðàìëÿþùåãî ñèìâîëà';
SSimpleXmlError15 = 'Íåêîððåêòíîå èìÿ óïðàâëÿþùåãî ñèìâîëà';
SSimpleXmlError16 = 'Îæèäàåòñÿ "%c"';
SSimpleXmlError17 = 'Îæèäàåòñÿ "%s"';
SSimpleXmlError18 = 'Ñèìâîë "<" íå ìîæåò èñïîëüçîâàòüñÿ â çíà÷åíèÿõ àòðèáóòîâ';
SimpleXmlError19 = 'Îæèäàåòñÿ "%s"';
SSimpleXmlError20 = 'Îæèäàåòñÿ çíà÷åíèå àòðèáóòà';
SSimpleXmlError21 = 'Îæèäàåòñÿ ñòðîêîâàÿ êîíñòàíòà';
SimpleXmlError22 = 'Îæèäàåòñÿ "%s"';
SSimpleXmlError23 = 'Îøèáêà ÷òåíèÿ äàííûõ.';
SSimpleXmlError24 = 'Îøèáêà ÷òåíèÿ çíà÷åíèÿ: íåêîððåêòíûé òèï.';
SSimpleXmlError25 = 'Îøèáêà çàïèñè çíà÷åíèÿ: íåêîððåêòíûé òèï.';
function XSTRToFloat(s: TXmlString): Double;
function FloatToXSTR(v: Double): TXmlString;
function DateTimeToXSTR(v: TDateTime): TXmlString;
function VarToXSTR(const v: TVarData): TXmlString;
function TextToXML(const aText: TXmlString): TXmlString;
function BinToBase64(const aBin; aSize, aMaxLineLength: Integer): String;
function Base64ToBin(const aBase64: String): String;
function IsXmlDataString(const aData: String): Boolean;
function XmlIsInBinaryFormat(const aData: String): Boolean;
procedure PrepareToSaveXml(var anElem: IXmlElement; const aChildName: String);
function PrepareToLoadXml(var anElem: IXmlElement; const aChildName: String): Boolean;
implementation
uses
Variants, DateUtils;
function TextToXML(const aText: TXmlString): TXmlString;
var
i, j: Integer;
begin
j := 0;
for i := 1 to Length(aText) do
case aText[i] of
'<', '>': Inc(j, 4);
'&': Inc(j, 5);
'"': Inc(j, 6);
else
Inc(j);
end;
if j = Length(aText) then
Result := aText
else begin
SetLength(Result, j);
j := 1;
for i := 1 to Length(aText) do
case aText[i] of
'<': begin Move(PChar('<')^, Result[j], 4); Inc(j, 4) end;
'>': begin Move(PChar('>')^, Result[j], 4); Inc(j, 4) end;
'&': begin Move(PChar('&')^, Result[j], 5); Inc(j, 5) end;
'"': begin Move(PChar('"')^, Result[j], 6); Inc(j, 6) end;
else begin Result[j] := aText[i]; Inc(j) end;
end;
end;
end;
function XSTRToFloat(s: TXmlString): Double;
var
aPos: Integer;
begin
if '.' = DecimalSeparator then
aPos := Pos(',', s)
else if ',' = DecimalSeparator then
aPos := Pos('.', s)
else begin
aPos := Pos(',', s);
if aPos = 0 then
aPos := Pos('.', s);
end;
if aPos <> 0 then
s[aPos] := TXmlChar(DecimalSeparator);
Result := StrToFloat(s);
end;
function FloatToXSTR(v: Double): TXmlString;
var
aPos: Integer;
begin
Result := FloatToStr(v);
aPos := Pos(DecimalSeparator, Result);
if aPos <> 0 then
Result[aPos] := '.';
end;
function XSTRToDateTime(const s: String): TDateTime;
var
aPos: Integer;
function FetchTo(aStop: Char): Integer;
var
i: Integer;
begin
i := aPos;
while (i <= Length(s)) and (s[i] in ['0'..'9']) do
Inc(i);
if i > aPos then
Result := StrToInt(Copy(s, aPos, i - aPos))
else
Result := 0;
if (i <= Length(s)) and (s[i] = aStop) then
aPos := i + 1
else
aPos := Length(s) + 1;
end;
var
y, m, d, h, n, ss: Integer;
begin
aPos := 1;
y := FetchTo('-'); m := FetchTo('-'); d := FetchTo('T');
h := FetchTo('-'); n := FetchTo('-'); ss := FetchTo('-');
Result := EncodeDateTime(y, m, d, h, n, ss, 0);
end;
function DateTimeToXSTR(v: TDateTime): TXmlString;
var
y, m, d, h, n, s, ms: Word;
begin
DecodeDateTime(v, y, m, d, h, n, s, ms);
Result := Format('%.4d-%.2d-%.2dT%.2d-%.2d-%.2d', [y, m, d, h, n, s])
end;
function VarToXSTR(const v: TVarData): TXmlString;
const
BoolStr: array[Boolean] of TXmlString = ('0', '1');
var
p: Pointer;
begin
case v.VType of
varNull: Result := XSTR_NULL;
varSmallint: Result := IntToStr(v.VSmallInt);
varInteger: Result := IntToStr(v.VInteger);
varSingle: Result := FloatToXSTR(v.VSingle);
varDouble: Result := FloatToXSTR(v.VDouble);
varCurrency: Result := FloatToXSTR(v.VCurrency);
varDate: Result := DateTimeToXSTR(v.VDate);
varOleStr: Result := v.VOleStr;
varBoolean: Result := BoolStr[v.VBoolean = True];
varShortInt: Result := IntToStr(v.VShortInt);
varByte: Result := IntToStr(v.VByte);
varWord: Result := IntToStr(v.VWord);
varLongWord: Result := IntToStr(v.VLongWord);
varInt64: Result := IntToStr(v.VInt64);
varString: Result := String(v.VString);
varArray + varByte:
begin
p := VarArrayLock(Variant(v));
try
Result := BinToBase64(p^, VarArrayHighBound(Variant(v), 1) - VarArrayLowBound(Variant(v), 1) + 1, 0);
finally
VarArrayUnlock(Variant(v))
end
end;
else
Result := Variant(v)
end;
end;
procedure PrepareToSaveXml(var anElem: IXmlElement; const aChildName: String);
begin
if aChildName <> '' then
anElem := anElem.AppendElement(aChildName);
end;
function PrepareToLoadXml(var anElem: IXmlElement; const aChildName: String): Boolean;
begin
if (aChildName <> '') and Assigned(anElem) then
anElem := anElem.selectSingleNode(aChildName).AsElement;
Result := Assigned(anElem);
end;
function LoadXMLResource(aModule: HMODULE; aName, aType: PChar; const aXMLDoc: IXmlDocument): boolean;
var
aRSRC: HRSRC;
aGlobal: HGLOBAL;
aSize: DWORD;
aPointer: Pointer;
aStream: TStringStream;
begin
Result := false;
aRSRC := FindResource(aModule, aName, aType);
if aRSRC <> 0 then begin
aGlobal := LoadResource(aModule, aRSRC);
aSize := SizeofResource(aModule, aRSRC);
if (aGlobal <> 0) and (aSize <> 0) then begin
aPointer := LockResource(aGlobal);
if Assigned(aPointer) then begin
aStream := TStringStream.Create('');
try
aStream.WriteBuffer(aPointer^, aSize);
aXMLDoc.LoadXML(aStream.DataString);
Result := true;
finally
aStream.Free;
end;
end;
end;
end;
end;
function IsXmlDataString(const aData: String): Boolean;
var
i: Integer;
begin
Result := Copy(aData, 1, BinXmlSignatureSize) = BinXmlSignature;
if not Result then begin
i := 1;
while (i <= Length(aData)) and (aData[i] in [#10, #13, #9, ' ']) do
Inc(i);
Result := Copy(aData, i, Length('<?xml ')) = '<?xml ';
end;
end;
function XmlIsInBinaryFormat(const aData: String): Boolean;
begin
Result := Copy(aData, 1, BinXmlSignatureSize) = BinXmlSignature
end;
var
Base64Map: array [0..63] of Char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
type
PChars = ^TChars;
TChars = packed record a, b, c, d: Char end;
POctet = ^TOctet;
TOctet = packed record a, b, c: Byte; end;
procedure OctetToChars(po: POctet; aCount: Integer; pc: PChars);
var
o: Integer;
begin
if aCount = 1 then begin
o := po.a shl 16;
LongWord(pc^) := $3D3D3D3D;
pc.a := Base64Map[(o shr 18) and $3F];
pc.b := Base64Map[(o shr 12) and $3F];
end
else if aCount = 2 then begin
o := po.a shl 16 or po.b shl 8;
LongWord(pc^) := $3D3D3D3D;
pc.a := Base64Map[(o shr 18) and $3F];
pc.b := Base64Map[(o shr 12) and $3F];
pc.c := Base64Map[(o shr 6) and $3F];
end
else if aCount > 2 then begin
o := po.a shl 16 or po.b shl 8 or po.c;
LongWord(pc^) := $3D3D3D3D;
pc.a := Base64Map[(o shr 18) and $3F];
pc.b := Base64Map[(o shr 12) and $3F];
pc.c := Base64Map[(o shr 6) and $3F];
pc.d := Base64Map[o and $3F];
end;
end;
function BinToBase64(const aBin; aSize, aMaxLineLength: Integer): String;
var
o: POctet;
c: PChars;
aCount: Integer;
i: Integer;
begin
o := @aBin;
aCount := aSize;
SetLength(Result, ((aCount + 2) div 3)*4);
c := PChars(Result);
while aCount > 0 do begin
OctetToChars(o, aCount, c);
Inc(o);
Inc(c);
Dec(aCount, 3);
end;
if aMaxLineLength > 0 then begin
i := aMaxLineLength;
while i <= Length(Result) do begin
Insert(#13#10, Result, i);
Inc(i, 2 + aMaxLineLength);
end
end;
end;
function CharTo6Bit(c: Char): Byte;
begin
if (c >= 'A') and (c <= 'Z') then
Result := Ord(c) - Ord('A')
else if (c >= 'a') and (c <= 'z') then
Result := Ord(c) - Ord('a') + 26
else if (c >= '0') and (c <= '9') then
Result := Ord(c) - Ord('0') + 52
else if c = '+' then
Result := 62
else if c = '/' then
Result := 63
else
Result := 0
end;
procedure CharsToOctet(c: PChars; o: POctet);
var
i: Integer;
begin
if c.c = '=' then begin // 1 byte
i := CharTo6Bit(c.a) shl 18 or CharTo6Bit(c.b) shl 12;
o.a := (i shr 16) and $FF;
end
else if c.d = '=' then begin // 2 bytes
i := CharTo6Bit(c.a) shl 18 or CharTo6Bit(c.b) shl 12 or CharTo6Bit(c.c) shl 6;
o.a := (i shr 16) and $FF;
o.b := (i shr 8) and $FF;
end
else begin // 3 bytes
i := CharTo6Bit(c.a) shl 18 or CharTo6Bit(c.b) shl 12 or CharTo6Bit(c.c) shl 6 or CharTo6Bit(c.d);
o.a := (i shr 16) and $FF;
o.b := (i shr 8) and $FF;
o.c := i and $FF;
end;
end;
function Base64ToBin(const aBase64: String): String;
var
o: POctet;
c: PChars;
aCount: Integer;
s: String;
i, j: Integer;
begin
s := aBase64;
i := 1;
while i <= Length(s) do begin
while (i <= Length(s)) and (s[i] > ' ') do
Inc(i);
if i <= Length(s) then begin
j := i;
while (j <= Length(s)) and (s[j] <= ' ') do
Inc(j);
Delete(s, i, j - i);
end;
end;
if Length(s) < 4 then
Result := ''
else begin
aCount := ((Length(s) + 3) div 4)*3;
if aCount > 0 then begin
if s[Length(s) - 1] = '=' then
Dec(aCount, 2)
else if s[Length(s)] = '=' then
Dec(aCount);
SetLength(Result, aCount);
FillChar(Result[1], aCount, '*');
c := @s[1];
o := @Result[1];
while aCount > 0 do begin
CharsToOctet(c, o);
Inc(o);
Inc(c);
Dec(aCount, 3);
end;
end;
end;
end;
type
TBinXmlReader = class
private
FOptions: LongWord;
public
procedure Read(var aBuf; aSize: Integer); virtual; abstract;
function ReadLongint: Longint;
function ReadAnsiString: String;
function ReadWideString: WideString;
function ReadXmlString: TXmlString;
procedure ReadVariant(var v: TVarData);
end;
TStmXmlReader = class(TBinXmlReader)
private
FStream: TStream;
FOptions: LongWord;
FBufStart,
FBufEnd,
FBufPtr: PChar;
FBufSize,
FRestSize: Integer;
public
constructor Create(aStream: TStream; aBufSize: Integer);
destructor Destroy; override;
procedure Read(var aBuf; aSize: Integer); override;
end;
TStrXmlReader = class(TBinXmlReader)
private
FString: String;
FOptions: LongWord;
FPtr: PChar;
FRestSize: Integer;
public
constructor Create(const aStr: String);
procedure Read(var aBuf; aSize: Integer); override;
end;
TBinXmlWriter = class
private
FOptions: LongWord;
public
procedure Write(const aBuf; aSize: Integer); virtual; abstract;
procedure WriteLongint(aValue: Longint);
procedure WriteAnsiString(const aValue: String);
procedure WriteWideString(const aValue: WideString);
procedure WriteXmlString(const aValue: TXmlString);
procedure WriteVariant(const v: TVarData);
end;
TStmXmlWriter = class(TBinXmlWriter)
private
FStream: TStream;
FBufStart,
FBufEnd,
FBufPtr: PChar;
FBufSize: Integer;
public
constructor Create(aStream: TStream; anOptions: LongWord; aBufSize: Integer);
destructor Destroy; override;
procedure Write(const aBuf; aSize: Integer); override;
end;
TStrXmlWriter = class(TBinXmlWriter)
private
FData: String;
FBufStart,
FBufEnd,
FBufPtr: PChar;
FBufSize: Integer;
procedure FlushBuf;
public
constructor Create(anOptions: LongWord; aBufSize: Integer);
destructor Destroy; override;
procedure Write(const aBuf; aSize: Integer); override;
end;
TXmlBase = class(TInterfacedObject, IXmlBase)
protected
// ðåàëèçàöèÿ èíòåðôåéñà IXmlBase
function GetObject: TObject;
public
end;
PNameIndexArray = ^TNameIndexArray;
TNameIndexArray = array of Longint;
TXmlNameTable = class(TXmlBase, IXmlNameTable)
private
FNames: array of TXmlString;
FHashTable: array of TNameIndexArray;
FXmlTextNameID: Integer;
FXmlCDATASectionNameID: Integer;
FXmlCommentNameID: Integer;
FXmlDocumentNameID: Integer;
FXmlID: Integer;
protected
function GetID(const aName: TXmlString): Integer;
function GetName(anID: Integer): TXmlString;
public
constructor Create(aHashTableSize: Integer);
procedure LoadBinXml(aReader: TBinXmlReader);
procedure SaveBinXml(aWriter: TBinXmlWriter);
end;
{ TXmlBase }
function TXmlBase.GetObject: TObject;
begin
Result := Self;
end;
{ TXmlNameTable }
constructor TXmlNameTable.Create(aHashTableSize: Integer);
begin
inherited Create;
SetLength(FHashTable, aHashTableSize);
FXmlTextNameID := GetID('#text');
FXmlCDATASectionNameID := GetID('#cdata-section');
FXmlCommentNameID := GetID('#comment');
FXmlDocumentNameID := GetID('#document');
FXmlID := GetID('xml');
end;
procedure TXmlNameTable.LoadBinXml(aReader: TBinXmlReader);
var
aCount: LongInt;
anIndex, i: Integer;
begin
// Ñ÷èòàòü ìàññèâ èìåí
aCount := aReader.ReadLongint;
SetLength(FNames, aCount);
for i := 0 to aCount - 1 do
FNames[i] := aReader.ReadXmlString;
// Ñ÷èòàòü õýø-òàáëèöó
SetLength(FHashTable, aReader.ReadLongint);
for i := 0 to Length(FHashTable) - 1 do
SetLength(FHashTable[i], 0);
aCount := aReader.ReadLongint;
for i := 0 to aCount - 1 do begin
anIndex := aReader.ReadLongInt;
SetLength(FHashTable[anIndex], aReader.ReadLongInt);
aReader.Read(FHashTable[anIndex][0], Length(FHashTable[anIndex])*sizeof(Longint));
end;
end;
procedure TXmlNameTable.SaveBinXml(aWriter: TBinXmlWriter);
var
aCount: LongInt;
i: Integer;
begin
// Çàïèñàòü ìàññèâ èìåí
aCount := Length(FNames);
aWriter.WriteLongint(aCount);
for i := 0 to aCount - 1 do
aWriter.WriteXmlString(FNames[i]);
// Çàïèñàòü õýø-òàáëèöó
aWriter.WriteLongint(Length(FHashTable));
aCount := 0;
for i := 0 to Length(FHashTable) - 1 do