Ignore:
Timestamp:
Jun 4, 2024, 12:22:49 AM (5 months ago)
Author:
chronos
Message:
  • Modified: Removed U prefix from unit names.
  • Modified: Updated Common package.
File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/XML.pas

    r74 r75  
    1 unit UXMLUtils;
    2 
    3 {$mode delphi}
     1unit XML;
    42
    53interface
     
    75uses
    86  {$IFDEF WINDOWS}Windows,{$ENDIF}
    9   Classes, SysUtils, DateUtils;
     7  Classes, SysUtils, DateUtils, DOM, xmlread;
    108
    119function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
    12 function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): WideString;
     10function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): string;
     11procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
     12procedure WriteInt64(Node: TDOMNode; Name: string; Value: Int64);
     13procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
     14procedure WriteString(Node: TDOMNode; Name: string; Value: string);
     15procedure WriteDateTime(Node: TDOMNode; Name: string; Value: TDateTime);
     16procedure WriteDouble(Node: TDOMNode; Name: string; Value: Double);
     17function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
     18function ReadInt64(Node: TDOMNode; Name: string; DefaultValue: Int64): Int64;
     19function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
     20function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
     21function ReadDateTime(Node: TDOMNode; Name: string; DefaultValue: TDateTime): TDateTime;
     22function ReadDouble(Node: TDOMNode; Name: string; DefaultValue: Double): Double;
     23procedure ReadXMLFileParser(out Doc: TXMLDocument; FileName: string);
    1324
    1425
    1526implementation
     27
     28function ReadDouble(Node: TDOMNode; Name: string; DefaultValue: Double): Double;
     29var
     30  NewNode: TDOMNode;
     31begin
     32  Result := DefaultValue;
     33  NewNode := Node.FindNode(DOMString(Name));
     34  if Assigned(NewNode) then
     35    Result := StrToFloat(string(NewNode.TextContent));
     36end;
     37
     38procedure ReadXMLFileParser(out Doc: TXMLDocument; FileName: string);
     39var
     40  Parser: TDOMParser;
     41  Src: TXMLInputSource;
     42  InFile: TFileStream;
     43begin
     44  try
     45    InFile := TFileStream.Create(FileName, fmOpenRead);
     46    Src := TXMLInputSource.Create(InFile);
     47    Parser := TDOMParser.Create;
     48    Parser.Options.PreserveWhitespace := True;
     49    Parser.Parse(Src, Doc);
     50  finally
     51    Src.Free;
     52    Parser.Free;
     53    InFile.Free;
     54  end;
     55end;
    1656
    1757function GetTimeZoneBias: Integer;
     
    2060  TimeZoneInfo: TTimeZoneInformation;
    2161begin
     62  {$push}{$warn 5057 off}
    2263  case GetTimeZoneInformation(TimeZoneInfo) of
    23   TIME_ZONE_ID_STANDARD: Result := TimeZoneInfo.Bias + TimeZoneInfo.StandardBias;
    24   TIME_ZONE_ID_DAYLIGHT: Result := TimeZoneInfo.Bias + TimeZoneInfo.DaylightBias;
     64    TIME_ZONE_ID_STANDARD: Result := TimeZoneInfo.Bias + TimeZoneInfo.StandardBias;
     65    TIME_ZONE_ID_DAYLIGHT: Result := TimeZoneInfo.Bias + TimeZoneInfo.DaylightBias;
    2566  else
    2667    Result := 0;
    2768  end;
     69  {$pop}
    2870end;
    2971{$ELSE}
     
    3577function LeftCutString(var Source: string; out Output: string; Delimiter: string; Allowed: string = ''): Boolean;
    3678var
    37   I, J: Integer;
     79  I: Integer;
    3880  Matched: Boolean;
    3981begin
     
    66108  Minute: Integer;
    67109  Second: Integer;
     110  SecondFraction: Double;
    68111  Millisecond: Integer;
    69112begin
     
    88131      if Pos('Z', XMLDateTime) > 0 then
    89132        LeftCutString(XMLDateTime, Part, 'Z');
    90       Millisecond := StrToInt(Part);
     133      SecondFraction := StrToFloat('0' + DefaultFormatSettings.DecimalSeparator + Part);
     134      Millisecond := Trunc(SecondFraction * 1000);
    91135    end else begin
    92136      if Pos('+', XMLDateTime) > 0 then
     
    106150end;
    107151
    108 function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): WideString;
     152function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): string;
    109153const
    110154  Neg: array[Boolean] of string =  ('+', '-');
     
    123167end;
    124168
     169procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
     170var
     171  NewNode: TDOMNode;
     172begin
     173  NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
     174  NewNode.TextContent := DOMString(IntToStr(Value));
     175  Node.AppendChild(NewNode);
     176end;
     177
     178procedure WriteInt64(Node: TDOMNode; Name: string; Value: Int64);
     179var
     180  NewNode: TDOMNode;
     181begin
     182  NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
     183  NewNode.TextContent := DOMString(IntToStr(Value));
     184  Node.AppendChild(NewNode);
     185end;
     186
     187procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
     188var
     189  NewNode: TDOMNode;
     190begin
     191  NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
     192  NewNode.TextContent := DOMString(BoolToStr(Value));
     193  Node.AppendChild(NewNode);
     194end;
     195
     196procedure WriteString(Node: TDOMNode; Name: string; Value: string);
     197var
     198  NewNode: TDOMNode;
     199begin
     200  NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
     201  NewNode.TextContent := DOMString(Value);
     202  Node.AppendChild(NewNode);
     203end;
     204
     205procedure WriteDateTime(Node: TDOMNode; Name: string; Value: TDateTime);
     206var
     207  NewNode: TDOMNode;
     208begin
     209  NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
     210  NewNode.TextContent := DOMString(DateTimeToXMLTime(Value));
     211  Node.AppendChild(NewNode);
     212end;
     213
     214procedure WriteDouble(Node: TDOMNode; Name: string; Value: Double);
     215var
     216  NewNode: TDOMNode;
     217begin
     218  NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
     219  NewNode.TextContent := DOMString(FloatToStr(Value));
     220  Node.AppendChild(NewNode);
     221end;
     222
     223function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
     224var
     225  NewNode: TDOMNode;
     226begin
     227  Result := DefaultValue;
     228  NewNode := Node.FindNode(DOMString(Name));
     229  if Assigned(NewNode) then
     230    Result := StrToInt(string(NewNode.TextContent));
     231end;
     232
     233function ReadInt64(Node: TDOMNode; Name: string; DefaultValue: Int64): Int64;
     234var
     235  NewNode: TDOMNode;
     236begin
     237  Result := DefaultValue;
     238  NewNode := Node.FindNode(DOMString(Name));
     239  if Assigned(NewNode) then
     240    Result := StrToInt64(string(NewNode.TextContent));
     241end;
     242
     243function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
     244var
     245  NewNode: TDOMNode;
     246begin
     247  Result := DefaultValue;
     248  NewNode := Node.FindNode(DOMString(Name));
     249  if Assigned(NewNode) then
     250    Result := StrToBool(string(NewNode.TextContent));
     251end;
     252
     253function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
     254var
     255  NewNode: TDOMNode;
     256begin
     257  Result := DefaultValue;
     258  NewNode := Node.FindNode(DOMString(Name));
     259  if Assigned(NewNode) then
     260    Result := string(NewNode.TextContent);
     261end;
     262
     263function ReadDateTime(Node: TDOMNode; Name: string; DefaultValue: TDateTime
     264  ): TDateTime;
     265var
     266  NewNode: TDOMNode;
     267begin
     268  Result := DefaultValue;
     269  NewNode := Node.FindNode(DOMString(Name));
     270  if Assigned(NewNode) then
     271    Result := XMLTimeToDateTime(string(NewNode.TextContent));
     272end;
     273
    125274end.
    126 
Note: See TracChangeset for help on using the changeset viewer.