Today I will share a good way to use abap source code to dynamic get the field name/value of structure ,
get the name/value of inner table . And concacrate them to a standard XML element string.
Below is the sample source code demo. Hope you like it,Or u can ask CHATGPT. It is amazing.
REPORT hot583.
DATA lt TYPE STANDARD TABLE OF dd02t.
DATA lwa TYPE dd03t.
DATA LV_XML TYPE STRING.
select * FROM dd02t
INTO CORRESPONDING FIELDS OF TABLE lt
UP TO 2 ROWS.
select * FROM dd03t
INTO CORRESPONDING FIELDS OF lwa.
exit.
ENDSELECT.
*xml dynamic table demo.
PERFORM FRM_GET_XML USING ‘@’ ‘L’ “low case.
lt CHANGING LV_XML.
ULINE.
WRITE:/ LV_XML.
ULINE.
*xml dynamic work area structure .
PERFORM FRM_GET_XML USING ‘@’ ‘L’ “low case.
lwa CHANGING LV_XML.
SKIP.
WRITE:/ LV_XML.
FORM FRM_GET_XML USING uv_split
uv_case “L low,H,High.
uWA_INPUT
CHANGING cV_XML.
DATA: lr_tabledescr TYPE REF TO cl_abap_tabledescr.
DATA: lr_structdescr TYPE REF TO cl_abap_structdescr.
FIELD-SYMBOLS <fs_name> TYPE ANY.
CLEAR cV_XML.
DATA:g_fieldnm TYPE txt30.
DATA LV_GOT_TAB TYPE C LENGTH 1.
TRY .
lr_tabledescr ?= cl_abap_typedescr=>describe_by_data( uWA_INPUT ).
lr_structdescr ?= lr_tabledescr->get_table_line_type( ).
LV_GOT_TAB = ‘X’.
CATCH CX_ROOT.
ENDTRY.
IF LV_GOT_TAB IS INITIAL.
lr_structdescr ?= cl_abap_typedescr=>describe_by_data( uWA_INPUT ).
PERFORM FRM_SPLIT_XML USING uv_split
uv_case
uWA_INPUT
lr_structdescr CHANGING LV_XML.
ELSE.
FIELD-SYMBOLS <LFS_LINE> TYPE ANY.
FIELD-SYMBOLS <LFS_TABE> TYPE ANY TABLE.
ASSIGN uWA_INPUT TO <LFS_TABE>.
IF SY–SUBRC = 0.
LOOP AT <LFS_TABE> ASSIGNING <LFS_LINE>.
EXIT.
ENDLOOP.
IF SY–SUBRC <> 0.
insert INITIAL LINE INTO TABLE <LFS_TABE>.
ENDIF.
**********************************************************************
LOOP AT <LFS_TABE> ASSIGNING <LFS_LINE>.
PERFORM FRM_SPLIT_XML USING uv_split
uv_case
<LFS_LINE>
lr_structdescr CHANGING LV_XML.
ENDLOOP.
ENDIF.
ENDIF.
IF uv_split <> space.
shift LV_XML by 1 PLACES LEFT.
ENDIF.
ENDFORM.
*&———————————————————————*
*& Form FRM_SPLIT_XML
*&———————————————————————*
* 拼接XML。
*———————————————————————-*
FORM FRM_SPLIT_XML USING uv_split
UV_SPECIL
uwa_line
ur_componets
CHANGING cV_XML.
DATA: lr_structdescr TYPE REF TO cl_abap_structdescr.
lr_structdescr = ur_componets.
FIELD-SYMBOLS:<fs_comp> TYPE abap_compdescr.
FIELD-SYMBOLS:<LFS_VALUE> TYPE ANY.
DATA LV_VALUE TYPE STRING.
DATA LV_FNAME TYPE C LENGTH 40.
LOOP AT lr_structdescr->components ASSIGNING <fs_comp>.
* WRITE: / <fs_comp>-name, “authority: www.hot583.com
* <fs_comp>-type_kind, “字段类型
* <fs_comp>-length, “字段长度
* <fs_comp>-decimals. “字段小数位
* APPEND <fs_comp>-name TO gt_field.
LV_FNAME = <fs_comp>–name .
ASSIGN COMPONENT <fs_comp>–name OF STRUCTURE uwa_line TO <LFS_VALUE>.
IF SY–SUBRC = 0.
CASE UV_SPECIL.
WHEN ‘L’.
TRANSLATE LV_FNAME TO LOWER CASE.
WHEN ‘C’.
WHEN OTHERS.
ENDCASE.
LV_VALUE = <LFS_VALUE>.
CONDENSE LV_VALUE .
CONCATENATE `<` LV_FNAME `>`
LV_VALUE
`</` LV_FNAME `>`
cV_XML
INTO cV_XML.
ENDIF.
ENDLOOP.
CONCATENATE uv_split cV_XML INTO cV_XML.
ENDFORM. ” FRM_SPLIT_XML
本文为作者原创,如果这篇文章对您有用,欢迎转载,
转载请务必注明来源《今日热点在看》http://www.hot583.com/
微信原文《全文完》