当有时收到xml报文,但是没法解析时,可用该类按照报文生成特定的内表
具体代码可参考文章ABAP 动态创建变量,构造,表
如果想按照xml报文生成内表可用下面这段代码,定义部分可参考上面的代码。
DATA:LV_BXML TYPE STRING . "头
DATA:LV_TXML TYPE STRING . "体
DATA:LV_EXML TYPE STRING . "尾
CLEAR:GT_COMP,GT_COMP[].
"创建基本类型
GV_TYPE ?= cl_abap_elemdescr=>GET_STRING( ).
REPLACE ALL OCCURRENCES OF '/' IN PV_XML WITH ``.
GT_COMP-TYPE = GV_TYPE."新增成员的类型对象
LV_EXML = PV_XML .
DO .
SPLIT LV_EXML AT '<' INTO LV_BXML LV_EXML .
IF SY-SUBRC = 0 AND LV_EXML IS NOT INITIAL.
CLEAR :LV_TXML .
SPLIT LV_EXML AT '>' INTO LV_TXML LV_EXML .
IF SY-SUBRC = 0 AND LV_TXML IS NOT INITIAL.
GT_COMP-NAME = LV_TXML .
APPEND GT_COMP TO GT_COMP[] .
ELSE.
EXIT .
ENDIF.
ELSE .
EXIT .
ENDIF.
ENDDO.
SORT GT_COMP BY NAME .
DELETE ADJACENT DUPLICATES FROM GT_COMP COMPARING ALL FIELDS.
"根据新的组件创建结构类型
GS_TYPE_STR = cl_abap_structdescr=>create( GT_COMP[] ).
"根据结构类型,创建结构对象
CREATE DATA GS_DREF_STR TYPE HANDLE GS_TYPE_STR.
*引用创建的动态构造
IF GS_DREF_STR IS BOUND.
ASSIGN GS_DREF_STR->* TO <FS_STR>.
ENDIF.
* 基于结构类型对象创建内表类型对象
GS_TYPE_TAB = cl_abap_tabledescr=>create( GS_TYPE_STR ).
"使用内表类型对象来创建内表类型
CREATE DATA GS_DREF_TAB TYPE HANDLE GS_TYPE_TAB.
*引用构建的表
IF GS_DREF_TAB IS BOUND.
ASSIGN GS_DREF_TAB->* TO <FS_TAB>.
ENDIF.
生成完成内表后,就可以循环内表解析xml报文存储到动态内表了,代码如下
DATA:LV_BXML TYPE STRING . "头
DATA:LV_TXML TYPE STRING . "体
DATA:LV_EXML TYPE STRING . "尾
DATA:LV_BCHAR TYPE STRING . "头组件
DATA:LV_ECHAR TYPE STRING . "尾组件
LV_EXML = PV_XML .
LOOP AT GT_COMP.
LV_BCHAR = '<' && GT_COMP-NAME && '>' .
LV_ECHAR = '</' && GT_COMP-NAME && '>' .
SPLIT LV_EXML AT LV_BCHAR INTO LV_BXML LV_EXML .
IF SY-SUBRC = 0 AND LV_EXML IS NOT INITIAL.
CLEAR :LV_TXML .
SPLIT LV_EXML AT LV_ECHAR INTO LV_TXML LV_EXML .
IF SY-SUBRC = 0 AND LV_TXML IS NOT INITIAL.
ASSIGN COMPONENT GT_COMP-NAME OF STRUCTURE <FS_STR> TO <FS_FIELD> .
IF <FS_FIELD> IS ASSIGNED.
<FS_FIELD> = LV_TXML .
ENDIF.
ELSE.
CONTINUE .
ENDIF.
ELSE .
CONTINUE .
ENDIF.
ENDLOOP.
COLLECT <FS_STR> INTO <FS_TAB> .
CLEAR <FS_STR> .