XML数据绑定向导浮点数和定点数

时间:2018-10-17 21:01:07

标签: xml delphi delphi-10-seattle

我已经用XML绑定向导消耗了准备好的XSD。

<?xml version="1.0" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="examples" type="examples"/>
 <xs:complexType name="examples">
  <xs:sequence>
   <xs:element name="example" type="example" minOccurs="1" maxOccurs="unbounded"/>
  </xs:sequence>
 </xs:complexType>
 <xs:complexType name="example">
  <xs:sequence>
   <xs:element name="doublevalue" type="xs:double"/>
   <xs:element name="decimalvalue" type="xs:decimal"/>
  </xs:sequence>
 </xs:complexType>
</xs:schema>

首先,为什么默认情况下小数和双精度会被区别对待? XML Data Binding Wizard Double enter image description here

XML简单类型Double被转换为Delphi本机类型Double和 默认情况下,XML简单类型Decimal被转换为Delphi本机类型UnicodeString。

两种数据类型都存在相同的问题:语言环境冲突

我是德国人。这表示默认情况下(Windows),DecimalSeparator为,,ThousandSeparator为.

当我按如下方式阅读示例XML时,则0.08的double变为8的整数。

XML

<?xml version="1.0" encoding="UTF-8"?>
<examples>
  <example>
    <doublevalue>0.08</doublevalue>
    <decimalvalue>1001.015</decimalvalue>
  </example>
</examples>

代码

var
  xmldoc: IXMLDocument;
  examples: IXMLExamples;
  i: Integer;
  d: Double;
begin
  xmldoc := TXMLDocument.Create(nil) as IXMLDocument;
  try
    xmldoc.LoadFromFile('C:\temp\example.xml');
    examples := Getexamples(xmldoc); // Getexamples() is part of the unit generated by the Binding Wizard 
    for i := 0 to examples.Count - 1 do
      d := examples[i].Doublevalue;
  finally
    examples := nil;
    xmldoc := nil;
  end;
end;

快照 Runtime Debug Snapshot

现在,我将XML数据类型Double更改为Delphi本机类型UnicodeString并使用如下方法:

function XMLStringToDouble(const str: string): double;
var
  fs: TFormatSettings;
begin
  fs := FormatSettings;
  fs.DecimalSeparator := '.';
  fs.ThousandSeparator := #0;
  result := StrToFloat(str, fs);
end;

创建XML时还有另一个问题

代码

var
  xmldoc: TXMLDocument;
  examples: IXMLExamples;
  example: IXMLExample;
begin
  xmldoc := TXMLDocument.Create(nil);
  try
    xmldoc.DOMVendor := MSXML_DOM;
    xmldoc.Options := [doNodeAutoCreate, doNodeAutoIndent, doAttrNull, doAutoPrefix, doNamespaceDecl];
    xmldoc.Active := true;
    xmldoc.Version := '1.0';
    xmldoc.Encoding := 'UTF-8';
    examples := xmldoc.GetDocBinding('examples', TXMLExamples, '') as IXMLExamples;
    example := examples.Add;
    example.Doublevalue := 0.08;
    example.Decimalvalue := '1001.015';
    xmldoc.SaveToFile('C:\temp\example.xml');
  finally
    xmldoc.Free
  end;
end;

我最终得到的XML为,作为DecimalSeparator。

<?xml version="1.0" encoding="UTF-8"?>
<examples>
  <example>
    <doublevalue>0,08</doublevalue>
    <decimalvalue>1001.015</decimalvalue>
  </example>
</examples>

1。有没有办法以更简单/适当的方式对待Double?

2。我可以以某种方式将TFormatSettings传递给XMLDocument或以完全不同的方式解决它吗?

3。你如何做到的?

1 个答案:

答案 0 :(得分:0)

我无法重现您的问题(使用Delphi Tokyo 10.2.3)。

我做了一个小的MCVE来证明这一点,使用下面带有德国区域设置的代码仍会读/写一个'。'。作为小数点分隔符:

program SO52863558;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Windows,
  ActiveX,
  XmlDoc,
  XmlIntf,
  xmltest in 'xmltest.pas';

var
  Examples: IXMLExamples;
  Example: IXMLExample;
  i: Integer;
  d: Double;

begin
 try
  CoInitialize(nil);
  try
   Examples := Loadexamples('test.xml');
   for i := 0 to examples.Count - 1 do
    begin
     Writeln(examples[i].Doublevalue:0:2);
     Writeln(examples[i].Decimalvalue);
    end;
   Examples := nil;
   Examples := NewExamples;
   Example := Examples.Add;
   Example.Doublevalue := 0.012;
   Example.Decimalvalue := '12.51515';
   Examples.OwnerDocument.SaveToFile('test1.xml');
  finally
   Example := nil;
   Examples := nil;
   CoUninitialize;
  end;
 except
  on E: Exception do
   Writeln(E.Message);
 end;
 Readln;
end.

通过绑定从XSD文件生成的单元:

unit xmltest;

interface

uses Xml.xmldom, Xml.XMLDoc, Xml.XMLIntf;

type

{ Forward Decls }

  IXMLExamples = interface;
  IXMLExample = interface;

{ IXMLExamples }

  IXMLExamples = interface(IXMLNodeCollection)
    ['{E86B6745-A58E-439F-A73F-B92F446B146B}']
    { Property Accessors }
    function Get_Example(Index: Integer): IXMLExample;
    { Methods & Properties }
    function Add: IXMLExample;
    function Insert(const Index: Integer): IXMLExample;
    property Example[Index: Integer]: IXMLExample read Get_Example; default;
  end;

{ IXMLExample }

  IXMLExample = interface(IXMLNode)
    ['{41DB1169-948C-4B28-BFB0-F63ACEAC14DB}']
    { Property Accessors }
    function Get_Doublevalue: Double;
    function Get_Decimalvalue: UnicodeString;
    procedure Set_Doublevalue(Value: Double);
    procedure Set_Decimalvalue(Value: UnicodeString);
    { Methods & Properties }
    property Doublevalue: Double read Get_Doublevalue write Set_Doublevalue;
    property Decimalvalue: UnicodeString read Get_Decimalvalue write Set_Decimalvalue;
  end;

{ Forward Decls }

  TXMLExamples = class;
  TXMLExample = class;

{ TXMLExamples }

  TXMLExamples = class(TXMLNodeCollection, IXMLExamples)
  protected
    { IXMLExamples }
    function Get_Example(Index: Integer): IXMLExample;
    function Add: IXMLExample;
    function Insert(const Index: Integer): IXMLExample;
  public
    procedure AfterConstruction; override;
  end;

{ TXMLExample }

  TXMLExample = class(TXMLNode, IXMLExample)
  protected
    { IXMLExample }
    function Get_Doublevalue: Double;
    function Get_Decimalvalue: UnicodeString;
    procedure Set_Doublevalue(Value: Double);
    procedure Set_Decimalvalue(Value: UnicodeString);
  end;

{ Global Functions }

function Getexamples(Doc: IXMLDocument): IXMLExamples;
function Loadexamples(const FileName: string): IXMLExamples;
function Newexamples: IXMLExamples;

const
  TargetNamespace = '';

implementation

uses Xml.xmlutil;

{ Global Functions }

function Getexamples(Doc: IXMLDocument): IXMLExamples;
begin
  Result := Doc.GetDocBinding('examples', TXMLExamples, TargetNamespace) as IXMLExamples;
end;

function Loadexamples(const FileName: string): IXMLExamples;
begin
  Result := LoadXMLDocument(FileName).GetDocBinding('examples', TXMLExamples, TargetNamespace) as IXMLExamples;
end;

function Newexamples: IXMLExamples;
begin
  Result := NewXMLDocument.GetDocBinding('examples', TXMLExamples, TargetNamespace) as IXMLExamples;
end;

{ TXMLExamples }

procedure TXMLExamples.AfterConstruction;
begin
  RegisterChildNode('example', TXMLExample);
  ItemTag := 'example';
  ItemInterface := IXMLExample;
  inherited;
end;

function TXMLExamples.Get_Example(Index: Integer): IXMLExample;
begin
  Result := List[Index] as IXMLExample;
end;

function TXMLExamples.Add: IXMLExample;
begin
  Result := AddItem(-1) as IXMLExample;
end;

function TXMLExamples.Insert(const Index: Integer): IXMLExample;
begin
  Result := AddItem(Index) as IXMLExample;
end;

{ TXMLExample }

function TXMLExample.Get_Doublevalue: Double;
begin
  Result := XmlStrToFloatExt(ChildNodes['doublevalue'].Text);
end;

procedure TXMLExample.Set_Doublevalue(Value: Double);
begin
  ChildNodes['doublevalue'].NodeValue := Value;
end;

function TXMLExample.Get_Decimalvalue: UnicodeString;
begin
  Result := ChildNodes['decimalvalue'].Text;
end;

procedure TXMLExample.Set_Decimalvalue(Value: UnicodeString);
begin
  ChildNodes['decimalvalue'].NodeValue := Value;
end;    
end.

从生成数据绑定的代码中可以看到,它使用XmlStrToFloatExt将double转换为字符串。查看此函数背后的代码时:

type
  PFormatSettings = ^TFormatSettings;

var
  FormatSettings : TFormatSettings;

function GetFormatSettings: PFormatSettings;
begin
  if FormatSettings.DecimalSeparator <> XmlDecimalSeparator then
  begin
    FormatSettings := TFormatSettings.Create('');
    FormatSettings.DecimalSeparator := XmlDecimalSeparator;
  end;
  Result := @FormatSettings;
end;

function XmlFloatToStr(const Value: Extended): string;
begin
  Result := FloatToStr(Value, GetFormatSettings^);
end;

function XmlStrToFloat(const Value: string): Extended;
begin
  Result := StrToFloat(Value, GetFormatSettings^);
end;

function XmlStrToFloatExt(const Value: string): Extended;
var
  s: string;
begin
  s := Trim(Value);
  if s = '' then
    Result := 0.0
  else if SameText(Value, 'NaN') then
    Result := Nan
  else if SameText(Value, 'INF') then
    Result := Infinity
  else if SameText(Value, '-INF') then
    Result := NegInfinity
  else
    Result := XmlStrToFloat(Value);
end;

您可以看到,无论您的区域设置是什么,它将始终强制XmlDecimalSeparator(定义为'。')。