unicode - How to Convert Ansi to UTF 8 with TXMLDocument in Delphi -


it's possible convert xml utf-8 encoding in delphi 6?
that's doing:

  • fill txmldocument ansistring
  • at end convert data utf-8 using widestringvariable = ansitoutf8(doc.xml.text);
  • save value of widestringvariable file using tfilestream , adding bom utf8 @ file beggining.

code:

procedure saveasutf8( const name:string; data: tstrings );  const   cutf8 = $bfbbef; var   w_txt: widestring;   fs: tfilestream;   wbom: integer; begin   if trim(data.text) <> '' begin         w_txt:= ansitoutf8(data.text);     fs:= tfilestream.create( name, fmcreate );     try       wbom := cutf8;       fs.writebuffer( wbom, sizeof(wbom)-1);       fs.writebuffer( w_txt[1], length(w_txt)*sizeof( w_txt[1] ));           fs.free     end;   end; end; 

if open file in notepad++ or editor detects encoding, shows me utf-8 bom. however, seems text it's not encoded.

what wrong , how can fix it?

update: xml properties:

xmldoc.version := '1.0'; xmldoc.encoding := 'utf-8'; xmldoc.standalone := 'yes'; 

you can save file using standard savetofile method on txmldocument variable: http://docs.embarcadero.com/products/rad_studio/delphiandcpp2009/helpupdate2/en/html/delphivclwin32/xmldoc_txmldocument_savetofile.html

whether file or not utf8 have check using local tools aforementioned notepad++ or hex editor or else.


if insist of using intermediate string , file stream, should use proper variable. ansitoutf8 returns utf8string type , used. compiling `widestringvar := ansistringsource' issue compiler warning ,

it proper warning. googling "delphi widestring" - or reading delphi manuals on topic - shows widestring aka microsoft ole bstr keeps data in utf-16 format. http://delphi.about.com/od/beginners/l/aa071800a.htm assignment utf16 string <= 8-bit source convert data , dumping widestring data can not dumping utf-8 text definition of widestring

procedure saveasutf8( const name:string; data: tstrings ); const   cutf8: array [1..3] of byte = ($ef,$bb,$bf) var   w_txt: utf8string;   fs: tfilestream;   trimmed: ansistring; begin   trimmed := trim(data.text);   if trimmed <> '' begin         w_txt:= ansitoutf8(trimmed);     fs:= tfilestream.create( name, fmcreate );     try       fs.writebuffer( cutf8[1], sizeof(cutf8) );       fs.writebuffer( w_txt[1], length(w_txt)*sizeof( w_txt[1] ));           fs.free     end;   end; end; 

btw, code of yours not create empty file if source data empty. looks rather suspicious, though decide whether error or not wrt rest of program.


the proper "uploading" of received file or stream web yet issue (to put separate question on q&a site so), related testing conformance http. foreword, can readsome hints @ www server reports error after post request internet direct components in delphi


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -