Recent

Author Topic: Leitura de XML  (Read 2941 times)

Stclara

  • New Member
  • *
  • Posts: 10
Leitura de XML
« on: March 15, 2018, 12:10:28 am »
Galera, tenho o xml cidade.xml:
<?xml version='1.0' encoding='ISO-8859-1'?>
<cidade>
   <nome>Lapa</nome>
   <uf>PR</uf>
   <atualizacao>2018-03-14</atualizacao>
   <previsao>
      <dia>2018-03-15</dia>
      <tempo>pt</tempo>
      <maxima>33</maxima>
      <minima>20</minima>
      <iuv>11.0</iuv>
   </previsao>
</cidade>

Leio ele com esta rotina:

procedure TForm1.Button1Click(Sender: TObject);
var
  NomeNode, UfNode, DiaNode, IuvNode: TDOMNode;
  Doc:      TXMLDocument;
begin
  ReadXMLFile(Doc, 'C:\cef\tempo\cidade.xml');
 
  NomeNode := Doc.DocumentElement.FindNode('nome');
  edtcidade.Text := NomeNode.TextContent;

  UfNode := Doc.DocumentElement.FindNode('uf');
  edtuf.Text := UfNode.TextContent;

  DiaNode := Doc.DocumentElement.FindNode('dia');
  edtdata.Text := DiaNode.TextContent;

  IuvNode := Doc.DocumentElement.FindNode('iuv');
  edttemperatura.Text := IuvNode.TextContent;

  Doc.Free;
end;


No entanto, edtdata.Text := DiaNode.TextContent; e edttemperatura.Text := IuvNode.TextContent; retornam o erro:

unit1.pas(62,47) Warning: Implicit string type conversion with potential data loss from "WideString" to "TTranslateString"


Como resolver esta questão?

[]´s

Stclara.

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Leitura de XML
« Reply #1 on: March 15, 2018, 12:18:12 am »
unit1.pas(62,47) Warning: Implicit string type conversion with potential data loss from "WideString" to "TTranslateString"

LCL controls expect UTF8, but the FPC XML methods return WideString. Do this around any string returned by XML methods, eg,

UTF8Encode(NomeNode.TextContent);


Stclara

  • New Member
  • *
  • Posts: 10
Re: Leitura de XML
« Reply #2 on: March 15, 2018, 01:03:46 am »
Hi, Phil. I believe the problem is in the data type ... While NameNode and UfNode are strings, DiaNode is date (2018-03-17) and IuvNode is float (11.0).
I think I would have to convert, or something ... But I do not know how to do the conversion ...

 

TinyPortal © 2005-2018