| 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 | function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
|
|---|
| 18 | function ReadInt64(Node: TDOMNode; Name: string; DefaultValue: Int64): Int64;
|
|---|
| 19 | function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
|
|---|
| 20 | function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
|
|---|
| 21 | function ReadDateTime(Node: TDOMNode; Name: string; DefaultValue: TDateTime): TDateTime;
|
|---|
| 22 | function ReadDouble(Node: TDOMNode; Name: string; DefaultValue: Double): Double;
|
|---|
| 23 | procedure ReadXMLFileParser(out Doc: TXMLDocument; FileName: string);
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | implementation
|
|---|
| 27 |
|
|---|
| 28 | function ReadDouble(Node: TDOMNode; Name: string; DefaultValue: Double): Double;
|
|---|
| 29 | var
|
|---|
| 30 | NewNode: TDOMNode;
|
|---|
| 31 | begin
|
|---|
| 32 | Result := DefaultValue;
|
|---|
| 33 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 34 | if Assigned(NewNode) then
|
|---|
| 35 | Result := StrToFloat(string(NewNode.TextContent));
|
|---|
| 36 | end;
|
|---|
| 37 |
|
|---|
| 38 | procedure ReadXMLFileParser(out Doc: TXMLDocument; FileName: string);
|
|---|
| 39 | var
|
|---|
| 40 | Parser: TDOMParser;
|
|---|
| 41 | Src: TXMLInputSource;
|
|---|
| 42 | InFile: TFileStream;
|
|---|
| 43 | begin
|
|---|
| 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;
|
|---|
| 55 | end;
|
|---|
| 56 |
|
|---|
| 57 | function GetTimeZoneBias: Integer;
|
|---|
| 58 | {$IFDEF WINDOWS}
|
|---|
| 59 | var
|
|---|
| 60 | TimeZoneInfo: TTimeZoneInformation;
|
|---|
| 61 | begin
|
|---|
| 62 | {$push}{$warn 5057 off}
|
|---|
| 63 | case GetTimeZoneInformation(TimeZoneInfo) of
|
|---|
| 64 | TIME_ZONE_ID_STANDARD: Result := TimeZoneInfo.Bias + TimeZoneInfo.StandardBias;
|
|---|
| 65 | TIME_ZONE_ID_DAYLIGHT: Result := TimeZoneInfo.Bias + TimeZoneInfo.DaylightBias;
|
|---|
| 66 | else
|
|---|
| 67 | Result := 0;
|
|---|
| 68 | end;
|
|---|
| 69 | {$pop}
|
|---|
| 70 | end;
|
|---|
| 71 | {$ELSE}
|
|---|
| 72 | begin
|
|---|
| 73 | Result := 0;
|
|---|
| 74 | end;
|
|---|
| 75 | {$ENDIF}
|
|---|
| 76 |
|
|---|
| 77 | function LeftCutString(var Source: string; out Output: string; Delimiter: string; Allowed: string = ''): Boolean;
|
|---|
| 78 | var
|
|---|
| 79 | I: Integer;
|
|---|
| 80 | Matched: Boolean;
|
|---|
| 81 | begin
|
|---|
| 82 | I := 1;
|
|---|
| 83 | Matched := True;
|
|---|
| 84 | while (I < Length(Source)) and Matched do begin
|
|---|
| 85 | Matched := True;
|
|---|
| 86 | if (Source[I] = Delimiter) then Matched := False;
|
|---|
| 87 | //for J := 1 to Length(Allowed) do
|
|---|
| 88 | // if Source[I] = Allowed[J] then Matched := True;
|
|---|
| 89 | if Matched then Inc(I);
|
|---|
| 90 | end;
|
|---|
| 91 | if (Delimiter = Copy(Source, I, Length(Delimiter))) or (I = Length(Source)) then begin
|
|---|
| 92 | Output := Copy(Source, 1, I - 1);
|
|---|
| 93 | Delete(Source, 1, Length(Output) + Length(Delimiter));
|
|---|
| 94 | Result := True;
|
|---|
| 95 | end else begin
|
|---|
| 96 | Output := '';
|
|---|
| 97 | Result := False;
|
|---|
| 98 | end;
|
|---|
| 99 | end;
|
|---|
| 100 |
|
|---|
| 101 | function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
|
|---|
| 102 | var
|
|---|
| 103 | Part: string;
|
|---|
| 104 | Year: Integer;
|
|---|
| 105 | Month: Integer;
|
|---|
| 106 | Day: Integer;
|
|---|
| 107 | Hour: Integer;
|
|---|
| 108 | Minute: Integer;
|
|---|
| 109 | Second: Integer;
|
|---|
| 110 | SecondFraction: Double;
|
|---|
| 111 | Millisecond: Integer;
|
|---|
| 112 | begin
|
|---|
| 113 | if LeftCutString(XMLDateTime, Part, '-') then
|
|---|
| 114 | Year := StrToInt(Part);
|
|---|
| 115 | if LeftCutString(XMLDateTime, Part, '-') then
|
|---|
| 116 | Month := StrToInt(Part);
|
|---|
| 117 | if Pos('T', XMLDateTime) > 0 then begin
|
|---|
| 118 | if LeftCutString(XMLDateTime, Part, 'T') then
|
|---|
| 119 | Day := StrToInt(Part);
|
|---|
| 120 | if LeftCutString(XMLDateTime, Part, ':') then
|
|---|
| 121 | Hour := StrToInt(Part);
|
|---|
| 122 | if LeftCutString(XMLDateTime, Part, ':') then
|
|---|
| 123 | Minute := StrToInt(Part);
|
|---|
| 124 | if Pos('.', XMLDateTime) > 0 then begin
|
|---|
| 125 | if LeftCutString(XMLDateTime, Part, '.') then
|
|---|
| 126 | Second := StrToInt(Part);
|
|---|
| 127 | if Pos('+', XMLDateTime) > 0 then
|
|---|
| 128 | LeftCutString(XMLDateTime, Part, '+') else
|
|---|
| 129 | if Pos('-', XMLDateTime) > 0 then
|
|---|
| 130 | LeftCutString(XMLDateTime, Part, '-') else
|
|---|
| 131 | if Pos('Z', XMLDateTime) > 0 then
|
|---|
| 132 | LeftCutString(XMLDateTime, Part, 'Z');
|
|---|
| 133 | SecondFraction := StrToFloat('0' + DefaultFormatSettings.DecimalSeparator + Part);
|
|---|
| 134 | Millisecond := Trunc(SecondFraction * 1000);
|
|---|
| 135 | end else begin
|
|---|
| 136 | if Pos('+', XMLDateTime) > 0 then
|
|---|
| 137 | LeftCutString(XMLDateTime, Part, '+') else
|
|---|
| 138 | if Pos('-', XMLDateTime) > 0 then
|
|---|
| 139 | LeftCutString(XMLDateTime, Part, '-') else
|
|---|
| 140 | if Pos('Z', XMLDateTime) > 0 then
|
|---|
| 141 | LeftCutString(XMLDateTime, Part, 'Z');
|
|---|
| 142 | Second := StrToInt(Part);
|
|---|
| 143 | Millisecond := 0;
|
|---|
| 144 | end;
|
|---|
| 145 | end else begin
|
|---|
| 146 | Day := StrToInt(XMLDateTime);
|
|---|
| 147 | end;
|
|---|
| 148 | Result := EncodeDateTime(Year, Month, Day, Hour, Minute, Second, Millisecond);
|
|---|
| 149 | // TODO: Correct time by zone bias
|
|---|
| 150 | end;
|
|---|
| 151 |
|
|---|
| 152 | function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): string;
|
|---|
| 153 | const
|
|---|
| 154 | Neg: array[Boolean] of string = ('+', '-');
|
|---|
| 155 | var
|
|---|
| 156 | Bias: Integer;
|
|---|
| 157 | begin
|
|---|
| 158 | Result := FormatDateTime('yyyy''-''mm''-''dd''T''hh'':''nn'':''ss''.''zzz', Value); { Do not localize }
|
|---|
| 159 | Bias := GetTimeZoneBias;
|
|---|
| 160 | if (Bias <> 0) and ApplyLocalBias then
|
|---|
| 161 | begin
|
|---|
| 162 | Result := Format('%s%s%.2d:%.2d', [Result, Neg[Bias > 0], { Do not localize }
|
|---|
| 163 | Abs(Bias) div MinsPerHour,
|
|---|
| 164 | Abs(Bias) mod MinsPerHour]);
|
|---|
| 165 | end else
|
|---|
| 166 | Result := Result + 'Z'; { Do not localize }
|
|---|
| 167 | end;
|
|---|
| 168 |
|
|---|
| 169 | procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
|
|---|
| 170 | var
|
|---|
| 171 | NewNode: TDOMNode;
|
|---|
| 172 | begin
|
|---|
| 173 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 174 | NewNode.TextContent := DOMString(IntToStr(Value));
|
|---|
| 175 | Node.AppendChild(NewNode);
|
|---|
| 176 | end;
|
|---|
| 177 |
|
|---|
| 178 | procedure WriteInt64(Node: TDOMNode; Name: string; Value: Int64);
|
|---|
| 179 | var
|
|---|
| 180 | NewNode: TDOMNode;
|
|---|
| 181 | begin
|
|---|
| 182 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 183 | NewNode.TextContent := DOMString(IntToStr(Value));
|
|---|
| 184 | Node.AppendChild(NewNode);
|
|---|
| 185 | end;
|
|---|
| 186 |
|
|---|
| 187 | procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
|
|---|
| 188 | var
|
|---|
| 189 | NewNode: TDOMNode;
|
|---|
| 190 | begin
|
|---|
| 191 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 192 | NewNode.TextContent := DOMString(BoolToStr(Value));
|
|---|
| 193 | Node.AppendChild(NewNode);
|
|---|
| 194 | end;
|
|---|
| 195 |
|
|---|
| 196 | procedure WriteString(Node: TDOMNode; Name: string; Value: string);
|
|---|
| 197 | var
|
|---|
| 198 | NewNode: TDOMNode;
|
|---|
| 199 | begin
|
|---|
| 200 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 201 | NewNode.TextContent := DOMString(Value);
|
|---|
| 202 | Node.AppendChild(NewNode);
|
|---|
| 203 | end;
|
|---|
| 204 |
|
|---|
| 205 | procedure WriteDateTime(Node: TDOMNode; Name: string; Value: TDateTime);
|
|---|
| 206 | var
|
|---|
| 207 | NewNode: TDOMNode;
|
|---|
| 208 | begin
|
|---|
| 209 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 210 | NewNode.TextContent := DOMString(DateTimeToXMLTime(Value));
|
|---|
| 211 | Node.AppendChild(NewNode);
|
|---|
| 212 | end;
|
|---|
| 213 |
|
|---|
| 214 | procedure WriteDouble(Node: TDOMNode; Name: string; Value: Double);
|
|---|
| 215 | var
|
|---|
| 216 | NewNode: TDOMNode;
|
|---|
| 217 | begin
|
|---|
| 218 | NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
|
|---|
| 219 | NewNode.TextContent := DOMString(FloatToStr(Value));
|
|---|
| 220 | Node.AppendChild(NewNode);
|
|---|
| 221 | end;
|
|---|
| 222 |
|
|---|
| 223 | function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
|
|---|
| 224 | var
|
|---|
| 225 | NewNode: TDOMNode;
|
|---|
| 226 | begin
|
|---|
| 227 | Result := DefaultValue;
|
|---|
| 228 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 229 | if Assigned(NewNode) then
|
|---|
| 230 | Result := StrToInt(string(NewNode.TextContent));
|
|---|
| 231 | end;
|
|---|
| 232 |
|
|---|
| 233 | function ReadInt64(Node: TDOMNode; Name: string; DefaultValue: Int64): Int64;
|
|---|
| 234 | var
|
|---|
| 235 | NewNode: TDOMNode;
|
|---|
| 236 | begin
|
|---|
| 237 | Result := DefaultValue;
|
|---|
| 238 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 239 | if Assigned(NewNode) then
|
|---|
| 240 | Result := StrToInt64(string(NewNode.TextContent));
|
|---|
| 241 | end;
|
|---|
| 242 |
|
|---|
| 243 | function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
|
|---|
| 244 | var
|
|---|
| 245 | NewNode: TDOMNode;
|
|---|
| 246 | begin
|
|---|
| 247 | Result := DefaultValue;
|
|---|
| 248 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 249 | if Assigned(NewNode) then
|
|---|
| 250 | Result := StrToBool(string(NewNode.TextContent));
|
|---|
| 251 | end;
|
|---|
| 252 |
|
|---|
| 253 | function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
|
|---|
| 254 | var
|
|---|
| 255 | NewNode: TDOMNode;
|
|---|
| 256 | begin
|
|---|
| 257 | Result := DefaultValue;
|
|---|
| 258 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 259 | if Assigned(NewNode) then
|
|---|
| 260 | Result := string(NewNode.TextContent);
|
|---|
| 261 | end;
|
|---|
| 262 |
|
|---|
| 263 | function ReadDateTime(Node: TDOMNode; Name: string; DefaultValue: TDateTime
|
|---|
| 264 | ): TDateTime;
|
|---|
| 265 | var
|
|---|
| 266 | NewNode: TDOMNode;
|
|---|
| 267 | begin
|
|---|
| 268 | Result := DefaultValue;
|
|---|
| 269 | NewNode := Node.FindNode(DOMString(Name));
|
|---|
| 270 | if Assigned(NewNode) then
|
|---|
| 271 | Result := XMLTimeToDateTime(string(NewNode.TextContent));
|
|---|
| 272 | end;
|
|---|
| 273 |
|
|---|
| 274 | end.
|
|---|