| 1 | unit XML;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | {$IFDEF WINDOWS}Windows,{$ENDIF}
|
|---|
| 7 | Classes, SysUtils, DateUtils, DOM, xmlread;
|
|---|
| 8 |
|
|---|
| 9 | function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
|
|---|
| 10 | function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): string;
|
|---|
| 11 | procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
|
|---|
| 12 | procedure WriteInt64(Node: TDOMNode; Name: string; Value: Int64);
|
|---|
| 13 | procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
|
|---|
| 14 | procedure WriteString(Node: TDOMNode; Name: string; Value: string);
|
|---|
| 15 | procedure WriteDateTime(Node: TDOMNode; Name: string; Value: TDateTime);
|
|---|
| 16 | procedure WriteDouble(Node: TDOMNode; Name: string; Value: Double);
|
|---|
| 17 | procedure WriteCurrency(Node: TDOMNode; Name: string; Value: Currency);
|
|---|
| 18 | function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
|
|---|
| 19 | function ReadInt64(Node: TDOMNode; Name: string; DefaultValue: Int64): Int64;
|
|---|
| 20 | function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
|
|---|
| 21 | function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
|
|---|
| 22 | function ReadDateTime(Node: TDOMNode; Name: string; DefaultValue: TDateTime): TDateTime;
|
|---|
| 23 | function ReadDouble(Node: TDOMNode; Name: string; DefaultValue: Double): Double;
|
|---|
| 24 | function ReadCurrency(Node: TDOMNode; Name: string; DefaultValue: Currency): Currency;
|
|---|
| 25 | procedure ReadXMLFileParser(out Doc: TXMLDocument; FileName: string);
|
|---|
| 26 |
|
|---|
| 27 | var
|
|---|
| 28 | XmlFormatSettings: TFormatSettings = (
|
|---|
| 29 | CurrencyFormat: 1;
|
|---|
| 30 | NegCurrFormat: 5;
|
|---|
| 31 | ThousandSeparator: ',';
|
|---|
| 32 | DecimalSeparator: '.';
|
|---|
| 33 | CurrencyDecimals: 2;
|
|---|
| 34 | DateSeparator: '-';
|
|---|
| 35 | TimeSeparator: ':';
|
|---|
| 36 | ListSeparator: ',';
|
|---|
| 37 | CurrencyString: '$';
|
|---|
| 38 | ShortDateFormat: 'yyyy-mm-dd';
|
|---|
| 39 | LongDateFormat: 'dd" "mmmm" "yyyy';
|
|---|
| 40 | TimeAMString: 'AM';
|
|---|
| 41 | TimePMString: 'PM';
|
|---|
| 42 | ShortTimeFormat: 'hh:nn';
|
|---|
| 43 | LongTimeFormat: 'hh:nn:ss';
|
|---|
| 44 | ShortMonthNames: ('Jan','Feb','Mar','Apr','May','Jun',
|
|---|
| 45 | 'Jul','Aug','Sep','Oct','Nov','Dec');
|
|---|
| 46 | LongMonthNames: ('January','February','March','April','May','June',
|
|---|
| 47 | 'July','August','September','October','November','December');
|
|---|
| 48 | ShortDayNames: ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
|
|---|
| 49 | LongDayNames: ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
|
|---|
| 50 | TwoDigitYearCenturyWindow: 50;
|
|---|
| 51 | );
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | implementation
|
|---|
| 55 |
|
|---|
| 56 | function ReadDouble(Node: TDOMNode; Name: string; DefaultValue: Double): Double;
|
|---|
| 57 | var
|
|---|
| 58 | NewNode: TDOMNode;
|
|---|
| 59 | begin
|
|---|
| 60 | Result := DefaultValue;
|
|---|
| 61 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 62 | if Assigned(NewNode) then
|
|---|
| 63 | Result := StrToFloat(string(NewNode.TextContent), XmlFormatSettings);
|
|---|
| 64 | end;
|
|---|
| 65 |
|
|---|
| 66 | function ReadCurrency(Node: TDOMNode; Name: string; DefaultValue: Currency
|
|---|
| 67 | ): Currency;
|
|---|
| 68 | var
|
|---|
| 69 | NewNode: TDOMNode;
|
|---|
| 70 | begin
|
|---|
| 71 | Result := DefaultValue;
|
|---|
| 72 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 73 | if Assigned(NewNode) then
|
|---|
| 74 | Result := StrToCurr(string(NewNode.TextContent), XmlFormatSettings);
|
|---|
| 75 | end;
|
|---|
| 76 |
|
|---|
| 77 | procedure ReadXMLFileParser(out Doc: TXMLDocument; FileName: string);
|
|---|
| 78 | var
|
|---|
| 79 | Parser: TDOMParser;
|
|---|
| 80 | Src: TXMLInputSource;
|
|---|
| 81 | InFile: TFileStream;
|
|---|
| 82 | begin
|
|---|
| 83 | try
|
|---|
| 84 | InFile := TFileStream.Create(FileName, fmOpenRead);
|
|---|
| 85 | Src := TXMLInputSource.Create(InFile);
|
|---|
| 86 | Parser := TDOMParser.Create;
|
|---|
| 87 | Parser.Options.PreserveWhitespace := True;
|
|---|
| 88 | Parser.Parse(Src, Doc);
|
|---|
| 89 | finally
|
|---|
| 90 | Src.Free;
|
|---|
| 91 | Parser.Free;
|
|---|
| 92 | InFile.Free;
|
|---|
| 93 | end;
|
|---|
| 94 | end;
|
|---|
| 95 |
|
|---|
| 96 | function GetTimeZoneBias: Integer;
|
|---|
| 97 | {$IFDEF WINDOWS}
|
|---|
| 98 | var
|
|---|
| 99 | TimeZoneInfo: TTimeZoneInformation;
|
|---|
| 100 | begin
|
|---|
| 101 | {$push}{$warn 5057 off}
|
|---|
| 102 | case GetTimeZoneInformation(TimeZoneInfo) of
|
|---|
| 103 | TIME_ZONE_ID_STANDARD: Result := TimeZoneInfo.Bias + TimeZoneInfo.StandardBias;
|
|---|
| 104 | TIME_ZONE_ID_DAYLIGHT: Result := TimeZoneInfo.Bias + TimeZoneInfo.DaylightBias;
|
|---|
| 105 | else
|
|---|
| 106 | Result := 0;
|
|---|
| 107 | end;
|
|---|
| 108 | {$pop}
|
|---|
| 109 | end;
|
|---|
| 110 | {$ELSE}
|
|---|
| 111 | begin
|
|---|
| 112 | Result := 0;
|
|---|
| 113 | end;
|
|---|
| 114 | {$ENDIF}
|
|---|
| 115 |
|
|---|
| 116 | function LeftCutString(var Source: string; out Output: string; Delimiter: string; Allowed: string = ''): Boolean;
|
|---|
| 117 | var
|
|---|
| 118 | I: Integer;
|
|---|
| 119 | Matched: Boolean;
|
|---|
| 120 | begin
|
|---|
| 121 | I := 1;
|
|---|
| 122 | Matched := True;
|
|---|
| 123 | while (I < Length(Source)) and Matched do begin
|
|---|
| 124 | Matched := True;
|
|---|
| 125 | if (Source[I] = Delimiter) then Matched := False;
|
|---|
| 126 | //for J := 1 to Length(Allowed) do
|
|---|
| 127 | // if Source[I] = Allowed[J] then Matched := True;
|
|---|
| 128 | if Matched then Inc(I);
|
|---|
| 129 | end;
|
|---|
| 130 | if (Delimiter = Copy(Source, I, Length(Delimiter))) or (I = Length(Source)) then begin
|
|---|
| 131 | Output := Copy(Source, 1, I - 1);
|
|---|
| 132 | Delete(Source, 1, Length(Output) + Length(Delimiter));
|
|---|
| 133 | Result := True;
|
|---|
| 134 | end else begin
|
|---|
| 135 | Output := '';
|
|---|
| 136 | Result := False;
|
|---|
| 137 | end;
|
|---|
| 138 | end;
|
|---|
| 139 |
|
|---|
| 140 | function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
|
|---|
| 141 | var
|
|---|
| 142 | Part: string;
|
|---|
| 143 | Year: Integer;
|
|---|
| 144 | Month: Integer;
|
|---|
| 145 | Day: Integer;
|
|---|
| 146 | Hour: Integer;
|
|---|
| 147 | Minute: Integer;
|
|---|
| 148 | Second: Integer;
|
|---|
| 149 | SecondFraction: Double;
|
|---|
| 150 | Millisecond: Integer;
|
|---|
| 151 | begin
|
|---|
| 152 | if LeftCutString(XMLDateTime, Part, '-') then
|
|---|
| 153 | Year := StrToInt(Part);
|
|---|
| 154 | if LeftCutString(XMLDateTime, Part, '-') then
|
|---|
| 155 | Month := StrToInt(Part);
|
|---|
| 156 | if Pos('T', XMLDateTime) > 0 then begin
|
|---|
| 157 | if LeftCutString(XMLDateTime, Part, 'T') then
|
|---|
| 158 | Day := StrToInt(Part);
|
|---|
| 159 | if LeftCutString(XMLDateTime, Part, ':') then
|
|---|
| 160 | Hour := StrToInt(Part);
|
|---|
| 161 | if LeftCutString(XMLDateTime, Part, ':') then
|
|---|
| 162 | Minute := StrToInt(Part);
|
|---|
| 163 | if Pos('.', XMLDateTime) > 0 then begin
|
|---|
| 164 | if LeftCutString(XMLDateTime, Part, '.') then
|
|---|
| 165 | Second := StrToInt(Part);
|
|---|
| 166 | if Pos('+', XMLDateTime) > 0 then
|
|---|
| 167 | LeftCutString(XMLDateTime, Part, '+') else
|
|---|
| 168 | if Pos('-', XMLDateTime) > 0 then
|
|---|
| 169 | LeftCutString(XMLDateTime, Part, '-') else
|
|---|
| 170 | if Pos('Z', XMLDateTime) > 0 then
|
|---|
| 171 | LeftCutString(XMLDateTime, Part, 'Z');
|
|---|
| 172 | SecondFraction := StrToFloat('0' + DefaultFormatSettings.DecimalSeparator + Part);
|
|---|
| 173 | Millisecond := Trunc(SecondFraction * 1000);
|
|---|
| 174 | end else begin
|
|---|
| 175 | if Pos('+', XMLDateTime) > 0 then
|
|---|
| 176 | LeftCutString(XMLDateTime, Part, '+') else
|
|---|
| 177 | if Pos('-', XMLDateTime) > 0 then
|
|---|
| 178 | LeftCutString(XMLDateTime, Part, '-') else
|
|---|
| 179 | if Pos('Z', XMLDateTime) > 0 then
|
|---|
| 180 | LeftCutString(XMLDateTime, Part, 'Z');
|
|---|
| 181 | Second := StrToInt(Part);
|
|---|
| 182 | Millisecond := 0;
|
|---|
| 183 | end;
|
|---|
| 184 | end else begin
|
|---|
| 185 | Day := StrToInt(XMLDateTime);
|
|---|
| 186 | end;
|
|---|
| 187 | Result := EncodeDateTime(Year, Month, Day, Hour, Minute, Second, Millisecond);
|
|---|
| 188 | // TODO: Correct time by zone bias
|
|---|
| 189 | end;
|
|---|
| 190 |
|
|---|
| 191 | function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): string;
|
|---|
| 192 | const
|
|---|
| 193 | Neg: array[Boolean] of string = ('+', '-');
|
|---|
| 194 | var
|
|---|
| 195 | Bias: Integer;
|
|---|
| 196 | begin
|
|---|
| 197 | Result := FormatDateTime('yyyy''-''mm''-''dd''T''hh'':''nn'':''ss''.''zzz', Value); { Do not localize }
|
|---|
| 198 | Bias := GetTimeZoneBias;
|
|---|
| 199 | if (Bias <> 0) and ApplyLocalBias then
|
|---|
| 200 | begin
|
|---|
| 201 | Result := Format('%s%s%.2d:%.2d', [Result, Neg[Bias > 0], { Do not localize }
|
|---|
| 202 | Abs(Bias) div MinsPerHour,
|
|---|
| 203 | Abs(Bias) mod MinsPerHour]);
|
|---|
| 204 | end else
|
|---|
| 205 | Result := Result + 'Z'; { Do not localize }
|
|---|
| 206 | end;
|
|---|
| 207 |
|
|---|
| 208 | procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
|
|---|
| 209 | var
|
|---|
| 210 | NewNode: TDOMNode;
|
|---|
| 211 | begin
|
|---|
| 212 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 213 | NewNode.TextContent := DOMString(IntToStr(Value));
|
|---|
| 214 | Node.AppendChild(NewNode);
|
|---|
| 215 | end;
|
|---|
| 216 |
|
|---|
| 217 | procedure WriteInt64(Node: TDOMNode; Name: string; Value: Int64);
|
|---|
| 218 | var
|
|---|
| 219 | NewNode: TDOMNode;
|
|---|
| 220 | begin
|
|---|
| 221 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 222 | NewNode.TextContent := DOMString(IntToStr(Value));
|
|---|
| 223 | Node.AppendChild(NewNode);
|
|---|
| 224 | end;
|
|---|
| 225 |
|
|---|
| 226 | procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
|
|---|
| 227 | var
|
|---|
| 228 | NewNode: TDOMNode;
|
|---|
| 229 | begin
|
|---|
| 230 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 231 | NewNode.TextContent := DOMString(BoolToStr(Value));
|
|---|
| 232 | Node.AppendChild(NewNode);
|
|---|
| 233 | end;
|
|---|
| 234 |
|
|---|
| 235 | procedure WriteString(Node: TDOMNode; Name: string; Value: string);
|
|---|
| 236 | var
|
|---|
| 237 | NewNode: TDOMNode;
|
|---|
| 238 | begin
|
|---|
| 239 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 240 | NewNode.TextContent := DOMString(Value);
|
|---|
| 241 | Node.AppendChild(NewNode);
|
|---|
| 242 | end;
|
|---|
| 243 |
|
|---|
| 244 | procedure WriteDateTime(Node: TDOMNode; Name: string; Value: TDateTime);
|
|---|
| 245 | var
|
|---|
| 246 | NewNode: TDOMNode;
|
|---|
| 247 | begin
|
|---|
| 248 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 249 | NewNode.TextContent := DOMString(DateTimeToXMLTime(Value));
|
|---|
| 250 | Node.AppendChild(NewNode);
|
|---|
| 251 | end;
|
|---|
| 252 |
|
|---|
| 253 | procedure WriteDouble(Node: TDOMNode; Name: string; Value: Double);
|
|---|
| 254 | var
|
|---|
| 255 | NewNode: TDOMNode;
|
|---|
| 256 | begin
|
|---|
| 257 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 258 | NewNode.TextContent := DOMString(FloatToStr(Value, XmlFormatSettings));
|
|---|
| 259 | Node.AppendChild(NewNode);
|
|---|
| 260 | end;
|
|---|
| 261 |
|
|---|
| 262 | procedure WriteCurrency(Node: TDOMNode; Name: string; Value: Currency);
|
|---|
| 263 | var
|
|---|
| 264 | NewNode: TDOMNode;
|
|---|
| 265 | begin
|
|---|
| 266 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 267 | NewNode.TextContent := DOMString(CurrToStr(Value, XmlFormatSettings));
|
|---|
| 268 | Node.AppendChild(NewNode);
|
|---|
| 269 | end;
|
|---|
| 270 |
|
|---|
| 271 | function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
|
|---|
| 272 | var
|
|---|
| 273 | NewNode: TDOMNode;
|
|---|
| 274 | begin
|
|---|
| 275 | Result := DefaultValue;
|
|---|
| 276 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 277 | if Assigned(NewNode) then
|
|---|
| 278 | Result := StrToInt(string(NewNode.TextContent));
|
|---|
| 279 | end;
|
|---|
| 280 |
|
|---|
| 281 | function ReadInt64(Node: TDOMNode; Name: string; DefaultValue: Int64): Int64;
|
|---|
| 282 | var
|
|---|
| 283 | NewNode: TDOMNode;
|
|---|
| 284 | begin
|
|---|
| 285 | Result := DefaultValue;
|
|---|
| 286 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 287 | if Assigned(NewNode) then
|
|---|
| 288 | Result := StrToInt64(string(NewNode.TextContent));
|
|---|
| 289 | end;
|
|---|
| 290 |
|
|---|
| 291 | function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
|
|---|
| 292 | var
|
|---|
| 293 | NewNode: TDOMNode;
|
|---|
| 294 | begin
|
|---|
| 295 | Result := DefaultValue;
|
|---|
| 296 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 297 | if Assigned(NewNode) then
|
|---|
| 298 | Result := StrToBool(string(NewNode.TextContent));
|
|---|
| 299 | end;
|
|---|
| 300 |
|
|---|
| 301 | function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
|
|---|
| 302 | var
|
|---|
| 303 | NewNode: TDOMNode;
|
|---|
| 304 | begin
|
|---|
| 305 | Result := DefaultValue;
|
|---|
| 306 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 307 | if Assigned(NewNode) then
|
|---|
| 308 | Result := string(NewNode.TextContent);
|
|---|
| 309 | end;
|
|---|
| 310 |
|
|---|
| 311 | function ReadDateTime(Node: TDOMNode; Name: string; DefaultValue: TDateTime
|
|---|
| 312 | ): TDateTime;
|
|---|
| 313 | var
|
|---|
| 314 | NewNode: TDOMNode;
|
|---|
| 315 | begin
|
|---|
| 316 | Result := DefaultValue;
|
|---|
| 317 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 318 | if Assigned(NewNode) then
|
|---|
| 319 | Result := XMLTimeToDateTime(string(NewNode.TextContent));
|
|---|
| 320 | end;
|
|---|
| 321 |
|
|---|
| 322 | end.
|
|---|