Search code examples
utf-8vbscriptstreamqtpadodb

Open an XML, Read Text, Replace some Text, Write the file back in UTF-8 without BOM format


My IBM MQ is not accepting XML file saved in UTF-8 format. I want to try if it accepts UTF-8 without BOM format. I have tried multiple things, but was unable to save the file in non-BOM format. My code is below.

Dim objStreamUTF8      : Set objStreamUTF8      = CreateObject("ADODB.Stream")
Dim objStreamUTF8NoBOM : Set objStreamUTF8NoBOM = CreateObject("ADODB.Stream")

With objStreamUTF8
    .Charset = "UTF-8"
    .Mode = 3
    .Type = 2
    .Open
    objStreamUTF8.LoadFromFile uxtNewRenamePath'"C:\WINDOWS\Temp\DataFiles\TC10_ Apostrophe symbol in tag Cdtr_Adrline2_Pacs8_TxID201765133641.xml"
    objStreamUTF8.Flush
    strFileText = objStreamUTF8.ReadText()
    strFileText = Replace(strFileText,"&#", "&#")
    strFileText = Replace(Replace(Replace(Replace(Replace(strFileText, "GreaterThanSymbol", ">"), "LessThanSymbol", "<"), "ApostropheSymbol", "'"), "AmpersandSymbol", "&"), "DoubleQuotesSymbol", chr(34))
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.DeleteFile uxtNewRenamePath, True
    Set objFSO = Nothing
    .Position = 0
    .Flush
    .WriteText strFileText
    .SaveToFile uxtNewRenamePath, 2
    .Close
    .Type = 1
    .Open
    .Position  = objStreamUTF8.Size
End With

With objStreamUTF8NoBOM
    .Mode = 3
    .Type = 1
    .Open
    objStreamUTF8.CopyTo objStreamUTF8NoBOM
    .SaveToFile uxtNewRenamePath, 2
End With

objStreamUTF8.Close
objStreamUTF8NoBOM.Close

When objStreamUTF8NoBOM is saved to file, the file is completely blank. Can you please tell me what I am doing wrong or how I can save the file in the desired format?


Solution

  • Got it. The position of the first stream has to be set to 3rd postion to skip BOM characters

    i.e. objStreamUTF8.Position = 3