Save XML file from database to hard disk

In this forum you may discuss all issues concerning the Lazarus IDE and Freepascal (both running on Windows or Linux).

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
mark
Fresh Boarder
Fresh Boarder
Posts: 1
Joined: 13.08.2020, 06:12

Save XML file from database to hard disk

Post by mark »

Hi,

I have problem with saving XML files from postgresql 12.x database to hard drive (XML data type field). I have working procedure with blob data type (binary):

Code: Select all

var
 blob,myFileStream:TStream;
 SaveFile:string;
begin
 SaveFile:='c:\test\test.xml';
   blob := Zquery.CreateBlobStream(Zquery.FieldByName('xml_file'), bmRead);
   try
    blob.Seek(0, soFromBeginning);
    myFileStream := TFileStream.Create(SaveFile, fmCreate);
    try
     myFileStream.CopyFrom(blob, blob.Size) ;
    finally
     myFileStream.Free ;
    end;
   finally
    blob.Free ;
   end;
end;
This procedure works well with binary data type but when i use xml data type i have empty stream and file. How to save XML files to hard drive?

Regards
Mark
Fr0sT
Zeos Dev Team
Zeos Dev Team
Posts: 280
Joined: 08.05.2014, 12:08

Re: Save XML file from database to hard disk

Post by Fr0sT »

My guess (haven't worked with Pg): is Pg XML type really supported? It looks like a custom one. Probably it needs special handling
miab3
Zeos Test Team
Zeos Test Team
Posts: 1309
Joined: 11.05.2012, 12:32
Location: Poland

Re: Save XML file from database to hard disk

Post by miab3 »

Hi,

You can use ::xml

Michal
User avatar
aehimself
Zeos Dev Team
Zeos Dev Team
Posts: 766
Joined: 18.11.2018, 17:37
Location: Hungary

Re: Save XML file from database to hard disk

Post by aehimself »

For a really long time now, I switched storing everything (even Strings, if they are coming from an "outside" source - like HTTP requests, files what the user might edit) as TBytes (in Delphi, that's equivalent to Array Of Byte). That way I can do all kind of manipulation before storing it in it's final variable.

I don't have Lazarus / FPC, but this is really easy to be converted if necessary. And works like a charm, on all data types (not just blobs!).

Code: Select all

Uses System.SysUtils, System.IOUtils;

Var
 tb: TBytes;
Begin
 tb := SQLQuery.FieldByName('something').AsBytes;
 TFile.WriteAllBytes('C:\rawdump.bin', tb);
End;
Delphi 12.1, Zeos 8 from latest GIT snapshot
Using:
- MySQL server 8.0.18; libmariadb.dll 3.3.8
- Oracle server 11.2.0, 12.1.0, 19.0.0; oci.dll 21.13
- MSSQL 2012, 2019; sybdb.dll FreeTDS_2435
- SQLite 3.45.2
Post Reply