事务代码STRANS的使用(XML与ABAP内表相互转换)

Posted wuqingbo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了事务代码STRANS的使用(XML与ABAP内表相互转换)相关的知识,希望对你有一定的参考价值。

ABAP ---> XML  and XML --->ABAP

要求的XML格式如下:

<handleMLPurchaseOrderServiceResponse>
<out>
<headers>
<baseReturnHeader>
<hresv1>4500524641</hresv1>
<hresv2/>
<hresv3/>
<hresv4/>
<hresv5/>
<returnMsg>订单抬头数据更新成功哦!</returnMsg>
<status>S</status>
<uuid/>
</baseReturnHeader>
<baseReturnHeader>
<hresv1>4500524641</hresv1>
<hresv2/>
<hresv3/>
<hresv4/>
<hresv5/>
<returnMsg>订单抬头数据更新成功哦!</returnMsg>
<status>S</status>
<uuid/>
</baseReturnHeader>
</headers>
<items>
<BaseReturnItem>
<hresv1>4500524642</hresv1>
<hresv2>00010</hresv2>
<hresv3/>
<hresv4/>
<hresv5/>
<returnMsg>行项目新增成功哟!</returnMsg>
<status>S</status>
<uuid/>
</BaseReturnItem>
<BaseReturnItem>
<hresv1>4500524641</hresv1>
<hresv2/>
<hresv3/>
<hresv4/>
<hresv5/>
<returnMsg>行项目更新成功哟!</returnMsg>
<status>S</status>
<uuid/>
</BaseReturnItem>
<BaseReturnItem>
<hresv1>4500524641</hresv1>
<hresv2/>
<hresv3/>
<hresv4/>
<hresv5/>
<returnMsg>行项目更新成功哟!</returnMsg>
<status>S</status>
<uuid/>
</BaseReturnItem>
</items>
</out>
</handleMLPurchaseOrderServiceResponse>

 

事务代码Strans创建转换ZQBTEST_SAMPLE3

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">

  <tt:root name="baseReturnHeader"/>
  <tt:root name="BaseReturnItem"/>

  <tt:template>
    <handleMLPurchaseOrderServiceResponse>
      <out>
        <headers>
          <tt:loop name="Headers" ref=".baseReturnHeader">
            <baseReturnHeader>
              <hresv1>
                <tt:value ref="$ref.hresv1"/>
              </hresv1>
              <hresv2>
                <tt:value ref="$ref.hresv2"/>
              </hresv2>
              <hresv3>
                <tt:value ref="$ref.hresv3"/>
              </hresv3>
              <hresv4>
                <tt:value ref="$ref.hresv4"/>
              </hresv4>
              <hresv5>
                <tt:value ref="$ref.hresv5"/>
              </hresv5>
              <returnMsg>
                <tt:value ref="$ref.returnMsg"/>
              </returnMsg>
              <status>
                <tt:value ref="$ref.status"/>
              </status>
              <uuid>
                <tt:value ref="$ref.uuid"/>
              </uuid>
            </baseReturnHeader>
          </tt:loop>
        </headers>
        <items>

     <tt:loop name="Items" ref=".BaseReturnItem">
           <BaseReturnItem>
              <hresv1>
                <tt:value ref="$ref.hresv1"/>
              </hresv1>
              <hresv2>
                <tt:value ref="$ref.hresv2"/>
              </hresv2>
              <hresv3>
                <tt:value ref="$ref.hresv3"/>
              </hresv3>
              <hresv4>
                <tt:value ref="$ref.hresv4"/>
              </hresv4>
              <hresv5>
                <tt:value ref="$ref.hresv5"/>
              </hresv5>
              <returnMsg>
                <tt:value ref="$ref.returnMsg"/>
              </returnMsg>
              <status>
                <tt:value ref="$ref.status"/>
              </status>
              <uuid>
                <tt:value ref="$ref.uuid"/>
              </uuid>
           </BaseReturnItem>

    </tt:loop>
        </items>
      </out>
    </handleMLPurchaseOrderServiceResponse>
  </tt:template>

</tt:transform>

 

SE38创建测试的报表程序ZQBTEST57

*&---------------------------------------------------------------------*
*& Report  ZQBTEST57
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zqbtest57.

DATA: BEGIN OF basereturnheader OCCURS 0,
        hresv1 TYPE c LENGTH 20,
        hresv2 TYPE c LENGTH 20,
        hresv3 TYPE c LENGTH 20,
        hresv4 TYPE c LENGTH 20,
        hresv5 TYPE c LENGTH 20,
        returnmsg TYPE c LENGTH 20,
        status TYPE c LENGTH 20,
        uuid TYPE c LENGTH 20,
      END OF basereturnheader.
DATA: basereturnitem LIKE basereturnheader OCCURS WITH HEADER LINE.

DATA: xml_string TYPE string.
DATA: lo_oref TYPE REF TO cx_st_error.
DATA: lv_msg TYPE string .


CLEAR basereturnheader.
basereturnheader-hresv1 = \'4500524641\'.
basereturnheader-returnmsg = \'抬头成功\'.
basereturnheader-status = \'S\'.
APPEND basereturnheader.

CLEAR basereturnheader.
basereturnheader-hresv1 = \'4500524642\'.
basereturnheader-returnmsg = \'抬头成功\'.
basereturnheader-status = \'S\'.
APPEND basereturnheader.

CLEAR basereturnitem.
basereturnitem-hresv1 = \'4500524641\'.
basereturnitem-hresv2 = \'00010\'.
basereturnitem-returnmsg = \'行项成功\'.
basereturnitem-status = \'S\'.
APPEND basereturnitem.

CLEAR basereturnitem.
basereturnitem-hresv1 = \'4500524641\'.
basereturnitem-hresv2 = \'00020\'.
basereturnitem-returnmsg = \'行项成功\'.
basereturnitem-status = \'S\'.
APPEND basereturnitem.

CLEAR basereturnitem.
basereturnitem-hresv1 = \'4500524642\'.
basereturnitem-hresv2 = \'00010\'.
basereturnitem-returnmsg = \'行项失败\'.
basereturnitem-status = \'E\'.
APPEND basereturnitem.

TRY .
    BREAK-POINT.
    CALL TRANSFORMATION zqbtest_sample3
*OPTIONS xml_header = \'no\'
    SOURCE basereturnheader =  basereturnheader[]
           basereturnitem = basereturnitem[]
    RESULT XML xml_string.

  CATCH cx_st_error INTO lo_oref .
    lv_msg  = lo_oref->get_text( ) .
    WRITE: / \'Error message:\',lv_msg.
ENDTRY.

WRITE: AT /1 xml_string.

CLEAR basereturnheader[].
CLEAR basereturnitem[].

TRY .
    BREAK-POINT.
    CALL TRANSFORMATION zqbtest_sample3
    SOURCE XML xml_string
    RESULT basereturnheader =  basereturnheader[]
           basereturnitem = basereturnitem[].
  CATCH cx_st_error INTO lo_oref .
    CLEAR: lv_msg .
    lv_msg  = lo_oref->get_text( ) .
    WRITE: / \'Error message:\',lv_msg.
ENDTRY.

WRITE:/ \'*** here is header\'.
LOOP AT basereturnheader.
  WRITE:/ basereturnheader-hresv1,basereturnheader-hresv2,basereturnheader-returnmsg.
  CLEAR basereturnheader.
ENDLOOP.
WRITE:/ \'*** here is items\'.
LOOP AT basereturnitem.
  WRITE:/ basereturnitem-hresv1,basereturnitem-hresv2,basereturnitem-returnmsg.
  CLEAR basereturnitem.
ENDLOOP.

 

运行结果:

 

以上是关于事务代码STRANS的使用(XML与ABAP内表相互转换)的主要内容,如果未能解决你的问题,请参考以下文章

Strans 内表-xml文件

XML文件与ABAP结构/内表的转换

XML文件和ABAP内表数据的相互转换(基础篇)

XML和ABAP内表转换注意事项

关于SAP ABAP table control 显示数据、修改数据的问题

详解ABAP/4内表结构