Change XML file encoding

122 views
Skip to first unread message

Andy

unread,
May 2, 2012, 1:57:11 AM5/2/12
to Ensemble-in...@googlegroups.com
I have a number of files that come in. They are encoded as UTF-16.
There is nothing in them that needs UTF-16 and due to limitation on an
upstream system I need to change them to UTF-8. Anyone aware of an
easy way to change the encoding of a file?

Ted Peck

unread,
May 2, 2012, 9:25:07 AM5/2/12
to ensemble-in...@googlegroups.com, Andy
I think this will work:

Set fIn=##class(%IO.FileStream).%New("c:\test\FileUTF16.xml"), fIn.CharEncoding="UTF16"
Set fOut=##class(%IO.FileStream.New()
Do fOut.Open("c:\test\NewFileUTF8.xml","WNE","UTF-8",.tSC)  Quit:$$$ISERR(tSC) tSC
Do fOut.CopyFrom(fIn,,,.tSC)  Quit:$$$ISERR(tSC) tSC
Do fOut.Close(.tSC)   Quit:$$$ISERR(tSC) tSC
--
You received this message because you are subscribed to the Google Groups "InterSystems: Ensemble in Healthcare Community" group.
To post to this group, send email to Ensemble-in...@googlegroups.com
To unsubscribe from this group, send email to Ensemble-in-Healt...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Ensemble-in-Healthcare?hl=en

Dale du Preez

unread,
May 2, 2012, 10:53:54 AM5/2/12
to ensemble-in...@googlegroups.com, Ted Peck, Andy
The only thing to be careful of is the value of the encoding attribute in the <?xml?> declaration -- that will also need to be changed if present. (For UTF-16, it should be, as I think UTF-8 is implied if the attribute is not present.)

Dale

Ted Peck

unread,
May 2, 2012, 1:02:02 PM5/2/12
to Dale du Preez, ensemble-in...@googlegroups.com, Andy
Good point.  We have a method for doing that also:  Ens.Util.XML.Reader:ChangeXMLStreamEncoding().

So the correct code for XML would be:


Set fIn=##class(%IO.FileStream).%New("c:\test\FileUTF16.xml"), fIn.CharEncoding="UTF16"
Set fOut=##class(%IO.FileStream.New()
Do fOut.Open("c:\test\NewFileUTF8.xml","WNE","UTF-8",.tSC)  Quit:$$$ISERR(tSC) tSC
Do ##class(Ens.Util.XML.Reader).ChangeXMLStreamEncoding(fIn,"",fOut,.tSC)  Quit:$$$ISERR(tSC) tSC

Do fOut.Close(.tSC)   Quit:$$$ISERR(tSC) tSC

Ted
Reply all
Reply to author
Forward
0 new messages