Quantcast
Channel: SCN : Blog List - ABAP Development
Viewing all articles
Browse latest Browse all 943

Code snippet to get the tag values from XML instead of using Transformation

$
0
0

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') .       Capture1.JPG                                      

 

 

* 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>

***************************************  

Capture2.JPG

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>

**************************************

Capture3.JPG

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>

********************************************

Capture4.JPG

 

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.


Viewing all articles
Browse latest Browse all 943

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>