1.通过Method
REPORT ZTEST_XML. TYPE-POOLS: ixml. TYPES:BEGIN OF xml_line, line(256) TYPE c, END OF xml_line. DATA: g_ixml TYPE REF TO if_Ixml, g_stream_factory TYPE REF TO if_ixml_stream_factory, g_document TYPE REF TO if_ixml_document, g_ostream TYPE REF TO if_ixml_ostream. DATA: g_file TYPE string VALUE 'f:/flight.xml'. DATA: STRING TYPE STRING. g_ixml = cl_ixml=>create( ). g_stream_factory = g_ixml->create_stream_factory( ). g_document = g_ixml->create_document( ). DATA: SAPITEMS TYPE REF TO IF_IXML_ELEMENT, ITEM TYPE REF TO IF_IXML_ELEMENT, ITEM2 TYPE REF TO IF_IXML_ELEMENT, ELEMENT TYPE REF TO IF_IXML_ELEMENT. DATA: ITAB TYPE SPFLI OCCURS 0 WITH HEADER LINE. DATA: IT_XML TYPE TABLE OF XML_LINE , XML_SIZE TYPE I. START-OF-SELECTION. SELECT * FROM SPFLI INTO TABLE ITAB. SAPITEMS = G_DOCUMENT->CREATE_SIMPLE_ELEMENT( NAME = 'SAPITEM' PARENT = G_DOCUMENT ). LOOP AT ITAB. ITEM = G_DOCUMENT->CREATE_SIMPLE_ELEMENT( NAME = 'ITEM' PARENT = SAPITEMS ). STRING = ITAB-CARRID. ELEMENT = G_DOCUMENT->CREATE_SIMPLE_ELEMENT( NAME = 'CARRID' VALUE = STRING PARENT = ITEM ). STRING = ITAB-CONNID. ELEMENT = G_DOCUMENT->CREATE_SIMPLE_ELEMENT( NAME = 'CONNID' VALUE = STRING PARENT = ITEM ). ENDLOOP. G_OSTREAM = G_STREAM_FACTORY->CREATE_OSTREAM_ITABLE( IT_XML ). G_DOCUMENT->RENDER( OSTREAM = G_OSTREAM RECURSIVE = 'X' ). XML_SIZE = G_OSTREAM->GET_NUM_WRITTEN_RAW( ). CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD EXPORTING BIN_FILESIZE = XML_SIZE FILENAME = G_FILE FILETYPE = 'BIN' CHANGING DATA_TAB = IT_XML EXCEPTIONS FILE_WRITE_ERROR = 1 NO_BATCH = 2 others = 3 .
执行结果:
<?xml version="1.0"?> -<SAPITEM> -<ITEM> <CARRID>AA</CARRID> <CONNID>0017</CONNID> </ITEM> -<ITEM> <CARRID>AA</CARRID> <CONNID>0064</CONNID> </ITEM> -<ITEM> <CARRID>DL</CARRID> <CONNID>1699</CONNID> </ITEM> -<ITEM> <CARRID>DL</CARRID> <CONNID>1984</CONNID> </ITEM> -<ITEM> <CARRID>LH</CARRID> <CONNID>0454</CONNID> </ITEM> -<ITEM> <CARRID>LH</CARRID> <CONNID>0455</CONNID> </ITEM> -<ITEM> <CARRID>LH</CARRID> <CONNID>2402</CONNID> </ITEM> -<ITEM> <CARRID>LH</CARRID> <CONNID>2407</CONNID> </ITEM> -<ITEM> <CARRID>LH</CARRID> <CONNID>2415</CONNID> </ITEM> -<ITEM> </SAPITEM>
2.使用T-Code STRANS 创建Transformation
<?sap.transform simple?> <tt:transform xmlns:tt="http://www.sap.com/transformation-templates"> <tt:root name="PRD"/> <tt:root name="CLASS"/> <tt:template> <ROOT> <tt:namespace name="tt"/> <Products> <tt:text> wertyui </tt:text> <tt:loop name="line" ref="PRD"> <Product> <matnr> <tt:value ref="matnr"/> </matnr> <maktx> <tt:value ref="maktx"/> </maktx> </Product> </tt:loop> </Products> <Categories> <tt:loop name="line2" ref="CLASS"> <Categorie> <CLASS> <tt:value ref="$line2.class"/> </CLASS> <KSCHL> <tt:value ref="$line2.kschl"/> </KSCHL> </Categorie> </tt:loop> </Categories> </ROOT> </tt:template> </tt:transform>
调用这个Transformation:
REPORT ZTEST_XML. DATA: BEGIN OF it_xml OCCURS 0, line(100) TYPE x, END OF it_xml. DATA: w_xml TYPE string, w_len TYPE i, w_fname type string. DATA: BEGIN OF line, matnr LIKE mara-matnr, maktx like makt-maktx, END OF line. DATA: BEGIN OF line2, class like m_wwgha-class, kschl LIKE m_wwgha-kschl, END OF line2. DATA: itab_matnr LIKE TABLE OF line, itab_class LIKE TABLE OF line2. START-OF-SELECTION. PERFORM getdata. PERFORM create_xml. *&---------------------------------------------------------------------* *& Form GETDATA *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM GETDATA . SELECT matnr maktx FROM makt INTO TABLE itab_matnr UP TO 4 ROWS WHERE spras = '1'. SELECT Class kschl FROM m_wwgha INTO TABLE itab_class UP TO 4 ROWS WHERE spras in ('1','E'). ENDFORM. " GETDATA *&---------------------------------------------------------------------* *& Form CREATE_XML *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM CREATE_XML . CALL TRANSFORMATION ztest SOURCE prd = itab_matnr class = itab_class RESULT XML w_xml. CALL FUNCTION 'SCMS_STRING_TO_FTEXT' EXPORTING TEXT = w_xml IMPORTING LENGTH = w_len TABLES FTEXT_TAB = it_xml. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING * BIN_FILESIZE = FILENAME = 'f:prd1.xml' FILETYPE = 'BIN' * APPEND = ' ' * WRITE_FIELD_SEPARATOR = ' ' * HEADER = '00' * TRUNC_TRAILING_BLANKS = ' ' * WRITE_LF = 'X' * COL_SELECT = ' ' * COL_SELECT_MASK = ' ' * DAT_MODE = ' ' * CONFIRM_OVERWRITE = ' ' * NO_AUTH_CHECK = ' ' CODEPAGE = ' ' * IGNORE_CERR = ABAP_TRUE * REPLACEMENT = '#' * WRITE_BOM = ' ' * TRUNC_TRAILING_BLANKS_EOL = 'X' * WK1_N_FORMAT = ' ' * WK1_N_SIZE = ' ' * WK1_T_FORMAT = ' ' * WK1_T_SIZE = ' ' * WRITE_LF_AFTER_LAST_LINE = ABAP_TRUE * SHOW_TRANSFER_STATUS = ABAP_TRUE IMPORTING FILELENGTH = w_len TABLES DATA_TAB = it_xml * FIELDNAMES = * EXCEPTIONS * FILE_WRITE_ERROR = 1 * NO_BATCH = 2 * GUI_REFUSE_FILETRANSFER = 3 * INVALID_TYPE = 4 * NO_AUTHORITY = 5 * UNKNOWN_ERROR = 6 * HEADER_NOT_ALLOWED = 7 * SEPARATOR_NOT_ALLOWED = 8 * FILESIZE_NOT_ALLOWED = 9 * HEADER_TOO_LONG = 10 * DP_ERROR_CREATE = 11 * DP_ERROR_SEND = 12 * DP_ERROR_WRITE = 13 * UNKNOWN_DP_ERROR = 14 * ACCESS_DENIED = 15 * DP_OUT_OF_MEMORY = 16 * DISK_FULL = 17 * DP_TIMEOUT = 18 * FILE_NOT_FOUND = 19 * DATAPROVIDER_EXCEPTION = 20 * CONTROL_FLUSH_ERROR = 21 * OTHERS = 22 . IF SY-SUBRC <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDFORM. " CREATE_XML
执行结果:
<?xml version="1.0" encoding="BIG5"?> -<ROOT xmlns:tt="http://www.sap.com/transformation-templates"> -<Products> wertyui -<Product> <matnr>B</matnr> <maktx/> </Product> -<Product> <matnr>COMPAL#M1</matnr> <maktx>COMMENT 1</maktx> </Product> -<Product> <matnr>000000000000000452</matnr> <maktx>TPG萇齟</maktx> </Product> </Products> -<Categories> -<Categorie> <CLASS>1</CLASS> <KSCHL>level 1</KSCHL> </Categorie> -<Categorie> <CLASS>ZZZZZZZZZ</CLASS> <KSCHL>zzzzzzzzz</KSCHL> </Categorie> </Categories> </ROOT>