Peter
Peter

Reputation: 473

Save XML generated using DB2 for i into ASCII file

I am trying to write RPGLE that would generate an XML using SQL and save it to IFS. The problem I am running into is that when copying XML from IFS using a network share, the XML is not automatically translated from EBCDIC to ASCII. I have tried creating the file first with correct CCSID, but that seemed to get ignored. Only way I was able to overcome this is to use CPY and translate while coping. I am just hoping there is a cleaner way.

 File_Out_FO   = SQFOVR;
 File_Out_NAME = '/ifs/path/test.xml';
 File_Out_NL   = %Len(%TrimR(File_Out_NAME));
 EXEC SQL
   WITH
     elements AS (
         SELECT
          XMLELEMENT(NAME "element",
           XMLFOREST(
            field1 AS "field1",
            field2 AS "field2",
            field3 AS "field3"
           )
          ) AS element
         FROM table1
     )
     SELECT
      XMLSERIALIZE(
       XMLDOCUMENT(
        XMLELEMENT(NAME "document",
         XMLELEMENT(NAME "elements",
          XMLAGG(elements.element)
         )
        )
       ) AS CLOB  INCLUDING XMLDECLARATION
      ) AS response
     INTO :File_Out
     FROM elements  ;

Upvotes: 0

Views: 1290

Answers (2)

Martin Hieden
Martin Hieden

Reputation: 116

To get the data in ASCII add CCSID 1208 (819 is not possible) in the XmlSerialize function and make sure that the ifs-file doesn't exist. Otherwise it would keep the file CCSID

  XMLSERIALIZE(
   XMLDOCUMENT(
    XMLELEMENT(NAME "document",
     XMLELEMENT(NAME "elements",
      XMLAGG(elements.element)
     )
    )
   ) AS CLOB CCSID 1208 INCLUDING XMLDECLARATION
  ) AS Response

And make sure that your machine QCCSID is set to something other than 65535 (that's always causing lots of Problems with conversion aka not converting automatically).

Upvotes: 2

Jairo R. Flores
Jairo R. Flores

Reputation: 734

First you have to create the file with windows codepage

  // C Language IFS Prototypes

  //-----------------------------------------------------------------
  // createTempSTMF():  Creates a file name for a temporary stream file
  //
  //   filename = (input) path to file in the IFS
  //-----------------------------------------------------------------
 DcreateTempSTMF   PR              *   extproc('_C_IFS_tmpnam')
 D string                        39A   options(*omit)

  //-----------------------------------------------------------------
  // removeSTMF():  Deletes the defined streamed file.
  //
  //   filename = (input) path to file in the IFS
  //-----------------------------------------------------------------
 DremoveSTMF       PR            10I 0 extproc('_C_IFS_remove')
 D filename                        *   VALUE OPTIONS( *String)

  //-----------------------------------------------------------------
  // openSTMF():  Open File for buffered reading/writing
  //
  //   filename = (input) path to file in the IFS
  //       mode = (input) various open mode flags.  (see manual)
  //
  //  returns *NULL upon error, or a pointer to a FILE structure
  //-----------------------------------------------------------------
 dopenSTMF         PR                  extproc('_C_IFS_fopen')
 d                                     like(pFILE)
 d filename                        *   value options(*string)
 d mode                            *   value options(*string)

  //-----------------------------------------------------------------
  // closeSTMF(): Close File
  //    stream = (input) pointer to FILE structure to close
  //-----------------------------------------------------------------
 dcloseSTMF        PR            10i 0 extproc('_C_IFS_fclose')
 dparStream                            like(pFILE) value

 dpFile            s               *   based(prototype_only)
 d fd              s                   like(openSTMF)


    //Unique filename in /tmp
    tempFileName = %trim(%str(createTempSTMF(*omit)));

    // create new output file
    fd = openSTMF(%trim(tempFileName): 'w codepage=1252');
    if (fd = *NULL);
       *INLR = *ON;
       return;
    endif;

Then you reopen it with text mode so the translation occurs automatically

    // ------------------------------------------
    // close file & reopen in text mode so that
    // data will be automatically translated
    // ------------------------------------------
    closeSTMF(fd);
    fd = openSTMF( %trim(tempFileName) : 'a codepage=37');
    if (fd = *NULL);
       *INLR = *ON;
       return;
    endif;

Then write some data

     //Write some data
     fputsSTMF('Some data': fd);

Upvotes: 0

Related Questions