Create file in UTF8 with BOM encoding

Hello,
I need output file to be in UTF8 with BOM encoding, but all I can get is UTF8 without BOM. Can anyone suggest how can I make the encoding convertion or convert document to string straight with BOM?
Thanks in advice.

Did you check pub.string:bytesToString. For more details refer BIS

ignoreBOMChars - Flag indicating whether or not Integration Server removes the byte order mark (BOM) characters in the input sequence of bytes before converting the byte array to string.
Set to: true to remove the byte order mark (BOM) characters before converting the input sequence of bytes to string, if the byte array contains BOM characters.
false to include the byte order mark (BOM) characters while converting the input sequence of bytes to string. The default is false.

Let me know if you need more details.

Hey,
Thanks for the answer, but we are still using webMethods 7 and seems that bytesToString does have that flag in this version yet :frowning:

Ok, write a java service.

Can you confirm if utf-16 is what your looking for.

I was trying to replace HEX by adding BOM (FEFF) but getting unreadable output file, like
þÿ<?xml version="1.0"?>£ÇFç3¤Ö?7W6T?æfòa?ÖÆç3§?6?Ò&?GG¢ò÷wwrçs2æ÷… on the output.

Also I’ve just tried to put EFBBBF as BOM, got the same unreadable result.

Can you check and work on this code. See if it works for you.


IDataCursor cursor =pipeline.getCursor();
       String xmlstring ="";
       boolean littleEnd = false;
       String encoding = "UnicodeBig";

       if (cursor.first("xmlstring")) {
          xmlstring = (String)cursor.getValue();
       }
       if (cursor.first("littleEndian")) {
          littleEnd =IDataUtil.getBoolean(cursor);
          if (littleEnd) encoding ="UnicodeLittle";
       }
       if (xmlstring.length() == 0) return;
       StringBuffer sb = new StringBuffer(xmlstring);
       
       if (sb.charAt(0) !=; '\uFEFF') {
         sb.insert(0, '\uFEFF');
       }
       xmlstring =sb.toString();
       try {
          cursor.insertAfter("bytes", xmlstring.getBytes(encoding));
       } 
catch (UnsupportedEncodingException uex) {
        
         ServiceException se =; new ServiceException();
         se.fillInStackTrace();
         throw se;
       }

       cursor.destroy();

Hey,
I’ve modifyed code a bit for my needs and it worked perfectly. Thanks for your help.

Hey that’s cool :wink:

Do you mind sharing the working code here…

Thanks

Sure thing. Here it is:

IDataCursor pipelineCursor =pipeline.getCursor();  
   // documentSrc
   String xmlString = IDataUtil.getString( pipelineCursor, "xmlString");
   String encoding = IDataUtil.getString( pipelineCursor, "encoding");

   pipelineCursor.destroy();
 
   // pipeline
   IDataCursor pipelineCursor_1 = pipeline.getCursor();

       if (xmlString.length() == 0) return;  
       StringBuffer sb = new StringBuffer(xmlString);  
         
       if (sb.charAt(0) != '\uFEFF') {  
         sb.insert(0, '\uFEFF');  
       }  
       xmlString = sb.toString();  
       try {  
          pipelineCursor.insertAfter("bytes", xmlString.getBytes(encoding));  
       }   
catch (UnsupportedEncodingException uex) {  
          
         ServiceException se = new ServiceException();  
         se.fillInStackTrace();  
         throw se;  
       }  
      IDataUtil.put( pipelineCursor_1, "xmlStringWithBOM", xmlString );
      pipelineCursor_1.destroy();

The only thing is that now I’m trying to make it work depending on input encoding and just can’t get part where I’m setting BOM as ‘\uEFBBBF’ to work.

Seems that encoding doesn’t matter what BOM code to add in this case, so only \uFEFF can be left.