XML文件和ABAP内表数据的相互转换(基础篇)
Posted SAP ABAP学习屋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XML文件和ABAP内表数据的相互转换(基础篇)相关的知识,希望对你有一定的参考价值。
实现效果:将如下的两张表和XML文件相互转换(建议将代码直接COPY到GUI里面去看)
ABAP的两张内表:(偷了个懒,看不懂的话可以在下面的程序中去看内表的值)
XML文件:(可以在调试模式用XML浏览器格式查看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>
实现步骤:
T_CODE:STRANS.
转换编辑器的使用前提:ECC5.0版本以上才能使用
1:输入转换程序名称,点击新建(内表->XML,XML->内表用同样的转换程序)
2:输入描述,转换类型选择简单转换
输入转换代码(亲,注意看里面的注释奥)
<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">
<!--<!—需要转换的内表名称,有几个传几个-->
<tt:root name="baseReturnHeader"/>
<tt:root name="BaseReturnItem"/>
<tt:template>
<out>
<headers>
<!--<!—Headers XML文件抬头表名称-->
<tt:loop name="Headers" ref="baseReturnHeader">
<baseReturnHeader>
<!--<!—XML字段名称-->
<hresv1>
<!--<!—ABAP内表字段名称-->
<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>
</tt:template>
</tt:transform>
3.此时去新建一个程序,贴上如下代码,即可看到两者互相转换的事件实现
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 0 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 ZTEST
*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 ZTEST
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.
以上是关于XML文件和ABAP内表数据的相互转换(基础篇)的主要内容,如果未能解决你的问题,请参考以下文章