一、将ABAP数据生成XML文件且生成到本地
二、将XML文件解析到SAP中
三、将XML数据生成字符串
四、用XSLT的方式解析、生成XML,自定义XSLT的事务代码是strans
五、下面是具体实现代码
type-pools: IXML,ABAP. types: begin of XML_LINE, DATA(256) type X, end of XML_LINE. 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. 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. data: L_XML_TABLE type table of XML_LINE, L_XML_SIZE type I, L_RC type I. data: LT_SPFLI type table of SPFLI. data: L_SPFLI type SPFLI. start-of-selection. * Fill the internal table select * from SPFLI into table LT_SPFLI. * Sort internal table sort LT_SPFLI by CARRID. * 生成XML数据 loop at LT_SPFLI into L_SPFLI. at first. * Creating a ixml factory L_IXML = CL_IXML=>CREATE( ). * Creating the dom object model L_DOCUMENT = L_IXML->CREATE_DOCUMENT( ). * Fill root node with value flow L_ELEMENT_FLIGHTS = L_DOCUMENT->CREATE_SIMPLE_ELEMENT( NAME = 'flow' PARENT = L_DOCUMENT ). L_RC = L_ELEMENT_FLIGHTS->SET_ATTRIBUTE( NAME = 'KEY' VALUE = 'gsgs-cgdd' ). L_RC = L_ELEMENT_FLIGHTS->SET_ATTRIBUTE( NAME = 'DES' VALUE = '广深公司-采购订单'). L_RC = L_ELEMENT_FLIGHTS->SET_ATTRIBUTE( NAME = 'BAPI' VALUE ='ZBAPI_MM_RK_AFTER_APP' ). L_ELEMENT_AIRLINE = L_DOCUMENT->CREATE_SIMPLE_ELEMENT( NAME = 'customform' PARENT = L_ELEMENT_FLIGHTS ). endat. at new CONNID. L_ELEMENT_FLIGHT = L_DOCUMENT->CREATE_SIMPLE_ELEMENT( NAME = 'fd' PARENT = L_ELEMENT_AIRLINE ). L_VALUE = L_SPFLI-CONNID. L_RC = L_ELEMENT_FLIGHT->SET_ATTRIBUTE( NAME = 'n' VALUE = 'flight' ). endat. L_VALUE = L_SPFLI-DEPTIME. L_ELEMENT_DUMMY = L_DOCUMENT->CREATE_SIMPLE_ELEMENT( NAME = 'V' VALUE = L_VALUE PARENT = L_ELEMENT_FLIGHT ). endloop. * Creating a stream factory L_STREAMFACTORY = L_IXML->CREATE_STREAM_FACTORY( ). * Connect internal XML table to stream factory L_OSTREAM = L_STREAMFACTORY->CREATE_OSTREAM_ITABLE( TABLE = L_XML_TABLE ). * Rendering the document L_RENDERER = L_IXML->CREATE_RENDERER( OSTREAM = L_OSTREAM DOCUMENT = L_DOCUMENT ). L_RC = L_RENDERER->RENDER( ). ************************************************************* * 将xml数据导出到本地 * 取得XML数据大小 L_XML_SIZE = L_OSTREAM->GET_NUM_WRITTEN_RAW( ). * 将xml数据导出到本地 * call method CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD * exporting * BIN_FILESIZE = L_XML_SIZE * FILENAME = 'E:\flights.xml' * FILETYPE = 'BIN' * changing * DATA_TAB = L_XML_TABLE * exceptions * others = 24. * if SY-SUBRC <> 0. * message id SY-MSGID type SY-MSGTY number SY-MSGNO * with SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. * endif. ************************************************************ **************************************************** **--将XML数据导入到内表 * data XMLDATA type XSTRING . * data: RESULT_XML type standard table of SMUM_XMLTB . * data: RETURN type standard table of BAPIRET2 . * data: WA_XML type SMUM_XMLTB. * *如果需要上载XML可以用一下方法 * call function 'GUI_UPLOAD' * exporting * FILENAME = 'E:\flights.xml' * FILETYPE = 'BIN' * importing * FILELENGTH = L_XML_SIZE * tables * DATA_TAB = L_XML_TABLE. **--将XML数据导入到内表 * call function 'SCMS_BINARY_TO_XSTRING' * exporting * INPUT_LENGTH = L_XML_SIZE * importing * BUFFER = XMLDATA * tables * BINARY_TAB = L_XML_TABLE * exceptions * FAILED = 1 * others = 2. * * call function 'SMUM_XML_PARSE' * exporting * XML_INPUT = XMLDATA * tables * XML_TABLE = RESULT_XML * RETURN = RETURN. * * loop at RESULT_XML into WA_XML . * endloop. ************************************************ ************************************************** *将XML转换成字符串 data: W_STRING type XSTRING. data LS_XML type STRING. *将XML转换成字符串 call function 'SDIXML_DOM_TO_XML' exporting DOCUMENT = L_DOCUMENT importing XML_AS_STRING = W_STRING SIZE = L_XML_SIZE tables XML_AS_TABLE = L_XML_TABLE exceptions NO_DOCUMENT = 1 others = 2. if SY-SUBRC <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. endif. * 将Xstring转换成二进制 * call function 'SCMS_XSTRING_TO_BINARY' * exporting * BUFFER = W_STRING * importing * OUTPUT_LENGTH = L_XML_SIZE * tables * BINARY_TAB = L_XML_TABLE. call function 'SCMS_BINARY_TO_STRING' exporting INPUT_LENGTH = L_XML_SIZE importing TEXT_BUFFER = LS_XML tables BINARY_TAB = L_XML_TABLE exceptions FAILED = 1 others = 2. if SY-SUBRC <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. endif. **************************************************************** ******************************************************************* ** 用XSLT的方式解析、生成XML * data LT_TABLE type standard table of CHAR2048. * data: GT_RESULT_XML type ABAP_TRANS_RESBIND_TAB, * GS_RESULT_XML type ABAP_TRANS_RESBIND. *data lo_oref TYPE REF TO cx_root. * try. * call transformation ID "此ID是transformation,也可以用事务代码strans自定义一个transformation * source xml LT_TABLE * result (GT_RESULT_XML) . * * catch CX_ST_ERROR into LO_OREF. ** LS_MSG-MSGTYP = 'E'. ** LS_MSG-MSGNO = '000'. ** LS_MSG-MSGTXT = LO_OREF->GET_TEXT( ). ** append LS_MSG to C_MSGOUT. * endtry. |