代码如下
代码语言:javascript复制REPORT Y_XB_TEST10.
*&---------------------------------------------------------------------*
*&声明及定义部分
*&---------------------------------------------------------------------*
TYPE-POOLS: IXML.
****声明存放XML文件的结构
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.
****声明XML元素
DATA:L_ELEMENT_INPOT TYPE REF TO IF_IXML_ELEMENT,
L_ELEMENT_CARRID TYPE REF TO IF_IXML_ELEMENT,
L_ELEMENT_CONNID TYPE REF TO IF_IXML_ELEMENT,
L_ELEMENT_CITYFROM TYPE REF TO IF_IXML_ELEMENT,
L_ELEMENT_AIRPFROM TYPE REF TO IF_IXML_ELEMENT,
L_ELEMENT_CITYTO TYPE REF TO IF_IXML_ELEMENT,
L_ELEMENT_AIRPTO TYPE REF TO IF_IXML_ELEMENT,
L_VALUE TYPE STRING.
****声明存放XML文件的内表以及字节数
DATA: L_XML_TABLE TYPE TABLE OF XML_LINE,
L_XML_SIZE TYPE I,
L_RC TYPE I,
FLAG TYPE STRING.
****声明数据内表
DATA:BEGIN OF I_DATA OCCURS 0,
CARRID TYPE SPFLI-CARRID, "航线代码
CONNID TYPE SPFLI-CONNID, "航班连接编号
CITYFROM TYPE SPFLI-CITYFROM, "起飞城市
AIRPFROM TYPE SPFLI-AIRPFROM, "始发机场
CITYTO TYPE SPFLI-CITYTO, "到达城市
AIRPTO TYPE SPFLI-AIRPTO, "目标机场
END OF I_DATA.
*&---------------------------------------------------------------------*
*& 主逻辑部分
*&---------------------------------------------------------------------*
START-OF-SELECTION.
PERFORM ZFORM_GET_DATA.
PERFORM ZFORM_SAVE_XML.
*&---------------------------------------------------------------------*
*& FORM定义部分
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form zform_get_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM ZFORM_GET_DATA.
SELECT CARRID
CONNID
CITYFROM
AIRPFROM
CITYTO
AIRPTO
INTO TABLE I_DATA
FROM SPFLI
UP TO 10 ROWS.
ENDFORM. "zform_get_data
*&---------------------------------------------------------------------*
*& Form save_xml
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM ZFORM_SAVE_XML.
LOOP AT I_DATA.
****创建一个XML容器
AT FIRST.
L_IXML = CL_IXML=>CREATE( ).
****创建一个文档对象模型
L_DOCUMENT = L_IXML->CREATE_DOCUMENT( ).
****填充根节点
L_ELEMENT_INPOT = L_DOCUMENT->CREATE_SIMPLE_ELEMENT(
NAME = 'INPOT'
PARENT = L_DOCUMENT ).
ENDAT.
AT NEW CARRID.
****填充子节点(航线代码)
L_VALUE = I_DATA-CARRID.
L_ELEMENT_CARRID = L_DOCUMENT->CREATE_SIMPLE_ELEMENT(
NAME = 'CARRID'
VALUE = L_VALUE
PARENT = L_ELEMENT_INPOT ).
FLAG = FLAG 1.
L_RC = L_ELEMENT_CARRID->SET_ATTRIBUTE( NAME = 'ID' VALUE =
FLAG ).
ENDAT.
*****填充子节点(航班连接编号)
AT NEW CONNID.
L_VALUE = I_DATA-CONNID.
L_ELEMENT_CONNID = L_DOCUMENT->CREATE_SIMPLE_ELEMENT(
NAME = 'PRUEFLOS'
VALUE = L_VALUE
PARENT = L_ELEMENT_CARRID ).
ENDAT.
*****填充子节点(细节)
L_VALUE = I_DATA-CITYFROM. "起飞城市
L_ELEMENT_CITYFROM = L_DOCUMENT->CREATE_SIMPLE_ELEMENT(
NAME = 'CITYFROM'
VALUE = L_VALUE
PARENT = L_ELEMENT_CONNID ).
L_VALUE = I_DATA-AIRPFROM. "始发机场
L_ELEMENT_AIRPFROM = L_DOCUMENT->CREATE_SIMPLE_ELEMENT(
NAME = 'AIRPFROM'
VALUE = L_VALUE
PARENT = L_ELEMENT_CONNID ).
L_VALUE = I_DATA-CITYTO. "到达城市
L_ELEMENT_CITYTO = L_DOCUMENT->CREATE_SIMPLE_ELEMENT(
NAME = 'CITYTO'
VALUE = L_VALUE
PARENT = L_ELEMENT_CONNID ).
L_VALUE = I_DATA-AIRPTO. "始发机场
L_ELEMENT_AIRPTO = L_DOCUMENT->CREATE_SIMPLE_ELEMENT(
NAME = '目标机场'
VALUE = L_VALUE
PARENT = L_ELEMENT_CONNID ).
ENDLOOP.
****创建一个流容器
L_STREAMFACTORY = L_IXML->CREATE_STREAM_FACTORY( ).
****连接内部XML表到流容器
L_OSTREAM = L_STREAMFACTORY->CREATE_OSTREAM_ITABLE(
TABLE = L_XML_TABLE ).
****递交XML文档
L_RENDERER = L_IXML->CREATE_RENDERER( OSTREAM = L_OSTREAM
DOCUMENT = L_DOCUMENT ).
L_RC = L_RENDERER->RENDER( ).
****保存XML文档
L_XML_SIZE = L_OSTREAM->GET_NUM_WRITTEN_RAW( ).
* CALL METHOD cl_gui_frontend_services=>gui_download
* EXPORTING
* bin_filesize = l_xml_size
* filename = 'c:tempINPOT.xml'
* filetype = 'BIN'
* CHANGING
* data_tab = l_xml_table
* EXCEPTIONS
* OTHERS = 24.
* IF sy-subrc = 0.
* MESSAGE 'XML文件保存成功!!!!' TYPE 'I'.
* STOP.
* ENDIF.
*将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.
* LS_XML为转换出来的XML
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.
ENDFORM. "save_xml