你不找到的话,错误就在那里。你找到了错误才会成为财富!
Strans XML 解析3要素:
1.源xml 格式正常,
eg.
'<?xml version="1.0" encoding="UTF-8"?>' '<handleIMatnrResourceServiceResponse><out>' '<MLMatnrResource> <backup1/> <backup2/> <backup3/> <backup4/> <backup5/> <ebeln>4800064316</ebeln> <ebelp>00010</ebelp> <lifnrAmount/> <maktx>A级冰箱型号标贴_C0414.6-1(A</maktx>' '<matnr>000000000800013563</matnr> <proAmount/> <storeAmount>12.0</storeAmount> </MLMatnrResource>' '</out></handleIMatnrResourceServiceResponse>'
2.XML 解析例程可用,
判断方法:
a.code pass,
b.节点正确,
3.检查表结构对应是否OK,
用这个 以图 片方式编辑简单转换 ---正确和错误的对比没差别,这个方式作用不大!
Debug 进去比对源结构和程序结构是否对应,可以检查结构问题
<?sap.transform simple?> <tt:transform xmlns:tt="http://www.sap.com/transformation-templates"> <tt:root name="MLMatnrResource"/> <tt:template> <handleIMatnrResourceServiceResponse> <out> <tt:loop name="out" ref=".MLMatnrResource"> <MLMatnrResource> <backup1> <tt:value ref="$ref.backup1"/> </backup1> <backup2> <tt:value ref="$ref.backup2"/> </backup2> <backup3> <tt:value ref="$ref.backup3"/> </backup3> <backup4> <tt:value ref="$ref.backup4"/> </backup4> <backup5> <tt:value ref="$ref.backup5"/> </backup5> <ebeln> <tt:value ref="$ref.ebeln"/> </ebeln> <ebelp> <tt:value ref="$ref.ebelp"/> </ebelp> <lifnrAmount> <tt:value ref="$ref.lifnrAmount"/> </lifnrAmount> <maktx> <tt:value ref="$ref.maktx"/> </maktx> <matnr> <tt:value ref="$ref.matnr"/> </matnr> <proAmount> <tt:value ref="$ref.proAmount"/> </proAmount> <storeAmount> <tt:value ref="$ref.storeAmount"/> </storeAmount> </MLMatnrResource> </tt:loop> </out> </handleIMatnrResourceServiceResponse> </tt:template> </tt:transform>
3.RETURN 内表定义要与源数据表 匹配!
实例证明,表结构不对应,解析不了,
CALL TRANSFORMATION ZML_XML_TRANS02"ZML_XML_RESOURCE SOURCE XML Z_OUT-RETURN_DATA RESULT MLMATNRRESOURCE = GT_RE.
FUNCTION ZMLSCP1_FR0008. *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" VALUE(I_EBELN) TYPE EBELN OPTIONAL *" VALUE(I_MATNR) TYPE MATNR OPTIONAL *" EXPORTING *" VALUE(O_STATU) TYPE CHAR1 *" VALUE(O_MSG) TYPE CHAR255 *" TABLES *" OT_RESOURCE STRUCTURE ZSRM_RESOURCE *"---------------------------------------------------------------------- *&Flag 0-手动; 1-自动 DATA:L_TABIX TYPE SY-TABIX. DATA:LV_FLAG TYPE C. DATA:LV_CON TYPE STRING. DATA: ERROR TYPE REF TO CX_AI_SYSTEM_FAULT. DATA: PRXY TYPE REF TO ZCO_REQUEST_IMATNR_RESOURCE_SE."Proxy Name DATA: Z_IN TYPE ZREQUEST8. "request in DATA: Z_OUT TYPE ZRESPONSE8."request out DATA: O_CX TYPE REF TO CX_ROOT. DATA: O_ERROR TYPE REF TO CX_ST_ERROR. TYPES:BEGIN OF TY_RE, BACKUP1 TYPE STRING,"备用字段1 BACKUP2 TYPE STRING,"备用字段2 BACKUP3 TYPE STRING,"备用字段3 BACKUP4 TYPE STRING,"备用字段4 BACKUP5 TYPE STRING,"备用字段5 EBELN TYPE STRING,"采购订单号 EBELP TYPE STRING,"采购订单行项目号 LIFNRAMOUNT TYPE MENGE, "供应商库存数量 "结构-MENGE:物料主记录视图: 可选的物料数量 MAKTX TYPE STRING, "物料描述 MATNR TYPE STRING, "物料代码 PROAMOUNT TYPE MENGE, "生产数量 STOREAMOUNT TYPE MENGE, "线边仓库存数量 PRODATE TYPE DATUM, "生产日期 END OF TY_RE. DATA:GT_RE TYPE STANDARD TABLE OF TY_RE, WA_RE LIKE LINE OF GT_RE. *&job user IF SY-UNAME EQ ''. LV_FLAG = '1'. ELSE. LV_FLAG = '0'. ENDIF. CONCATENATE '<in0>' LV_FLAG '</in0>' '<in1>' I_EBELN '</in1>' '<in2>' I_MATNR '</in2>' INTO LV_CON. *&s1.call proxy get data via cis * Z_IN-REQUEST_DATA = '<?xml version="1.0" encoding="UTF-8"?><handleIMatnrResourceService><in0>' && LV_FLAG && '</in0></handleIMatnrResourceService>'. CONCATENATE '<?xml version="1.0" encoding="UTF-8"?><handleIMatnrResourceService>' LV_CON '</handleIMatnrResourceService>' INTO Z_IN-REQUEST_DATA. CLEAR ERROR. TRY. CREATE OBJECT PRXY EXPORTING LOGICAL_PORT_NAME = 'ZMLSCP_GET_RESOURCE'. CALL METHOD PRXY->REQUEST EXPORTING INPUT = Z_IN IMPORTING OUTPUT = Z_OUT. CATCH CX_AI_SYSTEM_FAULT INTO ERROR. O_STATU = 'E'. O_MSG = ERROR->ERRORTEXT. RETURN. ENDTRY. *&1.1 is it ok? IF Z_OUT-RETURN_CODE = 'E'. O_STATU = 'E'. O_MSG = Z_OUT-RETURN_DESC. RETURN. ENDIF. BREAK-POINT. *&s2.transfer XML IF Z_OUT-RETURN_DATA IS NOT INITIAL."业务系统返回报文不为空 TRY . CLEAR:GT_RE,WA_RE. CALL TRANSFORMATION ZML_XML_TRANS02"ZML_XML_RESOURCE SOURCE XML Z_OUT-RETURN_DATA RESULT MLMATNRRESOURCE = GT_RE. * IF SY-SUBRC NE 0. O_STATU = 'E'. O_MSG = 'XML解析失败!'. RETURN. ENDIF. BREAK-POINT. CATCH CX_ST_ERROR INTO O_ERROR . O_STATU = 'E'. O_MSG = O_ERROR->GET_TEXT( ) . ENDTRY. ELSE. O_STATU = 'E'. O_MSG = '返回结果为空!'. ENDIF. *&s3.data save to ZSRM_RESOURCE IF GT_RE[] IS NOT INITIAL. LOOP AT GT_RE INTO WA_RE. MOVE-CORRESPONDING WA_RE TO OT_RESOURCE. OT_RESOURCE-ZDATUM = SY-DATUM. OT_RESOURCE-ZCPUTM = SY-UZEIT. OT_RESOURCE-USNAM = SY-UNAME. OT_RESOURCE-ZFLAG = LV_FLAG. APPEND OT_RESOURCE. * MODIFY GT_RE FROM WA_RE. * CLEAR WA_RE. ENDLOOP. * TRY MODIFY ZSRM_RESOURCE FROM TABLE OT_RESOURCE. IF SY-SUBRC EQ 0. COMMIT WORK AND WAIT . DESCRIBE TABLE OT_RESOURCE LINES L_TABIX. O_STATU = 'S'. O_MSG = '成功存入ZSRM_RESOURCE' && L_TABIX && '条记录!'. ELSE. ROLLBACK WORK. O_STATU = 'E'. O_MSG = '数据存入 ZSRM_RESOURCE表失败!'. ENDIF. ENDIF. ENDFUNCTION.