This file is indexed.

/usr/src/castle-game-engine-6.4/files/castlexmlutils.pas is in castle-game-engine-src 6.4+dfsg1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
{
  Copyright 2012-2017 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

{ Various XML and DOM utilities. }
unit CastleXMLUtils;

interface

uses SysUtils, DOM, CastleDownload,
  CastleUtils, CastleColors, CastleVectors;

type
  EDOMAttributeMissing = class(Exception);
  EDOMChildElementError = class(Exception);

  TXMLElementIterator = class;

  TDOMNodeHelper = class helper for TDOMNode
  private
    function GetNodeValue8: string;
    procedure SetNodeValue8(const S: string);
  public
    { Node name (attribute, element or such name).
      Expressed as an 8-bit string (in UTF-8 encoding), contrary to the NodeName
      from FPC DOM unit that is a WideString (DOMString). }
    function NodeName8: string;

    { Node value (like an attribute value).
      Expressed as an 8-bit string (in UTF-8 encoding), contrary to the NodeValue
      from FPC DOM unit that is a WideString (DOMString). }
    property NodeValue8: string read GetNodeValue8 write SetNodeValue8;
  end;

  TDOMCharacterDataHelper = class helper for TDOMCharacterData
    { String data.
      Expressed as an 8-bit string (in UTF-8 encoding), contrary to the Data
      from FPC DOM unit that is a WideString (DOMString). }
    function Data8: string;
  end;

  TDOMElementHelper = class helper for TDOMElement
  public

    { ------------------------------------------------------------------------
      Get an optional attribute to a "var" parameter, returns if found. }

    { Read from Element attribute value and returns @true,
      or (if there is no such attribute) returns @false
      and does not modify Value. Value is a "var", not "out" param,
      because in the latter case it's guaranteed that the old Value
      will not be cleared.

      Note that the returned Value may be empty, even when this returns @true,
      if the value is explicitly set to empty in XML (by @code(xxx="") in XML). }
    function AttributeString(const AttrName: string; var Value: string): boolean; overload;

    { Read from Element attribute value as URL and returns @true,
      or (if there is no such attribute) returns @false
      and does not modify Value.

      Returned URL is always absolute. The value in file may be a relative URL,
      it is resolved with respect to BaseUrl, that must be absolute. }
    function AttributeURL(const AttrName: string; const BaseUrl: string; var URL: string): boolean; overload;

    { Read from Element attribute value as Cardinal and returns @true,
      or (if there is no such attribute) returns @false
      and does not modify Value. }
    function AttributeCardinal(const AttrName: string; var Value: Cardinal): boolean; overload;

    { Read from Element attribute value as Integer and returns @true,
      or (if there is no such attribute) returns @false
      and does not modify Value. }
    function AttributeInteger(const AttrName: string; var Value: Integer): boolean; overload;

    { Read from Element attribute value as Int64 and returns @true,
      or (if there is no such attribute) returns @false
      and does not modify Value. }
    function AttributeInt64(const AttrName: string; var Value: Int64): boolean; overload;

    { Read from Element attribute value as Single and returns @true,
      or (if there is no such attribute) returns @false
      and does not modify Value. }
    function AttributeSingle(const AttrName: string; var Value: Single): boolean; overload;

    { Read from Element attribute value as Float and returns @true,
      or (if there is no such attribute) returns @false
      and does not modify Value.

      Note: for powerful reading of float expressions,
      consider using @code(AttributeFloatExpression) instead of @code(AttributeFloat).
      It can read expressions like @code("3.0 * 2.0") or @code("sin(2.0)").
      Use CastleScriptXML unit to introduce
      necessary class helper for this, see @link(TDOMElementScriptHelper.AttributeFloatExpression). }
    function AttributeFloat(const AttrName: string; var Value: Float): boolean; overload;

    { Read from Element attribute value as Boolean and returns @true,
      or (if there is no such attribute) returns @false
      and does not modify Value.

      A boolean value is interpreted just like FPC's TXMLConfig
      objects: true is designated by word @code(true), false by word
      @code(false), case is ignored.
      If attribute exists but it's value
      is not @code(true) or @code(false), then returns @false and doesn't
      modify Value paramater. So behaves just like the attribute didn't exist. }
    function AttributeBoolean(const AttrName: string; var Value: boolean): boolean; overload;

    { Read from Element attribute value as color and returns @true,
      or (if there is no such attribute) returns @false
      and does not modify Value. }
    function AttributeColor(const AttrName: string; var Value: TCastleColor): boolean; overload;

    { Read from Element attribute value as RGB color and returns @true,
      or (if there is no such attribute) returns @false
      and does not modify Value. }
    function AttributeColorRGB(const AttrName: string; var Value: TCastleColorRGB): boolean; overload;

    { Read from Element attribute as a 2D vector (2 floats), and returns @true,
      or (if there is no such attribute) returns @false
      and does not modify Value.

      @raises EConvertError If the attribute exists in XML, but has invalid format. }
    function AttributeVector2(const AttrName: string; var Value: TVector2): boolean; overload;

    { Read from Element attribute as a 3D vector (3 floats), and returns @true,
      or (if there is no such attribute) returns @false
      and does not modify Value.

      @raises EConvertError If the attribute exists in XML, but has invalid format. }
    function AttributeVector3(const AttrName: string; var Value: TVector3): boolean; overload;

    { ------------------------------------------------------------------------
      Get a required attribute, returns value (exception if not found). }

    { Retrieves from Element given attribute as a string,
      raises EDOMAttributeMissing if missing.

      Note that the attribute is required, but it's value may still be empty
      if it's explicitly set to empty in XML (by @code(xxx="") in XML).
      This is different than TCastleConfig.GetStringNonEmpty method,
      that serves a similar purpose for TCastleConfig, but it requires
      @italic(non-empty value) exists. Here, we only require that the value
      exists, but it may still be empty.

      @raises EDOMAttributeMissing }
    function AttributeString(const AttrName: string): string; overload;

    { Retrieves from Element given attribute as an absolute URL,
      raises EDOMAttributeMissing if missing.
      Returned URL is always absolute. The value in file may be a relative URL,
      it is resolved with respect to BaseUrl, that must be absolute.
      @raises EDOMAttributeMissing }
    function AttributeURL(const AttrName: string; const BaseUrl: string): string; overload;

    { Retrieves from Element given attribute as a Cardinal,
      raises EDOMAttributeMissing if missing.
      @raises EDOMAttributeMissing }
    function AttributeCardinal(const AttrName: string): Cardinal; overload;

    { Retrieves from Element given attribute as an Integer,
      raises EDOMAttributeMissing if missing.
      @raises EDOMAttributeMissing }
    function AttributeInteger(const AttrName: string): Integer; overload;

    { Retrieves from Element given attribute as an Int64,
      raises EDOMAttributeMissing if missing.
      @raises EDOMAttributeMissing }
    function AttributeInt64(const AttrName: string): Int64; overload;

    { Retrieves from Element given attribute as a Single,
      raises EDOMAttributeMissing if missing.
      @raises EDOMAttributeMissing }
    function AttributeSingle(const AttrName: string): Single; overload;

    { Retrieves from Element given attribute as a Float,
      raises EDOMAttributeMissing if missing.

      Note: for powerful reading of float expressions,
      consider using @code(AttributeFloatExpression) instead of @code(AttributeFloat).
      It can read expressions like @code("3.0 * 2.0") or @code("sin(2.0)").
      Use CastleScriptXML unit to introduce
      necessary class helper for this, see @link(TDOMElementScriptHelper.AttributeFloatExpression).

      @raises EDOMAttributeMissing }
    function AttributeFloat(const AttrName: string): Float; overload;

    { Retrieves from Element given attribute as a boolean,
      raises EDOMAttributeMissing if missing or has invalid value.
      A boolean value is interpreted just like FPC's TXMLConfig
      objects: true is designated by word @code(true), false by word
      @code(false), case is ignored.

      If attribute exists but it's value
      is not @code(true) or @code(false), then raises EDOMAttributeMissing.
      So behaves just like the attribute didn't exist.

      @raises EDOMAttributeMissing }
    function AttributeBoolean(const AttrName: string): boolean; overload;

    { Retrieves from Element given attribute as a color,
      raises EDOMAttributeMissing if missing or has invalid format.
      @raises EDOMAttributeMissing }
    function AttributeColor(const AttrName: string): TCastleColor; overload;

    { Retrieves from Element given attribute as an RGB color,
      raises EDOMAttributeMissing if missing or has invalid format.
      @raises EDOMAttributeMissing }
    function AttributeColorRGB(const AttrName: string): TCastleColorRGB; overload;

    { Retrieves from Element given attribute as a 2D vector (2 floats),
      raises EDOMAttributeMissing if missing or has invalid format.
      @raises EDOMAttributeMissing }
    function AttributeVector2(const AttrName: string): TVector2; overload;

    { Retrieves from Element given attribute as a 3D vector (3 floats),
      raises EDOMAttributeMissing if missing or has invalid format.
      @raises EDOMAttributeMissing }
    function AttributeVector3(const AttrName: string): TVector3; overload;

    { ------------------------------------------------------------------------
      Get an optional attribute, returns attribute or a default value. }

    { Retrieves from Element given attribute as a string, or a default value
      if the attribute was not explicitly given. }
    function AttributeStringDef(const AttrName: string; const DefaultValue: string): string;

    { Retrieves from Element given attribute as a Cardinal, or a default value. }
    function AttributeCardinalDef(const AttrName: string; const DefaultValue: Cardinal): Cardinal;

    { Retrieves from Element given attribute as an Integer, or a default value. }
    function AttributeIntegerDef(const AttrName: string; const DefaultValue: Integer): Integer;

    { Retrieves from Element given attribute as an Int64, or a default value. }
    function AttributeInt64Def(const AttrName: string; const DefaultValue: Int64): Int64;

    { Retrieves from Element given attribute as a Single, or a default value. }
    function AttributeSingleDef(const AttrName: string; const DefaultValue: Single): Single;

    { Retrieves from Element given attribute as a Float, or a default value.

      Note: for powerful reading of float expressions,
      consider using @code(AttributeFloatExpressionDef) instead of @code(AttributeFloatDef).
      It can read expressions like @code("3.0 * 2.0") or @code("sin(2.0)").
      Use CastleScriptXML unit to introduce
      necessary class helper for this, see @link(TDOMElementScriptHelper.AttributeFloatExpressionDef). }
    function AttributeFloatDef(const AttrName: string; const DefaultValue: Float): Float;

    { Retrieves from Element given attribute as a boolean,
      returns a default value if missing or has invalid value. }
    function AttributeBooleanDef(const AttrName: string; const DefaultValue: boolean): boolean;

    { Retrieves from Element given attribute as a color, or a default value. }
    function AttributeColorDef(const AttrName: string; const DefaultValue: TCastleColor): TCastleColor;

    { Retrieves from Element given attribute as an RGB color, or a default value. }
    function AttributeColorRGBDef(const AttrName: string; const DefaultValue: TCastleColorRGB): TCastleColorRGB;

    { Retrieves from Element given attribute as a 2D vector (2 floats), or a default value.
      @raises EConvertError If the value exists in XML, but has invalid format. }
    function AttributeVector2Def(const AttrName: string; const DefaultValue: TVector2): TVector2;

    { Retrieves from Element given attribute as a 3D vector (3 floats), or a default value.
      @raises EConvertError If the value exists in XML, but has invalid format. }
    function AttributeVector3Def(const AttrName: string; const DefaultValue: TVector3): TVector3;

    { Attribute setting ------------------------------------------------------ }

    { Set the attribute as string. Equivalent to standard SetAttribute in DOM unit,
      but provided here for consistency with other AttributeSet overloads. }
    procedure AttributeSet(const AttrName: string; const Value: string); overload;

    { Set the attribute as boolean,
      such that it's readable back by @link(AttributeBoolean) and @link(AttributeBooleanDef). }
    procedure AttributeSet(const AttrName: string; const Value: boolean); overload;

    { Set the attribute as Integer,
      such that it's readable back by @link(AttributeInteger) and @link(AttributeIntegerDef). }
    procedure AttributeSet(const AttrName: string; const Value: Integer); overload;

    { Set the attribute as Int64,
      such that it's readable back by @link(AttributeInt64) and @link(AttributeInt64Def). }
    procedure AttributeSet(const AttrName: string; const Value: Int64); overload;

    { Set the attribute as Cardinal,
      such that it's readable back by @link(AttributeCardinal) and @link(AttributeCardinalDef). }
    procedure AttributeSet(const AttrName: string; const Value: Cardinal); overload;

    { Set the attribute as Int64,
      such that it's readable back by @link(AttributeSingle) and @link(AttributeSingleDef). }
    procedure AttributeSet(const AttrName: string; const Value: Single); overload;

    { Other methods ---------------------------------------------------------- }

    { Get child element with given ChildName.

      For example use @code(LevelElement.ChildElement('items'))
      to get the <items> element within <level> element, as in example below.

      @preformatted(
        <level>
          <creatures>
            ...
          </creatures>
          <items>
            ...
          </items>
        </level>
      )

      There must be @bold(one and only one child element with this name).
      In case there's zero, or more than one such element,
      we will raise EDOMChildElementError (if Required is @true, default)
      or return @nil (if Required is @false).

      @raises(EDOMChildElementError
        If child not found (or found more than once), and Required = @true.)  }
    function ChildElement(const ChildName: string; const Required: boolean = true): TDOMElement;

    { Iterator over all children elements. Use like this:

      @longCode(#
      var
        I: TXMLElementIterator;
      begin
        I := Element.ChildrenIterator;
        try
          while I.GetNext do
          begin
            // ... here goes your code to process I.Current ...
          end;
        finally FreeAndNil(I) end;
      end;
      #) }
    function ChildrenIterator: TXMLElementIterator; overload;

    { Iterator over all children elements named ChildName. Use like this:

      @longCode(#
      var
        I: TXMLElementIterator;
      begin
        I := Element.ChildrenIterator('item');
        try
          while I.GetNext do
          begin
            // ... here goes your code to process I.Current ...
          end;
        finally FreeAndNil(I) end;
      end;
      #) }
    function ChildrenIterator(const ChildName: string): TXMLElementIterator; overload;

    { The text data contained in this element.

      This is suitable if an element is supposed to contain only some text.
      Like @code(<some_value>This is a text inside.</some_value>).
      It raises an error if an element contains anything else as child.

      If there are no text data nodes, e.g. if the element is empty,
      it returns empty string without raising any error. }
    function TextData: string;

    { Tag name (element name).
      Expressed as an 8-bit string (in UTF-8 encoding), contrary to the TagName
      from FPC DOM unit that is a WideString (DOMString). }
    function TagName8: string;
  end;

  { Iterate over all children elements of given XML element.

    Without this, typical iteration looks like

    @longCode(#
    var
      Index: Integer;
      ChildrenList: TDOMNodeList;
      ChildNode: TDOMNode;
      ChildElement: TDOMElement;
    begin
      ChildrenList := Element.ChildNodes;

      for Index := 0 to ChildrenList.Count - 1 do
      begin
        ChildNode := ChildrenList.Item[Index];
        if ChildNode.NodeType = ELEMENT_NODE then
        begin
          ChildElement := ChildNode as TDOMElement;
          ... here goes your code to process ChildElement ...
        end;
      end;
    end;
    #)

    ... which is an easy code, but it becomes tiresome
    to write this over and over again, especially
    for units that heavily process XML (like X3D XML or Collada readers).
    So this class allows you to write instead

    @longCode(#
    var
      I: TXMLElementIterator;
    begin
      I := Element.ChildrenIterator;
      try
        while I.GetNext do
        begin
          ... here goes your code to process I.Current ...
        end;
      finally FreeAndNil(I) end;
    end;
    #) }
  TXMLElementIterator = class
  private
    ChildNodes: TDOMNodeList;
    ChildIndex: Integer;
    FCurrent: TDOMElement;
  public
    constructor Create(ParentElement: TDOMElement);
    function GetNext: boolean; virtual;
    property Current: TDOMElement read FCurrent;
    procedure Rewind;
    function Count: Integer;
  end;

  { Iterate over children elements of given XML element, that have matching TagName. }
  TXMLElementFilteringIterator = class(TXMLElementIterator)
  private
    FTagName: DOMString;
  public
    constructor Create(ParentElement: TDOMElement; const TagName: string);
    function GetNext: boolean; override;
  end;

  { Iterate over all CDATA nodes of given XML element.

    Simple usage:

    @longCode(#
    var
      I: TXMLCDataIterator;
    begin
      I := TXMLCDataIterator.Create(Element);
      try
        while I.GetNext do
        begin
          ... here goes your code to process I.Current ...
        end;
      finally FreeAndNil(I) end;
    end;
    #) }
  TXMLCDataIterator = class
  private
    ChildNodes: TDOMNodeList;
    ChildIndex: Integer;
    FCurrent: string;
  public
    constructor Create(ParentElement: TDOMElement);
    destructor Destroy; override;
    function GetNext: boolean;
    property Current: string read FCurrent;
  end;

{ Retrieves from Element attribute Value and returns @true,
  or (if there is no such attribute) returns @false
  and does not modify Value. Value is a "var", not "out" param,
  because in the latter case it's guaranteed that the old Value
  will not be cleared.

  @deprecated Deprecated, use Element.AttributeString instead. }
function DOMGetAttribute(const Element: TDOMElement;
  const AttrName: string; var Value: string): boolean;
  deprecated 'use helper method AttributeString on TDOMElement';

{ Like DOMGetAttribute, but reads Cardinal value.

  @deprecated Deprecated, use Element.AttributeCardinal instead. }
function DOMGetCardinalAttribute(const Element: TDOMElement;
  const AttrName: string; var Value: Cardinal): boolean;
  deprecated 'use helper method AttributeCardinal on TDOMElement';

{ Like DOMGetAttribute, but reads Integer value.

  @deprecated Deprecated, use Element.AttributeInteger instead. }
function DOMGetIntegerAttribute(const Element: TDOMElement;
  const AttrName: string; var Value: Integer): boolean;
  deprecated 'use helper method AttributeInteger on TDOMElement';

{ Like DOMGetAttribute, but reads Single value.

  @deprecated Deprecated, use Element.AttributeSingle instead. }
function DOMGetSingleAttribute(const Element: TDOMElement;
  const AttrName: string; var Value: Single): boolean;
  deprecated 'use helper method AttributeSingle on TDOMElement';

{ Like DOMGetAttribute, but reads Float value.

  @deprecated Deprecated, use Element.AttributeFloat instead. }
function DOMGetFloatAttribute(const Element: TDOMElement;
  const AttrName: string; var Value: Float): boolean;
  deprecated 'use helper method AttributeFloat on TDOMElement';

{ Like DOMGetAttribute, but reads Boolean value.
  A boolean value is interpreted just like FPC's TXMLConfig
  objects: true is designated by word @code(true), false by word
  @code(false), case is ignored.

  If attribute exists but it's value
  is not @code(true) or @code(false), then returns @false and doesn't
  modify Value paramater. So behaves just like the attribute didn't exist.

  @deprecated Deprecated, use Element.AttributeBoolean instead. }
function DOMGetBooleanAttribute(const Element: TDOMElement;
  const AttrName: string; var Value: boolean): boolean;
  deprecated 'use helper method AttributeBoolean on TDOMElement';

{ Returns the @italic(one and only) child element of this Element.
  If given Element has none or more than one child elements,
  returns @nil. This is handy for parsing XML in cases when you
  know that given element must contain exactly one other element
  in correct XML file. }
function DOMGetOneChildElement(const Element: TDOMElement): TDOMElement;
  deprecated 'This method did not prove to be of much use, and it only clutters the API. Don''t use, or show us a convincing usecase when this is sensible.';

function DOMGetChildElement(const Element: TDOMElement;
  const ChildName: string; RaiseOnError: boolean): TDOMElement;
  deprecated 'use TDOMElement helper called ChildElement';

function DOMGetTextData(const Element: TDOMElement): string;
  deprecated 'use TDOMElement helper called TextData';

{ Gets a child of Element named ChildName, and gets text data within
  this child.

  This is just a shortcut for @code(DOMGetTextData(DOMGetChildElement(Element,
  ChildName, true))).

  @raises(EDOMChildElementError
    If child not found or found more than once and RaiseOnError) }
function DOMGetTextChild(const Element: TDOMElement;
  const ChildName: string): string;
  deprecated 'This method did not prove to be of much use, and it only clutters the API. Don''t use, or show us a convincing usecase when this is sensible.';

{ If needed, free result of TDOMElement.ChildNodes.

  This abstracts FPC DOM unit differences:
  @unorderedList(
    @item(
      For FPC <= 2.2.x, it was needed to call ChildNodes.Release when you're
      done with them.)

    @item(
      In newer FPC, you do not have to free nodes at all (since FPC rev 13143),
      and their Release method doesn't exist (since rev 13113).)
  ) }
procedure FreeChildNodes(const ChildNodes: TDOMNodeList);
  deprecated 'this is useless since a long time (FPC >= 2.4.x), you can remove this a the engine does not support older FPC versions anyway';

{ Replacements for standard ReadXMLFile and WriteXMLFile that operate on URLs.
  Optionally they can encrypt / decrypt content using BlowFish.
  @groupBegin }
procedure URLReadXML(out Doc: TXMLDocument; const URL: String); overload;
procedure URLReadXML(out Doc: TXMLDocument; const URL: String; const BlowFishKeyPhrase: string); overload;
function URLReadXML(const URL: String): TXMLDocument; overload;
function URLReadXML(const URL: String; const BlowFishKeyPhrase: string): TXMLDocument; overload;
procedure URLWriteXML(Doc: TXMLDocument; const URL: String); overload;
procedure URLWriteXML(Doc: TXMLDocument; const URL: String; const BlowFishKeyPhrase: string); overload;
{ @groupEnd }

implementation

uses Classes, XMLRead, XMLWrite, BlowFish,
  CastleURIUtils, CastleClassUtils;

{ TDOMNodeHelper ------------------------------------------------------------- }

function TDOMNodeHelper.NodeName8: string;
begin
  Result := UTF8Encode(NodeName);
end;

function TDOMNodeHelper.GetNodeValue8: string;
begin
  Result := UTF8Encode(NodeValue);
end;

procedure TDOMNodeHelper.SetNodeValue8(const S: string);
begin
  NodeValue := UTF8Decode(S);
end;

{ TDOMCharacterDataHelper ---------------------------------------------------- }

function TDOMCharacterDataHelper.Data8: string;
begin
  Result := UTF8Encode(Data);
end;

{ ----------------------------------------------------------------------------
  TDOMElementHelper:
  Get an optional attribute to a "var" parameter, returns if found. }

function TDOMElementHelper.AttributeString(const AttrName: string; var Value: string): boolean;
var
  AttrNode: TDOMNode;
begin
  AttrNode := Attributes.GetNamedItem(UTF8decode(AttrName));
  Result := AttrNode <> nil;
  if Result then
  begin
    Check(AttrNode.NodeType = ATTRIBUTE_NODE,
      'All element attributes must have ATTRIBUTE_NODE');
    Value := UTF8Encode((AttrNode as TDOMAttr).Value);
  end;
end;

function TDOMElementHelper.AttributeURL(
  const AttrName: string; const BaseUrl: string; var URL: string): boolean;
begin
  Result := AttributeString(AttrName, URL);
  if Result then
    URL := CombineURI(BaseUrl, URL);
end;

function TDOMElementHelper.AttributeCardinal(
  const AttrName: string; var Value: Cardinal): boolean;
var
  ValueStr: string;
begin
  Result := AttributeString(AttrName, ValueStr);
  if Result then
    Value := StrToInt64(ValueStr);
end;

function TDOMElementHelper.AttributeInteger(
  const AttrName: string; var Value: Integer): boolean;
var
  ValueStr: string;
begin
  Result := AttributeString(AttrName, ValueStr);
  if Result then
    Value := StrToInt(ValueStr);
end;

function TDOMElementHelper.AttributeInt64(
  const AttrName: string; var Value: Int64): boolean;
var
  ValueStr: string;
begin
  Result := AttributeString(AttrName, ValueStr);
  if Result then
    Value := StrToInt64(ValueStr);
end;

function TDOMElementHelper.AttributeSingle(
  const AttrName: string; var Value: Single): boolean;
var
  ValueStr: string;
begin
  Result := AttributeString(AttrName, ValueStr);
  if Result then
    Value := StrToFloat(ValueStr);
end;

function TDOMElementHelper.AttributeFloat(
  const AttrName: string; var Value: Float): boolean;
var
  ValueStr: string;
begin
  Result := AttributeString(AttrName, ValueStr);
  if Result then
    Value := StrToFloat(ValueStr);
end;

function TDOMElementHelper.AttributeBoolean(
  const AttrName: string; var Value: boolean): boolean;
var
  ValueStr: string;
begin
  Result := AttributeString(AttrName, ValueStr);
  if Result then
  begin
    if AnsiCompareText(ValueStr, 'TRUE') = 0 then
      Value := true else
    if AnsiCompareText(ValueStr, 'FALSE') = 0 then
      Value := false else
      Result := false;
  end;
end;

function TDOMElementHelper.AttributeColor(
  const AttrName: string; var Value: TCastleColor): boolean;
var
  ValueStr: string;
begin
  Result := AttributeString(AttrName, ValueStr);
  if Result then
    Value := HexToColor(ValueStr);
end;

function TDOMElementHelper.AttributeColorRGB(
  const AttrName: string; var Value: TCastleColorRGB): boolean;
var
  ValueStr: string;
begin
  Result := AttributeString(AttrName, ValueStr);
  if Result then
    Value := HexToColorRGB(ValueStr);
end;

function TDOMElementHelper.AttributeVector2(
  const AttrName: string; var Value: TVector2): boolean;
var
  ValueStr: string;
begin
  Result := AttributeString(AttrName, ValueStr);
  if Result then
    Value := Vector2FromStr(ValueStr);
end;

function TDOMElementHelper.AttributeVector3(
  const AttrName: string; var Value: TVector3): boolean;
var
  ValueStr: string;
begin
  Result := AttributeString(AttrName, ValueStr);
  if Result then
    Value := Vector3FromStr(ValueStr);
end;

{ ------------------------------------------------------------------------
  TDOMElementHelper:
  Get a required attribute, returns value (exception if not found). }

function TDOMElementHelper.AttributeString(const AttrName: string): string;
begin
  if not AttributeString(AttrName, Result) then
    raise EDOMAttributeMissing.CreateFmt('Missing required (string) attribute "%s" on element "%s"', [AttrName, TagName]);
end;

function TDOMElementHelper.AttributeURL(const AttrName: string; const BaseUrl: string): string;
begin
  if not AttributeURL(AttrName, BaseUrl, Result) then
    raise EDOMAttributeMissing.CreateFmt('Missing required (URL) attribute "%s" on element "%s"', [AttrName, TagName]);
end;

function TDOMElementHelper.AttributeCardinal(const AttrName: string): Cardinal;
begin
  if not AttributeCardinal(AttrName, Result) then
    raise EDOMAttributeMissing.CreateFmt('Missing required (unsigned integer) attribute "%s" on element "%s"', [AttrName, TagName]);
end;

function TDOMElementHelper.AttributeInteger(const AttrName: string): Integer;
begin
  if not AttributeInteger(AttrName, Result) then
    raise EDOMAttributeMissing.CreateFmt('Missing required (integer) attribute "%s" on element "%s"', [AttrName, TagName]);
end;

function TDOMElementHelper.AttributeInt64(const AttrName: string): Int64;
begin
  if not AttributeInt64(AttrName, Result) then
    raise EDOMAttributeMissing.CreateFmt('Missing required (integer 64-bit) attribute "%s" on element "%s"', [AttrName, TagName]);
end;

function TDOMElementHelper.AttributeSingle(const AttrName: string): Single;
begin
  if not AttributeSingle(AttrName, Result) then
    raise EDOMAttributeMissing.CreateFmt('Missing required (float) attribute "%s" on element "%s"', [AttrName, TagName]);
end;

function TDOMElementHelper.AttributeFloat(const AttrName: string): Float;
begin
  if not AttributeFloat(AttrName, Result) then
    raise EDOMAttributeMissing.CreateFmt('Missing required (float) attribute "%s" on element "%s"', [AttrName, TagName]);
end;

function TDOMElementHelper.AttributeBoolean(const AttrName: string): boolean;
begin
  if not AttributeBoolean(AttrName, Result) then
    raise EDOMAttributeMissing.CreateFmt('Missing (or has an invalid value) required (boolean) attribute "%s" on element "%s"', [AttrName, TagName]);
end;

function TDOMElementHelper.AttributeColor(const AttrName: string): TCastleColor;
begin
  if not AttributeColor(AttrName, Result) then
    raise EDOMAttributeMissing.CreateFmt('Missing (or has an invalid value) required (color) attribute "%s" on element "%s"', [AttrName, TagName]);
end;

function TDOMElementHelper.AttributeColorRGB(const AttrName: string): TCastleColorRGB;
begin
  if not AttributeColorRGB(AttrName, Result) then
    raise EDOMAttributeMissing.CreateFmt('Missing (or has an invalid value) required (RGB color) attribute "%s" on element "%s"', [AttrName, TagName]);
end;

function TDOMElementHelper.AttributeVector2(const AttrName: string): TVector2;
begin
  if not AttributeVector2(AttrName, Result) then
    raise EDOMAttributeMissing.CreateFmt('Missing (or has an invalid value) required (vector2) attribute "%s" on element "%s"', [AttrName, TagName]);
end;

function TDOMElementHelper.AttributeVector3(const AttrName: string): TVector3;
begin
  if not AttributeVector3(AttrName, Result) then
    raise EDOMAttributeMissing.CreateFmt('Missing (or has an invalid value) required (vector3) attribute "%s" on element "%s"', [AttrName, TagName]);
end;

{ ------------------------------------------------------------------------
  TDOMElementHelper:
  Get an optional attribute, returns attribute or a default value. }

function TDOMElementHelper.AttributeStringDef(const AttrName: string; const DefaultValue: string): string;
begin
  if not AttributeString(AttrName, Result) then
    Result := DefaultValue;
end;

function TDOMElementHelper.AttributeIntegerDef(const AttrName: string; const DefaultValue: Integer): Integer;
begin
  if not AttributeInteger(AttrName, Result) then
    Result := DefaultValue;
end;

function TDOMElementHelper.AttributeInt64Def(const AttrName: string; const DefaultValue: Int64): Int64;
begin
  if not AttributeInt64(AttrName, Result) then
    Result := DefaultValue;
end;

function TDOMElementHelper.AttributeCardinalDef(const AttrName: string; const DefaultValue: Cardinal): Cardinal;
begin
  if not AttributeCardinal(AttrName, Result) then
    Result := DefaultValue;
end;

function TDOMElementHelper.AttributeSingleDef(const AttrName: string; const DefaultValue: Single): Single;
begin
  if not AttributeSingle(AttrName, Result) then
    Result := DefaultValue;
end;

function TDOMElementHelper.AttributeFloatDef(const AttrName: string; const DefaultValue: Float): Float;
begin
  if not AttributeFloat(AttrName, Result) then
    Result := DefaultValue;
end;

function TDOMElementHelper.AttributeBooleanDef(const AttrName: string; const DefaultValue: boolean): boolean;
begin
  if not AttributeBoolean(AttrName, Result) then
    Result := DefaultValue;
end;

function TDOMElementHelper.AttributeColorDef(const AttrName: string; const DefaultValue: TCastleColor): TCastleColor;
begin
  if not AttributeColor(AttrName, Result) then
    Result := DefaultValue;
end;

function TDOMElementHelper.AttributeColorRGBDef(const AttrName: string; const DefaultValue: TCastleColorRGB): TCastleColorRGB;
begin
  if not AttributeColorRGB(AttrName, Result) then
    Result := DefaultValue;
end;

function TDOMElementHelper.AttributeVector2Def(const AttrName: string; const DefaultValue: TVector2): TVector2;
begin
  if not AttributeVector2(AttrName, Result) then
    Result := DefaultValue;
end;

function TDOMElementHelper.AttributeVector3Def(const AttrName: string; const DefaultValue: TVector3): TVector3;
begin
  if not AttributeVector3(AttrName, Result) then
    Result := DefaultValue;
end;

{ TDOMElementHelper: Attribute setting ------------------------------------------------------ }

procedure TDOMElementHelper.AttributeSet(const AttrName: string; const Value: string);
begin
  SetAttribute(UTF8Decode(AttrName), UTF8Decode(Value));
end;

procedure TDOMElementHelper.AttributeSet(const AttrName: string; const Value: boolean);
begin
  SetAttribute(UTF8Decode(AttrName), UTF8Decode(SysUtils.BoolToStr(Value, true)));
end;

procedure TDOMElementHelper.AttributeSet(const AttrName: string; const Value: Integer);
begin
  SetAttribute(UTF8Decode(AttrName), UTF8Decode(IntToStr(Value)));
end;

procedure TDOMElementHelper.AttributeSet(const AttrName: string; const Value: Int64);
begin
  SetAttribute(UTF8Decode(AttrName), UTF8Decode(IntToStr(Value)));
end;

procedure TDOMElementHelper.AttributeSet(const AttrName: string; const Value: Cardinal);
begin
  SetAttribute(UTF8Decode(AttrName), UTF8Decode(IntToStr(Value)));
end;

procedure TDOMElementHelper.AttributeSet(const AttrName: string; const Value: Single);
begin
  SetAttribute(UTF8Decode(AttrName), UTF8Decode(FloatToStr(Value)));
end;

{ ------------------------------------------------------------------------
  TDOMElementHelper: Other methods. }

function TDOMElementHelper.ChildElement(const ChildName: string;
  const Required: boolean): TDOMElement;
var
  Children: TDOMNodeList;
  Node: TDOMNode;
  I: Integer;
begin
  Result := nil;
  Children := ChildNodes;

  for I := 0 to Integer(Children.Count) - 1 do
  begin
    Node := Children.Item[I];
    if (Node.NodeType = ELEMENT_NODE) and
       ((Node as TDOMElement).TagName = UTF8Decode(ChildName)) then
    begin
      if Result = nil then
        Result := TDOMElement(Node) else
      begin
        if Required then
          raise EDOMChildElementError.CreateFmt(
            'Child "%s" occurs more than once', [ChildName]) else
          Exit(nil);
      end;
    end;
  end;

  if (Result = nil) and Required then
    raise EDOMChildElementError.CreateFmt('Child "%s" not found', [ChildName])
end;

function TDOMElementHelper.TextData: string;

{
  It concatenates all text data nodes that are direct children
  of this element. So if there are no text data nodes, it returns
  empty string without raising any error.

  AFAIK it's uncommon but
  possible to have here more than one text node. Normally, more than one
  text nodes occur because they are separated by other child elements,
  but we already eliminated this possibility (i.e. we raise error
  in this case). Still, if you operated on DOM tree, e.g. deleted
  some elements, or inserted some text nodes, then I think it's possible
  that you will have more than one text node within this element.
  So this procedure should still work OK in this case.
}

var
  Children: TDOMNodeList;
  Node: TDOMNode;
  I: Integer;
begin
  Result := '';
  Children := ChildNodes;
  for I := 0 to Integer(Children.Count) - 1 do
  begin
    Node := Children.Item[I];
    case Node.NodeType of
      TEXT_NODE: Result := Result + UTF8Encode((Node as TDOMText).Data);
      ELEMENT_NODE: raise Exception.CreateFmt(
        'Child elements not allowed within element <%s>, but found %s',
          [TagName, (Node as TDOMElement).TagName]);
    end;
  end;
end;

function TDOMElementHelper.ChildrenIterator: TXMLElementIterator;
begin
  Result := TXMLElementIterator.Create(Self);
end;

function TDOMElementHelper.ChildrenIterator(const ChildName: string): TXMLElementIterator;
begin
  Result := TXMLElementFilteringIterator.Create(Self, ChildName);
end;

function TDOMElementHelper.TagName8: string;
begin
  Result := UTF8Encode(TagName);
end;

{ TXMLElementIterator -------------------------------------------------------- }

constructor TXMLElementIterator.Create(ParentElement: TDOMElement);
begin
  inherited Create;
  ChildNodes := ParentElement.ChildNodes;
  ChildIndex := -1;
end;

procedure TXMLElementIterator.Rewind;
begin
  ChildIndex := -1;
end;

function TXMLElementIterator.Count: Integer;
begin
  Result := ChildNodes.Count;
end;

function TXMLElementIterator.GetNext: boolean;
var
  ChildNode: TDOMNode;
begin
  repeat
    Inc(ChildIndex);

    if ChildIndex >= Integer(ChildNodes.Count) then
    begin
      Result := false;
      Break;
    end else
    begin
      ChildNode := ChildNodes[ChildIndex];
      if ChildNode.NodeType = ELEMENT_NODE then
      begin
        Result := true;
        FCurrent := ChildNode as TDOMElement;
        Break;
      end;
    end;
  until false;
end;

{ TXMLElementFilteringIterator ----------------------------------------------- }

constructor TXMLElementFilteringIterator.Create(ParentElement: TDOMElement; const TagName: string);
begin
  inherited Create(ParentElement);
  FTagName := UTF8Decode(TagName);
end;

function TXMLElementFilteringIterator.GetNext: boolean;
begin
  repeat
    Result := inherited GetNext;
  until (not Result) or (Current.TagName = FTagName);
end;

{ TXMLCDataIterator -------------------------------------------------------- }

constructor TXMLCDataIterator.Create(ParentElement: TDOMElement);
begin
  inherited Create;
  ChildNodes := ParentElement.ChildNodes;
  ChildIndex := -1;
end;

destructor TXMLCDataIterator.Destroy;
begin
  inherited;
end;

function TXMLCDataIterator.GetNext: boolean;
var
  ChildNode: TDOMNode;
begin
  repeat
    Inc(ChildIndex);

    if ChildIndex >= Integer(ChildNodes.Count) then
    begin
      Result := false;
      Break;
    end else
    begin
      ChildNode := ChildNodes[ChildIndex];
      if ChildNode.NodeType = CDATA_SECTION_NODE then
      begin
        Result := true;
        FCurrent := UTF8Encode((ChildNode as TDOMCDataSection).Data);
        Break;
      end;
    end;
  until false;
end;

{ globals -------------------------------------------------------------------- }

function DOMGetAttribute(const Element: TDOMElement;
  const AttrName: string; var Value: string): boolean;
begin
  Result := Element.AttributeString(AttrName, Value);
end;

function DOMGetCardinalAttribute(const Element: TDOMElement;
  const AttrName: string; var Value: Cardinal): boolean;
begin
  Result := Element.AttributeCardinal(AttrName, Value);
end;

function DOMGetIntegerAttribute(const Element: TDOMElement;
  const AttrName: string; var Value: Integer): boolean;
begin
  Result := Element.AttributeInteger(AttrName, Value);
end;

function DOMGetSingleAttribute(const Element: TDOMElement;
  const AttrName: string; var Value: Single): boolean;
begin
  Result := Element.AttributeSingle(AttrName, Value);
end;

function DOMGetFloatAttribute(const Element: TDOMElement;
  const AttrName: string; var Value: Float): boolean;
begin
  Result := Element.AttributeFloat(AttrName, Value);
end;

function DOMGetBooleanAttribute(const Element: TDOMElement;
  const AttrName: string; var Value: boolean): boolean;
begin
  Result := Element.AttributeBoolean(AttrName, Value);
end;

function DOMGetChildElement(const Element: TDOMElement;
  const ChildName: string; RaiseOnError: boolean): TDOMElement;
begin
  Result := Element.ChildElement(ChildName, RaiseOnError);
end;

function DOMGetTextData(const Element: TDOMElement): string;
begin
  Result := Element.TextData;
end;

function DOMGetOneChildElement(const Element: TDOMElement): TDOMElement;
var
  Children: TDOMNodeList;
  Node: TDOMNode;
  I: Integer;
begin
  Result := nil;
  Children := Element.ChildNodes;
  for I := 0 to Integer(Children.Count) - 1 do
  begin
    Node := Children.Item[I];
    if Node.NodeType = ELEMENT_NODE then
    begin
      if Result = nil then
        Result := Node as TDOMElement else
      begin
        { More than one element in Children. }
        Result := nil;
        Exit;
      end;
    end;
  end;
end;

function DOMGetTextChild(const Element: TDOMElement;
  const ChildName: string): string;
begin
  Result := Element.ChildElement(ChildName).TextData;
end;

procedure FreeChildNodes(const ChildNodes: TDOMNodeList);
begin
  {$ifdef VER2_0} ChildNodes.Release; {$endif}
  {$ifdef VER2_1} ChildNodes.Release; {$endif}
  {$ifdef VER2_2} ChildNodes.Release; {$endif}
end;

procedure URLReadXML(out Doc: TXMLDocument; const URL: String; const BlowFishKeyPhrase: string);
var
  Stream: TStream;
  DecryptStream: TBlowFishDecryptStream;
  DecryptedCorrectStream: TStringStream;
  L: Integer;
  DecryptedContent: string;
begin
  Doc := nil; // clean "out" param at start, just like ReadXMLFile
  Stream := Download(URL, []);
  try
    DecryptStream := TBlowFishDecryptStream.Create(BlowFishKeyPhrase, Stream);
    try
      { TBlowFishDecryptStream (or maybe encryption?) adds zeros at the end.
        Cut them off. }
      DecryptedContent := ReadGrowingStreamToString(DecryptStream);
      L := Length(DecryptedContent);
      while (L > 0) and (DecryptedContent[L] = #0) do
        Dec(L);
      SetLength(DecryptedContent, L);
      DecryptedCorrectStream := TStringStream.Create(DecryptedContent);
      try
        ReadXMLFile(Doc, DecryptedCorrectStream);
      finally FreeAndNil(DecryptedCorrectStream) end;
    finally FreeAndNil(DecryptStream) end;
  finally FreeAndNil(Stream) end;
end;

procedure URLReadXML(out Doc: TXMLDocument; const URL: String);
var
  Stream: TStream;
  StreamOptions: TStreamOptions;
  Gzipped: boolean;
begin
  Doc := nil; // clean "out" param at start, just like ReadXMLFile

  //guess gzipped based on file extension
  StreamOptions := [];
  URIMimeType(URL, Gzipped);
  if Gzipped then
    Include(StreamOptions, soGzip);

  Stream := Download(URL, StreamOptions);
  try
    ReadXMLFile(Doc, Stream);
  finally FreeAndNil(Stream) end;
end;

function URLReadXML(const URL: String): TXMLDocument;
begin
  try
    // URLReadXML and ReadXMLFile nil the parameter when there's no need to free it
    URLReadXML(Result, URL);     //this one will automatically take care of gzipping
  except FreeAndNil(Result); raise; end;
end;

function URLReadXML(const URL: String; const BlowFishKeyPhrase: string): TXMLDocument;
begin
  try
    // URLReadXML and ReadXMLFile nil the parameter when there's no need to free it
    URLReadXML(Result, URL, BlowFishKeyPhrase);
  except FreeAndNil(Result); raise; end;
end;

procedure URLWriteXML(Doc: TXMLDocument; const URL: String; const BlowFishKeyPhrase: string);
var
  Stream: TStream;
  EncryptStream: TBlowFishEncryptStream;
begin
  Stream := URLSaveStream(URL);
  try
    EncryptStream := TBlowFishEncryptStream.Create(BlowFishKeyPhrase, Stream);
    try
      WriteXMLFile(Doc, EncryptStream);
    finally FreeAndNil(EncryptStream) end;
  finally FreeAndNil(Stream) end;
end;

procedure URLWriteXML(Doc: TXMLDocument; const URL: String);
var
  Stream: TStream;
  StreamOptions: TSaveStreamOptions;
  gzipped: boolean;
begin
  //guess gzipped based on file extension
  StreamOptions := [];
  URIMimeType(URL, Gzipped);
  if Gzipped then
    Include(StreamOptions, ssoGzip);

  Stream := URLSaveStream(URL, StreamOptions);
  try
    WriteXMLFile(Doc, Stream);
  finally FreeAndNil(Stream) end;
end;

end.