【问题标题】:overflow while converting variant of type (OleStr) into Type Integer Delphi将类型 (OleStr) 的变体转换为类型整数 Delphi 时溢出
【发布时间】:2013-04-16 07:19:54
【问题描述】:

我正在尝试读取 Xml 文件,但在整数或 WideString 类型上不断出现“将类型 (OleStr) 的变体转换为整数类型时溢出”错误,我尝试将类型从 VarToStr、IntToStr、VarToWideStr 和 OleStrToString 更改。

xml 文件示例

<product product-id="01126400000" product-group-id="10010877">
<name><![CDATA[Love-Positions]]></name>
<selling-price>6.95</selling-price>
<dicount-prohibited>0</dicount-prohibited>
<list-price>4.00</list-price>
<ean-code>4024144112647</ean-code>
<availability>1</availability>
<valid-from-date>19970623</valid-from-date>

procedure TForm1.Button1Click(Sender: TObject);
Var
MyXmlMax,MyXmlMin : integer;
Item: String;

begin

MyXmlMax := xmlDatafeed.Productlist.Count;
//item :=  (xmlDatafeedsub.Attributes['product-id']+','+ xmlDatafeedsub.Attributes['product-group-id'] );


for MyxmlMin := 1 to MyXmlMax-1 do begin
xmlDatafeedsub:=xmlDatafeed.Productlist[MyxmlMin];
memo1.lines.add('------');
memo1.lines.add(VarToStr(xmlDatafeedsub.Productid));  //Integer
memo1.lines.add(VarToStr(xmlDatafeedsub.Productgroupid)); //Integer
Memo1.Lines.Add(xmlDatafeedsub.Name);//WideString
memo1.lines.add((xmlDatafeedsub.Sellingprice)); //Integer
memo1.lines.add(VarToStr(XmlDataFeedSub.Dicountprohibited));//Integer
memo1.lines.add((xmlDatafeedsub.Listprice)); //Widestring
memo1.lines.Add(IntToStr(xmlDatafeedsub.Eancode));//Integer
memo1.lines.add(VarToStr(xmlDatafeedsub.Availability));//Integer
memo1.lines.add(VarToStr(xmlDatafeedsub.Validfromdate));//Integer
Inc(MyXmlMax,1);
memo1.lines.add('------');
end;   //end if
end;


//productdata_v2_01_01.xdb
`
function TXMLProductType.Get_Productid: Integer;
begin
  Result := AttributeNodes['product-id'].NodeValue;
end;

procedure TXMLProductType.Set_Productid(Value: Integer);
begin
  SetAttribute('product-id', Value);
end;

function TXMLProductType.Get_Productgroupid: Integer;
begin
  Result := AttributeNodes['product-group-id'].NodeValue;
end;

procedure TXMLProductType.Set_Productgroupid(Value: Integer);
begin
  SetAttribute('product-group-id', Value);
end;

function TXMLProductType.Get_Name: WideString;
begin
  Result := ChildNodes['name'].Text;
end;

procedure TXMLProductType.Set_Name(Value: WideString);
begin
  ChildNodes['name'].NodeValue := Value;
end;

function TXMLProductType.Get_Sellingprice: WideString;
begin
  Result := ChildNodes['selling-price'].Text;
end;

procedure TXMLProductType.Set_Sellingprice(Value: WideString);
begin
  ChildNodes['selling-price'].NodeValue := Value;
end;

function TXMLProductType.Get_Dicountprohibited: Integer;
begin
  Result := ChildNodes['dicount-prohibited'].NodeValue;
end;

procedure TXMLProductType.Set_Dicountprohibited(Value: Integer);
begin
  ChildNodes['dicount-prohibited'].NodeValue := Value;
end;

function TXMLProductType.Get_Listprice: WideString;
begin
  Result := ChildNodes['list-price'].Text;
end;

procedure TXMLProductType.Set_Listprice(Value: WideString);
begin
  ChildNodes['list-price'].NodeValue := Value;
end;

function TXMLProductType.Get_Eancode: Integer;
begin
  Result := ChildNodes['ean-code'].NodeValue;
end;

procedure TXMLProductType.Set_Eancode(Value: Integer);
begin
  ChildNodes['ean-code'].NodeValue := Value;
end;

function TXMLProductType.Get_Availability: Integer;
begin
  Result := ChildNodes['availability'].NodeValue;
end;

procedure TXMLProductType.Set_Availability(Value: Integer);
begin
  ChildNodes['availability'].NodeValue := Value;
end;

function TXMLProductType.Get_Validfromdate: Integer;
begin
  Result := ChildNodes['valid-from-date'].NodeValue;
end;

procedure TXMLProductType.Set_Validfromdate(Value: Integer);
begin
  ChildNodes['valid-from-date'].NodeValue := Value;
end;

【问题讨论】:

  • 请做一些调试。找到失败的代码行。让我们知道那行代码使用的变量的值是什么。
  • 如果我注释掉第一个错误,错误会跳到下一个变量,并且只有整数受它的影响 ' function TXMLProductType.Get_Productid: Integer; begin ' 结果 := AttributeNodes['product-id'].NodeValue;结束;code
  • @Gis "AttributeNodes['product-id'].NodeValue" 的值是多少?
  • 您好,当 xml 库尝试将 Attributenode[xx].nodevalue 转换为整数时,会发生实际错误,您在此处提供的代码中不会发生错误。请提供您要读取的 xml 单元和实际的 xml 文件。
  • @Gis 您的问题的解决方案可以在这里找到:sscce.org

标签: delphi delphi-7 pascal variant


【解决方案1】:

您的产品 ID 属性和EAN Codes 不是整数,但您正试图将它们视为整数。它们是字符串值。 (它们太大而无法放入整数。)它们不是您可以对其进行数学或算术运算的数字,因此不要尝试将它们视为此类。将定义改为WideString,应该没问题。

// You'll need to change these in the `interface` as well. Here are the EANCode
// conversions for you.
function TXMLProductType.Get_Eancode: WideString;
begin
  Result := ChildNodes['ean-code'].NodeValue;
end;

procedure TXMLProductType.Set_Eancode(Value: WideString);
begin
  ChildNodes['ean-code'].NodeValue := Value;
end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多