今天在写银企直连的报文日志时,需要显示出报文内容,有的报文是XML的,的是字符串的(比如工商银行的交易明细接口),所以查一下,找到如一方法,发现类:cl_demo_output,可以显示XML,JSON,STRING.
下面的例子中显示XML用了另外的一个类开显示XML,其实cl_demo_output=>DISPLAY_XML,应该也是可以显示的。
STRING显示
其中P_CFILE就是要显示的报文字符串内容。
1 2 3 4 5 6 7 |
FORM PF_TEXT_DISPLAY USING P_CFILE . DATA: L_TEXT TYPE REF TO CL_DEMO_OUTPUT . CALL METHOD cl_demo_output=>DISPLAY_TEXT EXPORTING TEXT = P_CFILE. ENDFORM. |
XML显示,
由于的我报文中刚好有二进制的报文内容,所以就使用了这个方法
1 2 3 4 5 6 7 8 9 10 11 |
FORM XML_DISPLAY USING P_XML_STR. DATA: L_XML TYPE REF TO CL_XML_DOCUMENT . CREATE OBJECT L_XML. CALL METHOD L_XML->PARSE_XSTRING EXPORTING STREAM = P_XML_STR. "xml_out is the variable which is holding the xml string CALL METHOD L_XML->DISPLAY. ENDFORM. |
在程序中还使用到了一个字符串转二进制的FORM,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
FORM FRM_STRING_TO_XSTRING USING IV_STRING TYPE STRING IV_CONVERT_CP TYPE OS_BOOLEAN "'X' IV_CODEPAGE TYPE ABAP_ENCOD IV_ADD_BOM TYPE OS_BOOLEAN CHANGING EV_XSTRING TYPE XSTRING . DATA LO_CONV TYPE REF TO CL_ABAP_CONV_OUT_CE. DATA LV_BOM TYPE XSTRING. DATA LV_XBUF TYPE XSTRING. DATA LV_CP TYPE ABAP_ENCOD. TRY. IF IV_CONVERT_CP IS INITIAL. EXPORT P = IV_STRING TO DATA BUFFER LV_XBUF. IMPORT P = EV_XSTRING FROM DATA BUFFER LV_XBUF IN CHAR-TO-HEX MODE. ELSE. IF IV_CODEPAGE IS INITIAL. LV_CP = CL_SX_MIME_SINGLEPART=>GET_SX_NODE_CODEPAGE( ). ELSE. LV_CP = IV_CODEPAGE. ENDIF. * convert string to xstring using class cl_abap_conv_out_ce * in this form available also in 620 LO_CONV = CL_ABAP_CONV_OUT_CE=>CREATE( ENCODING = LV_CP IGNORE_CERR = 'X' ). LO_CONV->WRITE( DATA = IV_STRING ). EV_XSTRING = LO_CONV->GET_BUFFER( ). * add the byte order mark IF IV_ADD_BOM = 'X'. CASE LV_CP. WHEN '4110'. "UTF-8 LV_BOM = CL_ABAP_CHAR_UTILITIES=>BYTE_ORDER_MARK_UTF8. WHEN '4102'. "UTF-16BE LV_BOM = CL_ABAP_CHAR_UTILITIES=>BYTE_ORDER_MARK_BIG. WHEN '4103'. "UTF-16LE LV_BOM = CL_ABAP_CHAR_UTILITIES=>BYTE_ORDER_MARK_LITTLE. ENDCASE. IF LV_BOM IS NOT INITIAL. CONCATENATE LV_BOM EV_XSTRING INTO EV_XSTRING IN BYTE MODE. ENDIF. ENDIF. ENDIF. CATCH CX_ROOT. "#EC * RAISE EXCEPTION TYPE CX_BCS EXPORTING ERROR_TYPE = CX_BCS=>CREATION_FAILED. ENDTRY. ENDFORM. |
使用时如下
1 2 |
PERFORM FRM_STRING_TO_XSTRING USING LS_ALV-CFILE '' '' '' CHANGING LS_ALV-XFILE |