1. Alexandre Quintela
  2. PowerBuilder
  3. Tuesday, 14 February 2023 11:37 AM UTC

How to create XML files in UTF-8?

It seems that windows always produces UTF-16LE files, but I need it to produce in UTF-8.


Example code I've already tried:

PBDOM_Document xml_doc
PBDOM_ProcessingInstruction xml_process
PBDOM_Element xml_root
constant string root_name = "root_node"

try
	xml_doc = create PBDOM_Document
	xml_doc.newdocument(root_name)
	
	xml_process = create PBDOM_ProcessingInstruction
	xml_process.setname("xml")
	xml_process.setvalue("version", "1.0")
	xml_process.setvalue("encoding", "utf-8")
	// it seems that windows always produce utf16 files
	
	xml_doc.addcontent(xml_process)
	destroy xml_process
	
	xml_root = xml_doc.getrootelement()
	
	// (....)
	
	xml_doc.savedocument("foobar.xml")
	destroy xml_root
	destroy xml_doc

CATCH ( PBDOM_Exception pbde )
	MessageBox( "PBDOM Exception",pbde.getMessage() )
CATCH ( PBXRuntimeError re )
	MessageBox( "PBNI Exception",re.getMessage() )
end try

I use Powerbuilder 2021 Build 1311

;) grateful

Andreas Mykonios Accepted Answer Pending Moderation
  1. Tuesday, 14 February 2023 12:20 PM UTC
  2. PowerBuilder
  3. # 1

Hi.

Instead of saving xml using PBDOM you could create the file yoursefl.

long ll_fileno

ls_xml =  xml_doc.SaveDocumentIntoString()

ll_fileno = fileopen("c:\tests\test.xml", TextMode!, write!, lockwrite!)

// Or even better (based on Mark's Goldsmith comment)
// ll_fileno = fileopen("c:\tests\test.xml", TextMode!, write!, lockwrite!, Replace!, EncodingUTF8!)

filewriteex(ll_fileno, ls_xml)
fileclose(ll_fileno)

Andreas.

Comment
  1. Mark Goldsmith
  2. Tuesday, 14 February 2023 14:30 PM UTC
Hi Andreas...I'm not sure if Alexandre at some future time will need to further process the XML file using PowerBuilder File Functions but in the event he does, it may be best to specify EncodingUTF8! in the FileOpen() call such that the BOM character codes are included at the beginning of the file (just my 2 cents).

Regards...Mark
  1. Helpful 2
  1. Andreas Mykonios
  2. Tuesday, 14 February 2023 14:33 PM UTC
Hi Mark.

You are right.
  1. Helpful
  1. Andreas Mykonios
  2. Tuesday, 14 February 2023 14:38 PM UTC
I did a correction as a comment in code.
  1. Helpful 2
There are no comments made yet.
Andreas Mykonios Accepted Answer Pending Moderation
  1. Tuesday, 14 February 2023 12:58 PM UTC
  2. PowerBuilder
  3. # 2

A string can be up to 1073741823 chars. That's why I used filewriteex.

Of course this is a limitation.

Andreas.

Comment
  1. Miguel Leeuwe
  2. Tuesday, 14 February 2023 13:12 PM UTC
Hi,

Just a warning: Powerbuilder is pretty unclear on how much heap memory you have available for for exampe a FileWriteEx(). The other day it blew up on writing only 50 MB. Probably it will behave better if you have a 64 bit executable (which I don't).

regards
  1. Helpful 2
  1. Alexandre Quintela
  2. Tuesday, 14 February 2023 13:13 PM UTC
Thanks! :)
  1. Helpful 1
  1. Andreas Mykonios
  2. Tuesday, 14 February 2023 13:15 PM UTC
Hi Miguel.

You are right.

Andreas.
  1. Helpful 1
There are no comments made yet.
Alexandre Quintela Accepted Answer Pending Moderation
  1. Tuesday, 14 February 2023 12:52 PM UTC
  2. PowerBuilder
  3. # 3

Hi, Andreas,


I'm concerned with the length a string can take.

The proposed solution worked.


Thanks. ;)

Alexandre.

Comment
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.