ABAP XML解析

前端之家收集整理的这篇文章主要介绍了ABAP XML解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

TYPES: t_xml_line(1024) TYPE x.

DATA: l_filename TYPE string,
l_xml_table TYPE TABLE OF t_xml_line,
l_xml_line TYPE t_xml_line,
l_xml_table_size TYPE i.

CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = l_filename
filetype = 'BIN'
IMPORTING
filelength = l_xml_table_size
CHANGING
data_tab = l_xml_table
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.


TYPE-POOLS: ixml.
DATA: l_ixml TYPE REF TO if_ixml.
l_ixml = cl_ixml=>create( ).

DATA: l_streamfactory TYPE REF TO if_ixml_stream_factory,
l_istream TYPE REF TO if_ixml_istream.

l_streamfactory = l_ixml->create_stream_factory( ).

l_istream = l_streamfactory->create_istream_itable(
table = l_xml_table
size = l_xml_table_size ).

DATA: l_document TYPE REF TO if_ixml_document,
l_parser TYPE REF TO if_ixml_parser.

l_document = l_ixml->create_document( ).
l_parser = l_ixml->create_parser(
stream_factory = l_streamfactory
istream = l_istream
document = l_document ).

IF l_parser->parse( ) NE 0.
IF l_parser->num_errors( ) NE 0.
...
l_parse_error = l_parser->get_error( index = l_index ).
...
ENDIF.
ENDIF.

At this point you have to check if the DOM was really created and if so you can process the document.

IF l_parser->is_dom_generating( ) EQ 'X'.
PERFORM process_dom USING l_document.
ENDIF.

DATA: node TYPE REF TO if_ixml_node,
iterator TYPE REF TO if_ixml_node_iterator.

node ?= document.

CHECK NOT node IS INITIAL.

iterator = node->create_iterator( ).
node = iterator->get_next( ).

WHILE NOT node IS INITIAL.
CASE node->get_type( ).
WHEN if_ixml_node=>co_node_element.
name = node->get_name( ).
...
WHEN if_ixml_node=>co_node_text OR
if_ixml_node=>co_node_cdata_section.
value = node->get_value( ).
...
ENDCASE.
node = iterator->get_next( ).
ENDWHILE.

nodemap = node->get_attributes( ). IF NOT nodemap IS INITIAL. count = nodemap->get_length( ). DO count TIMES. index = sy-index - 1. attr = nodemap->get_item( index ). name = attr->get_name( ). prefix = attr->get_namespace_prefix( ). value = attr->get_value( ). ... ENDDO. ENDIF.

原文链接:https://www.f2er.com/xml/295901.html

猜你在找的XML相关文章