SAP 将ITAB内表的数据转换为XML字符串 代码实例

前端之家收集整理的这篇文章主要介绍了SAP 将ITAB内表的数据转换为XML字符串 代码实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. 一、将ABAP数据生成XML文件生成到本地
  1. 二、将XML文件解析到SAP
  1. 三、将XML数据生成字符串
  1. 四、用XSLT的方式解析、生成XML自定义XSLT的事务代码strans
  1. 五、下面是具体实现代码
  1. type-pools: IXML,ABAP.
  2. types: begin of XML_LINE,DATA(256) type X,end of XML_LINE.
  3. data: L_IXML type ref to IF_IXML,L_STREAMFACTORY type ref to IF_IXML_STREAM_FACTORY,L_OSTREAM type ref to IF_IXML_OSTREAM,L_RENDERER type ref to IF_IXML_RENDERER,L_DOCUMENT type ref to IF_IXML_DOCUMENT.
  4. data: L_ELEMENT_FLIGHTS type ref to IF_IXML_ELEMENT,L_ELEMENT_AIRLINE type ref to IF_IXML_ELEMENT,L_ELEMENT_FLIGHT type ref to IF_IXML_ELEMENT,L_ELEMENT_DUMMY type ref to IF_IXML_ELEMENT,L_VALUE type STRING.
  5. data: L_XML_TABLE type table of XML_LINE,L_XML_SIZE type I,L_RC type I.
  6. data: LT_SPFLI type table of SPFLI.
  7. data: L_SPFLI type SPFLI.
  8.  
  9. start-of-selection.
  10. * Fill the internal table
  11. select * from SPFLI into table LT_SPFLI.
  12.  
  13. * Sort internal table
  14. sort LT_SPFLI by CARRID.
  15.  
  16. * 生成XML数据
  17. loop at LT_SPFLI into L_SPFLI.
  18.  
  19. at first.
  20. * Creating a ixml factory
  21. L_IXML = CL_IXML=>CREATE( ).
  22. * Creating the dom object model
  23. L_DOCUMENT = L_IXML->CREATE_DOCUMENT( ).
  24. * Fill root node with value flow
  25. L_ELEMENT_FLIGHTS = L_DOCUMENT->CREATE_SIMPLE_ELEMENT(
  26. NAME = 'flow'
  27. PARENT = L_DOCUMENT ).
  28.  
  29.  
  30. L_RC = L_ELEMENT_FLIGHTS->SET_ATTRIBUTE( NAME = 'KEY' VALUE = 'gsgs-cgdd' ).
  31. L_RC = L_ELEMENT_FLIGHTS->SET_ATTRIBUTE( NAME = 'DES' VALUE = '广深公司-采购订单').
  32. L_RC = L_ELEMENT_FLIGHTS->SET_ATTRIBUTE( NAME = 'BAPI' VALUE ='ZBAPI_MM_RK_AFTER_APP' ).
  33.  
  34. L_ELEMENT_AIRLINE = L_DOCUMENT->CREATE_SIMPLE_ELEMENT(
  35. NAME = 'customform'
  36. PARENT = L_ELEMENT_FLIGHTS ).
  37. endat.
  38.  
  39. at new CONNID.
  40.  
  41. L_ELEMENT_FLIGHT = L_DOCUMENT->CREATE_SIMPLE_ELEMENT(
  42. NAME = 'fd'
  43. PARENT = L_ELEMENT_AIRLINE ).
  44.  
  45. L_VALUE = L_SPFLI-CONNID.
  46. L_RC = L_ELEMENT_FLIGHT->SET_ATTRIBUTE( NAME = 'n' VALUE =
  47. 'flight' ).
  48. endat.
  49.  
  50. L_VALUE = L_SPFLI-DEPTIME.
  51. L_ELEMENT_DUMMY = L_DOCUMENT->CREATE_SIMPLE_ELEMENT(
  52. NAME = 'V'
  53. VALUE = L_VALUE
  54. PARENT = L_ELEMENT_FLIGHT ).
  55. endloop.
  56. * Creating a stream factory
  57. L_STREAMFACTORY = L_IXML->CREATE_STREAM_FACTORY( ).
  58. * Connect internal XML table to stream factory
  59. L_OSTREAM = L_STREAMFACTORY->CREATE_OSTREAM_ITABLE( TABLE =
  60. L_XML_TABLE ).
  61. * Rendering the document
  62. L_RENDERER = L_IXML->CREATE_RENDERER( OSTREAM = L_OSTREAM
  63. DOCUMENT = L_DOCUMENT ).
  64. L_RC = L_RENDERER->RENDER( ).
  65.  
  66. *************************************************************
  67. * xml数据导出到本地
  68. * 取得XML数据大小
  69. L_XML_SIZE = L_OSTREAM->GET_NUM_WRITTEN_RAW( ).
  70. * xml数据导出到本地
  71. * call method CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
  72. * exporting
  73. * BIN_FILESIZE = L_XML_SIZE
  74. * FILENAME = 'E:\flights.xml'
  75. * FILETYPE = 'BIN'
  76. * changing
  77. * DATA_TAB = L_XML_TABLE
  78. * exceptions
  79. * others = 24.
  80. * if SY-SUBRC <> 0.
  81. * message id SY-MSGID type SY-MSGTY number SY-MSGNO
  82. * with SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  83. * endif.
  84. ************************************************************
  85.  
  86. ****************************************************
  87. **--将XML数据导入到内表
  88. * data XMLDATA type XSTRING .
  89. * data: RESULT_XML type standard table of SMUM_XMLTB .
  90. * data: RETURN type standard table of BAPIRET2 .
  91. * data: WA_XML type SMUM_XMLTB.
  92. *
  93. *如果需要上载XML可以用一下方法
  94. * call function 'GUI_UPLOAD'
  95. * exporting
  96. * FILENAME = 'E:\flights.xml'
  97. * FILETYPE = 'BIN'
  98. * importing
  99. * FILELENGTH = L_XML_SIZE
  100. * tables
  101. * DATA_TAB = L_XML_TABLE.
  102.  
  103. **--将XML数据导入到内表
  104. * call function 'SCMS_BINARY_TO_XSTRING'
  105. * exporting
  106. * INPUT_LENGTH = L_XML_SIZE
  107. * importing
  108. * BUFFER = XMLDATA
  109. * tables
  110. * BINARY_TAB = L_XML_TABLE
  111. * exceptions
  112. * Failed = 1
  113. * others = 2.
  114. *
  115. * call function 'SMUM_XML_PARSE'
  116. * exporting
  117. * XML_INPUT = XMLDATA
  118. * tables
  119. * XML_TABLE = RESULT_XML
  120. * RETURN = RETURN.
  121. *
  122. * loop at RESULT_XML into WA_XML .
  123. * endloop.
  124.  
  125.  
  126.  
  127. ************************************************
  128.  
  129. **************************************************
  130. *将XML转换成字符串
  131. data: W_STRING type XSTRING.
  132. data LS_XML type STRING.
  133.  
  134. *将XML转换成字符串
  135. call function 'SDIXML_DOM_TO_XML'
  136. exporting
  137. DOCUMENT = L_DOCUMENT
  138. importing
  139. XML_AS_STRING = W_STRING
  140. SIZE = L_XML_SIZE
  141. tables
  142. XML_AS_TABLE = L_XML_TABLE
  143. exceptions
  144. NO_DOCUMENT = 1
  145. others = 2.
  146. if SY-SUBRC <> 0.
  147. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  148. * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  149. endif.
  150.  
  151. * Xstring转换成二进制
  152. * call function 'SCMS_XSTRING_TO_BINARY'
  153. * exporting
  154. * BUFFER = W_STRING
  155. * importing
  156. * OUTPUT_LENGTH = L_XML_SIZE
  157. * tables
  158. * BINARY_TAB = L_XML_TABLE.
  159.  
  160. call function 'SCMS_BINARY_TO_STRING'
  161. exporting
  162. INPUT_LENGTH = L_XML_SIZE
  163. importing
  164. TEXT_BUFFER = LS_XML
  165. tables
  166. BINARY_TAB = L_XML_TABLE
  167. exceptions
  168. Failed = 1
  169. others = 2.
  170.  
  171. if SY-SUBRC <> 0.
  172. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  173. * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  174. endif.
  175. ****************************************************************
  176.  
  177. *******************************************************************
  178. ** XSLT的方式解析、生成XML
  179. * data LT_TABLE type standard table of CHAR2048.
  180. * data: GT_RESULT_XML type ABAP_TRANS_RESBIND_TAB,* GS_RESULT_XML type ABAP_TRANS_RESBIND.
  181. *data lo_oref TYPE REF TO cx_root.
  182. * try.
  183. * call transformation ID "此ID是transformation,也可以用事务代码strans自定义一个transformation
  184. * source xml LT_TABLE
  185. * result (GT_RESULT_XML) .
  186. *
  187. * catch CX_ST_ERROR into LO_OREF.
  188. ** LS_MSG-MSGTYP = 'E'.
  189. ** LS_MSG-MSGNO = '000'.
  190. ** LS_MSG-MSGTXT = LO_OREF->GET_TEXT( ).
  191. ** append LS_MSG to C_MSGOUT.
  192. * endtry.

猜你在找的XML相关文章