REPORT zlm_xml_02.
*&---------------------------------------------------------------------*
*&声明及定义部分
*&---------------------------------------------------------------------*
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_matnr TYPE REF TO if_ixml_element,
l_element_art TYPE REF TO if_ixml_element,
l_element_prueflos TYPE REF TO if_ixml_element,
l_element_budat TYPE REF TO if_ixml_element,
l_element_losmenge TYPE REF TO if_ixml_element,
l_element_enstehdat 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,
matnr LIKE qals-matnr,
prueflos LIKE qals-prueflos,
art LIKE qals-art,
budat LIKE qals-budat,
losmenge LIKE qals-losmenge,
enstehdat LIKE qals-enstehdat,
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 matnr art prueflos budat losmenge enstehdat
INTO CORRESPONDING FIELDS OF TABLE i_data
FROM qals
WHERE budat = '20000412'.
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 matnr.
****填充子节点(物料)
l_value = i_data-matnr.
l_element_matnr = l_document->create_simple_element(
name = 'MATNR'
value = l_value
parent = l_element_inpot ).
flag = flag + 1.
l_rc = l_element_matnr->set_attribute( name = 'ID' value =
flag ).
ENDAT.
****填充子节点(检验批)
AT NEW prueflos.
l_value = i_data-prueflos.
l_element_prueflos = l_document->create_simple_element(
name = 'PRUEFLOS'
value = l_value
parent = l_element_matnr ).
ENDAT.
****填充子节点(检验批细节)
l_value = i_data-art.
l_element_art = l_document->create_simple_element(
name = 'ART'
value = l_value
parent = l_element_prueflos ).
l_value = i_data-budat.
l_element_budat = l_document->create_simple_element(
name = 'BUDAT'
value = l_value
parent = l_element_prueflos ).
l_value = i_data-enstehdat.
l_element_enstehdat = l_document->create_simple_element(
name = 'ENSTEHDAT'
value = l_value
parent = l_element_prueflos ).
l_value = i_data-losmenge.
l_element_losmenge = l_document->create_simple_element(
name = 'LOSMENGE'
value = l_value
parent = l_element_prueflos ).
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:\temp\INPOT.xml'
filetype = 'BIN'
CHANGING
data_tab = l_xml_table
EXCEPTIONS
OTHERS = 24.
IF sy-subrc = 0.
MESSAGE 'XML文件保存成功!!!!' TYPE 'I'.
STOP.
ENDIF.
ENDFORM. "save_xml