Page 1 of 1

TWideMemoField for a Blob Sub_Type 1 and SaveToFile problem

Posted: 03.02.2019, 18:10
by louis
Hello,
I use Delphi 2010 and I have a Firebird 2.5 Database and a table as:

Code: Select all

CREATE TABLE DOC_FE
(
  ID Integer NOT NULL,
  XML Blob sub_type 1,
  CONSTRAINT DOC_FE_PK PRIMARY KEY (ID)
);
The Field XML contain a xml file like this:

Code: Select all

<?xml version="1.0" encoding="Windows-1252" standalone="no"?>
but if I try to save the field as:

Code: Select all

DOC_FExml.SaveToFile(AFileName);
the file AFileName contain:

Code: Select all

< ? x m l   v e r s i o n = " 1 . 0 "   e n c o d i n g = " W i n d o w s - 1 2 5 2 "   s t a n d a l o n e = " n o " ? > 
if I try to save as:

Code: Select all

Xml := TStringList.Create;
Xml.Text := DOC_FEXML.AsString;
Xml.SaveToFile(AFileName);
the file AFileName contains as expected:

Code: Select all

<?xml version="1.0" encoding="Windows-1252" standalone="no"?>
What I wrong?

Thanks

Re: TWideMemoField for a Blob Sub_Type 1 and SaveToFile problem

Posted: 03.02.2019, 18:33
by marsupilami
Hello louis,

on a first glance it looks like xml.SaveToFile(AFileName); saves the contents without an UTF-16 BOM and so your Editor doesn't know how to correctly read the file. Could you check that with some kind of an hex editor / viewer? This might be a bug in Delphi.

Best regards,

Jan

Re: TWideMemoField for a Blob Sub_Type 1 and SaveToFile problem

Posted: 05.02.2019, 15:34
by louis
Hello,
marsupilami wrote:Could you check that with some kind of an hex editor / viewer? This might be a bug in Delphi.
with an hex viewer, in a file created by BlobField.SaveToFile, I get:

Code: Select all

3c 00 3f 00 78 00 6d 00 6c 00 20 00 76 00 65 00 <.?.x.m.l. .v.e.
but with an hex viewer I get in a right file (created by TStringList.SaveToFile):

Code: Select all

3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 <?xml version="1
Can I set anything to a BlobFile to SaveToFile in right mode?

Thanks.

Re: TWideMemoField for a Blob Sub_Type 1 and SaveToFile problem

Posted: 05.02.2019, 17:25
by marsupilami
Hello Louis,

the problem here is that both components behabe as expected. TWideStringField is a delphi supplied class where we can't modify its behaviour. Zeos only uses it. TWideStringField uses UTF-16 internally so an encoding with 0x00-Bytes is expected there.

TStringList on the other hand is specified to use your ANSI-Encoding for saving its contents to files on disk. My Suggestion is to do something like this:

Code: Select all

var
  List: TStringList;
begin
  // some code here...
  
  List := TStringList.Create;
  try
    List.Text := MyZQuery.FieldByName('MyBlobTextField').AsString;
    List.SaveToFile('MyFileName.xml') // This method also can take encodings as parameters afaik
  finally
    FreeAndNil(List);
  end;
  
  // some more code here...
end;
TStringList has options to select any encoding you want - at least if your Delphi uses Unicode (Delphi 2009 or later).

If you have an ancient version of Delphi that still uses ANSI you might want to set your Connection Character Set according to your ANSI-Codepage. In that case Zeos will switch from using TWideStringField and TWideMemoField to TStringField and TMemoField which use ANSI encodeing instead of Unicode internally.

Best regards,

Jan

Re: TWideMemoField for a Blob Sub_Type 1 and SaveToFile problem

Posted: 11.05.2019, 05:49
by EgonHugeist
Hi Louis,

that indeed is a problem. To be cleare the TBlobStream has no virtual function we can override to get your problem resolved.
IMHO the only way to write raw encoded files is the way you did it.