It's possible to convert the XML to UTF-8 encoding in Delphi 6?
Currently that's what I am doing:
- Fill TXMLDocument with AnsiString
- At the end convert the Data to UTF-8 by using
WideStringVariable = AnsiToUtf8(Doc.XML.Text); - Save the value of
WideStringVariableto file usingTFileStreamand AddingBOM for UTF8at the 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) <> '' then 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] ));
finally
fs.free
end;
end;
end;
If I open the file in Notepad++ or another editor that detects encoding, it shows me UTF-8 with BOM. However, it seems like the text it's not properly encoded.
What is wrong and how can I fix it?
UPDATE: XML Properties:
XMLDoc.Version := '1.0';
XMLDoc.Encoding := 'UTF-8';
XMLDoc.StandAlone := 'yes';
WideStringisMicrosoft OLE BSTRtype which is UTF-16 by definition. ShowAnsiToUtf8declaration, what is its return type ? you should use the same type for variable, otherwise text conversion happens. And since your output while declared ANSI would hold UTF-8 text, that means you're misleading Delphi and provoking it to make ANSI->Tf-16 conversin over non-ANSI text