*&---------------------------------------------------------------------*
*& Report ZLM_XML_UPLOAD
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zlm_xml_upload.
PARAMETERS: p_filnam TYPE localfile OBLIGATORY
DEFAULT 'C:\Users\mliu43\Desktop\1.xml'.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_filnam.
DATA: l_v_fieldname TYPE dynfnam.
l_v_fieldname = p_filnam.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = l_v_fieldname
IMPORTING
file_name = p_filnam.
START-OF-SELECTION.
* TYPES:
*
* BEGIN OF ty_tab,
*
* name TYPE string,
*
* value TYPE string,
*
* END OF ty_tab.
DATA:
lcl_xml_doc TYPE REF TO cl_xml_document,
v_subrc TYPE sysubrc,
v_node TYPE REF TO if_ixml_node,
v_child_node TYPE REF TO if_ixml_node,
v_root TYPE REF TO if_ixml_node,
v_iterator TYPE REF TO if_ixml_node_iterator,
v_nodemap TYPE REF TO if_ixml_named_node_map,
v_count TYPE i,
v_index TYPE i,
v_attr TYPE REF TO if_ixml_node,
v_name TYPE string,
v_prefix TYPE string,
v_value TYPE string,
v_char TYPE char2.
DATA:
itab TYPE STANDARD TABLE OF zlm_xml_node,
* itab TYPE STANDARD TABLE OF ty_tab,
wa TYPE zlm_xml_node.
* wa TYPE ty_tab.
CREATE OBJECT lcl_xml_doc.
CALL METHOD lcl_xml_doc->import_from_file
EXPORTING
filename = p_filnam
RECEIVING
retcode = v_subrc.
CHECK v_subrc = 0.
v_node = lcl_xml_doc->m_document.
CHECK NOT v_node IS INITIAL.
v_iterator = v_node->create_iterator( ).
v_node = v_iterator->get_next( ).
WHILE NOT v_node IS INITIAL.
CASE v_node->get_type( ).
WHEN if_ixml_node=>co_node_element.
v_name = v_node->get_name( ).
v_nodemap = v_node->get_attributes( ).
IF NOT v_nodemap IS INITIAL.
* attributes
v_count = v_nodemap->get_length( ).
DO v_count TIMES.
v_index = sy-index - 1.
v_attr = v_nodemap->get_item( v_index ).
v_name = v_attr->get_name( ).
v_prefix = v_attr->get_namespace_prefix( ).
v_value = v_attr->get_value( ).
ENDDO.
ENDIF.
WHEN if_ixml_node=>co_node_text OR
if_ixml_node=>co_node_cdata_section.
* text node
v_value = v_node->get_value( ).
MOVE v_value TO v_char.
IF v_char <> cl_abap_char_utilities=>cr_lf.
wa-name = v_name.
wa-value = v_value.
APPEND wa TO itab.
CLEAR wa.
ENDIF.
ENDCASE.
* advance to next node
v_node = v_iterator->get_next( ).
ENDWHILE.
FIELD-SYMBOLS:
TYPE ANY.
*
DATA : lt_idetails TYPE abap_compdescr_tab WITH HEADER LINE.
DATA : lr_ref_table_des TYPE REF TO cl_abap_structdescr.
DATA : ct_idetails TYPE abap_compdescr_tab WITH HEADER LINE.
DATA : cr_ref_table_des TYPE REF TO cl_abap_structdescr.
lr_ref_table_des ?=
cl_abap_typedescr=>describe_by_name( 'SPFLI' ).
CLEAR lt_idetails[].
lt_idetails[] = lr_ref_table_des->components[].
"循环判断结构每个字段
DATA:ls_data TYPE spfli.
DATA:lt_data TYPE TABLE OF spfli.
LOOP AT itab INTO wa.
READ TABLE lt_idetails WITH KEY name = wa-name.
CHECK sy-subrc = 0.
ASSIGN COMPONENT lt_idetails-name OF STRUCTURE ls_data TO .
IF lt_idetails-name = 'CARRID'.
APPEND ls_data TO lt_data.
ENDIF.
= wa-value.
ENDLOOP.
"添加最后一行
APPEND ls_data TO lt_data.
"删除空行
DELETE lt_data WHERE carrid IS INITIAL.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_structure_name = 'SPFLI'
TABLES
t_outtab = lt_data
EXCEPTIONS
program_error = 1
OTHERS = 2.
标签:XML,node,内表,get,DEMO,value,lt,TYPE,name From: https://blog.51cto.com/u_15680210/5751352