Using Transformation , while retrieving values from XML can be sometimes difficult as most of the times exceptions are thrown from Transformation, which is difficult to understand.
Using Object Orientented approach, we can retrieve node and attribute values of XML in much efficient way.
Below is the code snippet for achieving the same.
TYPES: BEGIN OF ty_input,
pspid TYPE proj-pspid,
seq_no(3) TYPE c,
actiontype(6) TYPE c,
END OF ty_input.
DATA:
xml_doc TYPE REF TO cl_xml_document.
node TYPE REF TO if_ixml_node,
iterator TYPE REF TO if_ixml_node_iterator,
name TYPE string,
value TYPE string,
ps_input TYPE ty_input.
IF xml_in IS INITIAL.
* Load the sample XML file stored in presentation server
IF xml_in IS INITIAL.
EXIT.
ENDIF.
ENDIF.
* Create an Instance for the class cl_xml_document
CREATE OBJECT xml_doc.
xml_doc->parse_string( stream = xml_in ).
* Get the Parent Node as actionids.
node = xml_doc->find_node( name = 'projectInfo') .
* If the node is found
IF node IS NOT INITIAL.
*Create a node iterator
iterator = node->create_iterator( ).
WHILE NOT node IS INITIAL.
* Create an iterator for getting all the nodes under the root node
* Get the node name
name = node->get_name( ).
****************************************
Get the Tag Value
****************************************
CASE name.
***************************************
Get the Tag Value of <projectId>
***************************************
WHEN 'projectId'.
lv_tag_name = name.
lv_node = node.
value = xml_doc->get_node_attribute( node = lv_node
name = lv_tag_name)
ps_input-pspid = value.
**************************************
Get the Tag Value of <seqno>
**************************************
WHEN 'seqno'.
lv_tag_name = name.
lv_node = node.
value = xml_doc->get_node_attribute( node = lv_node
name = lv_tag_name)
ps_input-seq_no= value.
********************************************
Get the Tag Value of <actionType>
********************************************
WHEN 'actionType'.
lv_tag_name = name
lv_node = node.
value = xml_doc->get_node_attribute( node = lv_node
name = lv_tag_name)
ps_input-actiontype = value.
ENDCASE.
**********************************
Advance to next node
**********************************
node = iterator->get_next( ).
ENDWHILE.
ENDIF.