Hi,

Below is the code snippet that can be referred to parse XML document into an internal table of the ABAP program. We would need to pass XML document as a string type to the function module.

FUNCTION z_parse_xml.

*”———————————————————————-

*”*”Local Interface:

*”  IMPORTING

*”     REFERENCE(XML_DATA) TYPE  STRING

*”  TABLES

*”      ITAB

*”  EXCEPTIONS

*”      EXCEPTION

*”———————————————————————-

DATA: l_xml_doc TYPE REF TO cl_xml_document,

lv_subrc TYPE sysubrc,

l_node TYPE REF TO if_ixml_node,

l_iterator TYPE REF TO if_ixml_node_iterator,

lv_nodetype TYPE i,

lv_name TYPE string,

lv_value TYPE string,

lv_char TYPE char2.

FIELDSYMBOLS: TYPE ANY.

***Create XML document using XML data

CREATE OBJECT l_xml_doc.

lv_subrc = l_xml_doc->parse_string( xml_data ).

IF lv_subrc IS NOT INITIAL.

MESSAGE e001(00) WITH ‘Error in XML document’ RAISING exception.

EXIT.

ENDIF.

***Create node to get data from XML document

l_node = l_xml_doc->m_document.

IF l_node IS INITIAL.

MESSAGE e001(00) WITH ‘Error in XML document’ RAISING exception.

EXIT.

ENDIF.

***Create iterator to loop through nodes

l_iterator = l_node->create_iterator( ).

l_node = l_iterator->get_next( ).

***Loop through all the nodes in the XML document

WHILE NOT l_node IS INITIAL.

lv_nodetype = l_node->get_type( ).

CASE lv_nodetype.

***XML tag name

WHEN if_ixml_node=>co_node_element.

***Get tag name

lv_name = l_node->get_name( ).

***Get value of the XML tag

WHEN if_ixml_node=>co_node_text OR if_ixml_node=>co_node_cdata_section.

lv_value = l_node->get_value( ).

MOVE lv_value TO lv_char.

IF lv_char NE cl_abap_char_utilities=>cr_lf.

ASSIGN lv_value TO .

ENDIF.

WHEN OTHERS.

“Do nothing

ENDCASE.

l_node = l_iterator->get_next( ).

ENDWHILE.

***Append the row to the internal table

APPEND TO itab.

ENDFUNCTION.

 

The function module will thus give us the parsed data which can be further used for processing.

New NetWeaver Information at SAP.com

Very Helpfull

 

 

User Rating: Be the first one !