Monday, September 14, 2009

XML file Conversion from ANSI to Unicode

Some XML files which are saved in ANSI Encoding type are not fully open in IE browser. Those files we have to save as in Unicode Encoding Type then we can open those files without errors. We can do above action in QTP with below function.

Function convertANSItoUTF8(input_file,output_file)
Const adTypeBinary = 1
Const adTypeText = 2
Const bOverwrite = True
Const bAsASCII = False
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFFSpec : sFFSpec = oFS.GetAbsolutePathName( input_file )
Dim sTFSpec : sTFSpec = oFS.GetAbsolutePathName(output_file )
Dim oFrom : Set oFrom = CreateObject( "ADODB.Stream" )
Dim sFrom : sFrom = "Windows-1252"
Dim oTo : Set oTo = CreateObject( "ADODB.Stream" )
Dim sTo : sTo = "utf-8"
If oFS.FileExists( sTFSpec ) Then oFS.DeleteFile sTFSpec
oFrom.Type = adTypeText
oFrom.Charset = sFrom
oFrom.Open
oFrom.LoadFromFile sFFSpec
msgbox oFrom.Size & " Bytes in " & sFFSpec
oTo.Type = adTypeText
oTo.Charset = sTo
oTo.Open
oTo.WriteText oFrom.ReadText
msgbox oTo.Size & " Bytes in " & sTFSpec
oTo.SaveToFile sTFSpec
oFrom.Close oTo.Close
End Function

Another Way:-

Const ForReading = 1, ForWriting = 2
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Set fso = CreateObject("Scripting.FileSystemObject")
XMLPath= "C:\File.xml"
fso.CreateTextFile XMLPath ' Create a file.
Set f = fso.GetFile(XMLPath)
Set ts = f.OpenAsTextStream(ForWriting, TristateTrue)
ts.Write Browser("Browser").Page("Page").WebXML("ObjectName").GetData
ts.Close

No comments:

Post a Comment