‎2009 May 19 11:05 AM
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
‎2009 May 19 5:49 PM
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
‎2009 May 20 7:55 AM
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
‎2009 May 20 10:04 AM
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
‎2009 May 20 2:49 PM
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
‎2009 May 21 6:34 AM
thanks for your suggestion. actually my structure is not that simple. its header and line items..anyways this approach is also good..
regards
rajeev