1. Jahir Bahsurudeen
  2. PowerBuilder
  3. Thursday, 31 October 2019 02:50 AM UTC

Is the Encoding option in SaveAs() works for XML ? 

I am trying as below, 

        ids_data.SaveAs( gs_path + ls_xmlfilename , XML!, FALSE , EncodingUTF8!)

XML file still created with encoding="UTF-16LE".. 

How do we save data in UTF-8 format without using template? 

 

Thank You,

Jahir

 

Jahir Bahsurudeen Accepted Answer Pending Moderation
  1. Friday, 1 November 2019 01:19 AM UTC
  2. PowerBuilder
  3. # 1

Chris,

Thank you.. I tried before submit the issue.  I just tried again and didn't work.

XML Declaration option values is not saved as part of dwsyntax and I believe it was designed for Templates.

MY PB Version : PowerBuilder 2017 Build 1666 . 

 

Thank You,

Jahir.

 

Comment
  1. Chris Pollach @Appeon
  2. Friday, 1 November 2019 15:08 PM UTC
Hi Jahir;

Yes, I just tried this myself now and it does not work in PB2017/2019. :-(

I'll post my workaround solution next as a reply to your initial post so that you can see more of my workaround. In the mean-time, I would open a Support Ticket for this issue.

Regards .. Chris
  1. Helpful
There are no comments made yet.
Jahir Bahsurudeen Accepted Answer Pending Moderation
  1. Friday, 1 November 2019 01:21 AM UTC
  2. PowerBuilder
  3. # 2

Miguel Leeuwe ,

I will try as you mentioned and keep you update.

 

Thank You,

Jahir.

 

 

Comment
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Friday, 1 November 2019 15:23 PM UTC
  2. PowerBuilder
  3. # 3

Hi Jahir;

   Here is how I made it work as a workaround for the non-UTF-8 issue ...

1) Extract the DW XML into a variable (it will still say UTF-16LE even if you changed it in the DW Painter).

2) Replace the UTF-16LE signature with a UTF-8 one

3) Write the XML data variable now to a UTF-8 file.

String     ls_xml
Int         li_pos
Int         li_file
ls_xml  =  dc_data.Describe("DataWindow.Data.XML")
li_pos  =  Pos ( ls_xml, "UTF-16LE")
ls_xml  =  Replace ( ls_xml, li_pos, 8,  "UTF-8")
li_file    =    FileOpen (  "CIPTEST.xml", LineMode! , Write! , LockWrite!, Replace!, EncodingUTF8!   )
IF    li_file > 0 THEN
    FileWrite ( li_file, ls_xml )
    FileClose ( li_file )
END IF

4) Here is what the final result looks for me in UTF-8 format. It verifies OK too  ...

HTH

Regards ... Chris

Comment
  1. Miguel Leeuwe
  2. Saturday, 2 November 2019 05:06 AM UTC
Nice one!
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Saturday, 2 November 2019 06:15 AM UTC
  2. PowerBuilder
  3. # 4

Hi,

Just a warning. Before I said to use the FileEncoding function. Be careful with that one as there's a bug:

https://www.appeon.com/standardsupport/search/view?id=3573

 

Chris' solution also seems easier to implement than mine.

Cheers!

Comment
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Thursday, 31 October 2019 07:27 AM UTC
  2. PowerBuilder
  3. # 5

Hi Jahir, that's surprising. I would expect it to be saved as ANSI, since the parameter is not supposed to do anything for XML format. 

From the PB help on SaveAs() it's supposed to end up as ANSI:

encoding (optional for PowerBuilder)

Character encoding of the file to which the data is saved. This parameter applies only to the following formats: TEXT, CSV, SQL, HTML, and DIF. If you do not specify an encoding parameter, the file is saved in ANSI format. Values are:

  • EncodingANSI! (default)

  • EncodingUTF8!

  • EncodingUTF16LE!

  • EncodingUTF16BE!

 

Anyway, I don't know if anyone knows an easier way, but I guess you'd have to convert the resulting file to UTF8 somehow after doing the SaveAs().

So use FileOpen(), Blob and String Conversion FileReadEx, FileWriteEx, etc. Here's an example which does not do the exact conversion that you want, but will give you an idea of how to do the conversion.

From the Powerbuilder Help on FileEncoding(), You'll also have to do a FileOpen() in streammode for the data that you are going to write as UTF8. See the help on FileOpen() and FileEncoding().

From the help on FileEncoding():

long ll_filenum
integer li_bytes
string ls_unicode
blob  lb_ansi
encoding eRet

ll_filenum = FileOpen("employee.dat", StreamMode!, Read!, LockWrite!, Replace!)

// test the file's encoding
eRet = FileEncoding("employee.dat")

if eRet = EncodingANSI! then
   li_ bytes = FileReadEx(ll_filenum, lb_ansi)
   ls_unicode = string(lb_ansi, EncodingANSI!)
else 
   li_ bytes = FileReadEx(ll_filenum, ls_unicode)
end if
FileClose(ll_filenum)

// You would add the code here for writing the "ls_unicode" to a file which you have opened with encoding UTF8
......
 
HTH, regards

 

Comment
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Thursday, 31 October 2019 17:52 PM UTC
  2. PowerBuilder
  3. # 6

Hi Jahir;

    The SaveAs() command does not control the XML encoding scheme. The encoding option at the end of the SaveAs() command is meant for more for text, SQL, CSV, etc file types. The DataWindow object will always default to UTF-16LE internally for any XML generation.

  The XML encoding is actually controlled within the DataWindow object's properties from the Export/Import XML pane. You can change the encoding type in the DW painter, as follows ...

HTH

Regards ... Chris

Comment
There are no comments made yet.
Ken Guo @Appeon Accepted Answer Pending Moderation
  1. Monday, 4 November 2019 09:58 AM UTC
  2. PowerBuilder
  3. # 7

Hi Jahir,

A DataWindow will be saved as XML based on the XML Template configured in it. If it is Encoding UTF-8 in the XML template, it will be saved as Encoding UTP-8.

Steps:

  1. Go to DataWindow Painter > Template – XML Window.
  2. Refer to Chris’s method to modify the XML’s Encoding to UTF-8.
  3. Save the template as ‘template_utf8’.
  4. Go to the Export Painter of the DataWindow, set the Use Template to ‘template_utf8’.

Of course, you can also:

  1. save multiple XML templates in the Template – XML Window in DataWindow Painter.
  2. Make some of them use UTF-8, while some of them use UTF-16LE.
  3. Use different template names to save them separately.
  4. Before the DW executes saveas XML, you can use the following Modify statements to switch the XML templates:

dw_1.modify("DataWindow.Export.XML.UseTemplate='template_utf8'")
dw_1.saveas()  //save xml with UTF8 Template

dw_1.modify("DataWindow.Export.XML.UseTemplate='template_utf16'")
dw_1.saveas()  //save xml with UTF16 Template

 

Regards,

Ken

Comment
  1. Chris Pollach @Appeon
  2. Monday, 4 November 2019 15:45 PM UTC
Thanks Ken ... The DW XML Template route was what I was trying to get Jahir to use as the 1st option. This works great for me for UTF-8 output via the SaveAs() command. :-)
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.