Skip to Content

Simple Transformation XML to ABAP Content of tag with subtrees to string field

Hi,

I have an requirement in which I have to do a transformation from an XML file to an structure.

I need get the value of a tag that have subtrees and move that to a field in my structure.

Example:

<TAG1>value1</TAG1>

<TAG2>value2</TAG2>

<TAG3>

<TAG4>value4</TAG4>

<TAG5>value5</TAG5>

</TAG3>

Result expected in ABAP Structure:

field1 -> tag1

field2 -> tag2

field3 -> <TAG4>value4</TAG4><TAG5>value5</TAG5>

The contents of the tag TAG3 is variable, so I want to store it as a string.

Can I make this with Simple Transformation?

In my tests I can move only the value of each child tag for the given field structure.

This syntax dont work:

<TAG3 tt:value-ref="STRUCTURE.FIELD3"/>

Thanks and Regards,

Miguel Motta

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Aug 22, 2014 at 11:02 AM

    Hi Miguel

    Have a look at below snippets. Here I have tried to escape the text inside TAG3 so that it gets treated as single node during transformation.

    ABAP code

    1. DATA: BEGIN OF result,
    2. col1 TYPE string,
    3. col2 TYPE string,
    4. col3 TYPE string,
    5. END OF result.
    6. DATA: xml_string TYPE string VALUE
    7. '<ROOT> <TAG1>value1</TAG1> <TAG2>value2</TAG2> <TAG3> <TAG4>value4</TAG4> <TAG5>value5</TAG5> </TAG3> </ROOT>',
    8. part1 TYPE string,
    9. part2 TYPE string,
    10. part3 TYPE string.
    11. * Escape the text inside TAG3 tag
    12. FIND REGEX '(.*<TAG3>)(.*)(</TAG3>.*)' IN xml_string SUBMATCHES part1 part2 part3.
    13. IF sy-subrc EQ 0.
    14. part2 = escape( val = part2 format = cl_abap_format=>e_xml_text ).
    15. * REPLACE ALL OCCURRENCES OF '<' IN part2 WITH '&lt;'.
    16. * REPLACE ALL OCCURRENCES OF '>' IN part2 WITH '&gt;'.
    17. xml_string = part1 && part2 && part3.
    18. ENDIF.
    19. TRY.
    20. * Display xml
    21. cl_abap_browser=>show_xml( EXPORTING xml_string = xml_string ).
    22. * Deserialization
    23. CALL TRANSFORMATION zmtest
    24. SOURCE XML xml_string
    25. RESULT para = result.
    26. * Check result
    27. WRITE:/ 'COL1=', result-col1,
    28. / 'COL2=', result-col2,
    29. / 'COL3=', result-col3.
    30. CATCH cx_st_error.
    31. * Error handling
    32. MESSAGE 'Error in Simple Transformation'
    33. TYPE 'I' DISPLAY LIKE 'E'.
    34. ENDTRY.

    Transformation code

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

    <tt:root name="PARA"/>

    <tt:template name="temp">
    <ROOT>
    <TAG1>
    <tt:value ref="PARA.COL1"/>
    </TAG1>
    <TAG2>
    <tt:value ref="PARA.COL2"/>
    </TAG2>
    <TAG3>
    <tt:value ref="PARA.COL3"/>
    </TAG3>
    </ROOT>
    </tt:template>

    </tt:transform>

    /.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 20, 2014 at 08:53 PM

    Hello Miguel,

    does the following work?

    <TAG3>

    <tt:value ref="$structure.field3"/>

    </TAG3>

    Interpreting your syntax example, the result would probably be

    <TAG3 <TAG4>value4</TAG4><TAG5>value5</TAG5>/>

    which is not valid XML.

    Best regards,

    Frakn.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.