Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Converting XML string to ABAP types

Former Member
0 Likes
1,079

Hello,

I am having a requirement where i wanted to parse incoming XML string and i checked few of examples of CALL TRANSFORMATION id.

but whatever examples i checked those were simple transformation with no hierarchy.

My source structure

<record>
<firstname>XYZ</firstname>
<position>Testposition</postion>
</record>

and i used

CALL transformation id
source xml lv_xml
result firstname = lv_firstname position = lv_position.

but lv_firstname and lv_position were blank. any idea whether it can be acheived through such method?

thanks in advance.

regards

rajeev

5 REPLIES 5
Read only

naimesh_patel
Active Contributor
0 Likes
798

You need to have the ROOT node in your XML transformation.

Try this Transformation:


<?sap.transform simple?>
<tt:transform template="temp"
    xmlns:tt="http://www.sap.com/transformation-templates"
    version="0.1">

  <tt:root name="IF_TEST"/>

  <tt:template name="temp">
    <Test>
        <tt:value ref="IF_TEST" />
    </Test>
  </tt:template>

</tt:transform>

Test program to generate the XML string and than back to the variable.


DATA: lf_string TYPE string.
DATA: xml_string TYPE string.

lf_string = 'This single variable test'.

TRY.

    CALL TRANSFORMATION ztest_np_xml2data
      SOURCE if_test = lf_string
      RESULT XML xml_string.

    CLEAR lf_string.

    CALL TRANSFORMATION ztest_np_xml2data
      SOURCE XML xml_string
      RESULT if_test = lf_string.

  CATCH cx_st_error.

ENDTRY.

Regards,

Naimesh Patel

Read only

Ramneek
Product and Topic Expert
Product and Topic Expert
0 Likes
798

Hello Rajeev,

Try using the following simple transformation


<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">
  <tt:root name="record"/>
  <tt:template name="record">
    <record>
      <firstname>
        <tt:value ref="record.firstname"/>
      </firstname>
      <position>
        <tt:value ref="record.position"/>
      </position>
    </record>
  </tt:template>
</tt:transform>

In your code you could define a structure like the following:


TYPES: BEGIN OF record,
              firstname TYPE char50,
              position  TYPE char50,
            END OF record.

And now you could use the CALL TRANSFORMATION in your code to convert the xml string into the structure and visa-versa.


CALL TRANSFORMATION zcl_test_rec
      SOURCE XML lv_string
      RESULT record = ls_test.

Where lv_string holds the xml as a string and ls_test is defined as a structure of type record (defined above).

Hope this helps.

Thank you,

Ramneek

Read only

Former Member
0 Likes
798

Thanks for your help..so we will have to write XSLT transformations in such cases. actually i was looking at options whether to go for such transformations or to have DOM parser APIs that are available in ABAP.

I thought this built-in transformation would go with simple XML and hence tried.

regards

rajeev

Read only

Ramneek
Product and Topic Expert
Product and Topic Expert
0 Likes
798

I would suggest the using the DOM parser when you want to do more complex XML manipulation. If your requirement is just to read some XML and populate a data structure, then the simple transformations work just fine. All you have to do is define an xslt and the structure.

Hope this helps.

Thank you,

Ramneek

Read only

Former Member
0 Likes
798

thanks for your suggestion. actually my structure is not that simple. its header and line items..anyways this approach is also good..

regards

rajeev