二、将XML文件解析到SAP中
三、将XML数据生成字符串
四、用XSLT的方式解析、生成XML,自定义XSLT的事务代码是strans
五、下面是具体实现代码
type-pools:IXML,ABAP. types:beginofXML_LINE,DATA(256)typeX,endofXML_LINE. data:L_IXMLtypereftoIF_IXML,L_STREAMFACTORYtypereftoIF_IXML_STREAM_FACTORY,L_OSTREAMtypereftoIF_IXML_OSTREAM,L_RENDERERtypereftoIF_IXML_RENDERER,L_DOCUMENTtypereftoIF_IXML_DOCUMENT. data:L_ELEMENT_FLIGHTStypereftoIF_IXML_ELEMENT,L_ELEMENT_AIRLINEtypereftoIF_IXML_ELEMENT,L_ELEMENT_FLIGHTtypereftoIF_IXML_ELEMENT,L_ELEMENT_DUMMYtypereftoIF_IXML_ELEMENT,L_VALUEtypeSTRING. data:L_XML_TABLEtypetableofXML_LINE,L_XML_SIZEtypeI,L_RCtypeI. data:LT_SPFLItypetableofSPFLI. data:L_SPFLItypeSPFLI. start-of-selection. *Filltheinternaltable select*fromSPFLIintotableLT_SPFLI. *Sortinternaltable sortLT_SPFLIbyCARRID. *生成XML数据 loopatLT_SPFLIintoL_SPFLI. atfirst. *Creatingaixmlfactory L_IXML=CL_IXML=>CREATE(). *Creatingthedomobjectmodel L_DOCUMENT=L_IXML->CREATE_DOCUMENT(). *Fillrootnodewithvalueflow 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. atnewCONNID. 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. *Creatingastreamfactory L_STREAMFACTORY=L_IXML->CREATE_STREAM_FACTORY(). *ConnectinternalXMLtabletostreamfactory L_OSTREAM=L_STREAMFACTORY->CREATE_OSTREAM_ITABLE(TABLE= L_XML_TABLE). *Renderingthedocument 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数据导出到本地 *callmethodCL_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. *ifSY-SUBRC<>0. *messageidSY-MSGIDtypeSY-MSGTYnumberSY-MSGNO *withSY-MSGV1SY-MSGV2SY-MSGV3SY-MSGV4. *endif. ************************************************************ **************************************************** **--将XML数据导入到内表 *dataxMLDATAtypeXSTRING. *data:RESULT_XMLtypestandardtableofSMUM_XMLTB. *data:RETURNtypestandardtableofBAPIRET2. *data:WA_XMLtypeSMUM_XMLTB. * *如果需要上载XML可以用一下方法 *callfunction'GUI_UPLOAD' *exporting *FILENAME='E:\flights.xml' *FILETYPE='BIN' *importing *FILELENGTH=L_XML_SIZE *tables *DATA_TAB=L_XML_TABLE. **--将XML数据导入到内表 *callfunction'SCMS_BINARY_TO_XSTRING' *exporting *INPUT_LENGTH=L_XML_SIZE *importing *BUFFER=XMLDATA *tables *BINARY_TAB=L_XML_TABLE *exceptions *Failed=1 *others=2. * *callfunction'SMUM_XML_PARSE' *exporting *XML_INPUT=XMLDATA *tables *XML_TABLE=RESULT_XML *RETURN=RETURN. * *loopatRESULT_XMLintoWA_XML. *endloop. ************************************************ ************************************************** *将XML转换成字符串 data:W_STRINGtypeXSTRING. dataLS_XMLtypeSTRING. *将XML转换成字符串 callfunction'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. ifSY-SUBRC<>0. *MESSAGEIDSY-MSGIDTYPESY-MSGTYNUMBERSY-MSGNO *WITHSY-MSGV1SY-MSGV2SY-MSGV3SY-MSGV4. endif. *将Xstring转换成二进制 *callfunction'SCMS_XSTRING_TO_BINARY' *exporting *BUFFER=W_STRING *importing *OUTPUT_LENGTH=L_XML_SIZE *tables *BINARY_TAB=L_XML_TABLE. callfunction'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. ifSY-SUBRC<>0. *MESSAGEIDSY-MSGIDTYPESY-MSGTYNUMBERSY-MSGNO *WITHSY-MSGV1SY-MSGV2SY-MSGV3SY-MSGV4. endif. **************************************************************** ******************************************************************* **用XSLT的方式解析、生成XML *dataLT_TABLEtypestandardtableofCHAR2048. *data:GT_RESULT_XMLtypeABAP_TRANS_RESBIND_TAB,*GS_RESULT_XMLtypeABAP_TRANS_RESBIND. *datalo_orefTYPEREFTOcx_root. *try. *calltransformationID"此ID是transformation,也可以用事务代码strans自定义一个transformation *sourcexmlLT_TABLE *result(GT_RESULT_XML). * *catchCX_ST_ERRORintoLO_OREF. **LS_MSG-MSGTYP='E'. **LS_MSG-MSGNO='000'. **LS_MSG-MSGTXT=LO_OREF->GET_TEXT(). **appendLS_MSGtoC_MSGOUT. *endtry. |