Supported by

Save XML data in an ST (Simple Transformation)

Some days ago I was using an ST (Simple Transformation) and thought that, even though its job is to convert an input into an output, it is can be used to store XML data.

Let’s asy we have the following XML:

    <cocktail id="gt" nome="Gin Tonic"/>
    <cocktail id="ws" nome="Whiskey Sour"/>
    <cocktail id="cl" nome="Campari Laranja"/>

The way to store it in an ST is to wrap it like this:

<xsl:transform version="1.0"
  <xsl:strip-space elements="*"/>
  <xsl:template match="/">
      <cocktail id="gt" name="Gin Tónico"/>
      <cocktail id="ws" name="Whiskey Sour"/>
      <cocktail id="cl" name="Campari Laranja"/>

And the ABAP code to read it to a string is quite simple:

DATA: xml_str TYPE string.
xml_str = ||.
  SOURCE XML xml_str
  RESULT XML xml_str.

Note that the input string must be a valid XML even though it will be ignored.
And so, now we have our XML in a string.

To convert this XML into an internal table we just have to use another simple ST:

<?sap.transform simple?>
  <tt:root name="T_COCKTAIL" type="ddic:ZCOCKTAIL_T"/>
      <tt:loop ref=".T_COCKTAIL">
          <tt:attribute name="id" value-ref="ID"/>
          <tt:attribute name="name" value-ref="NAME"/>

And call it using the following code:

DATA: t_cocktail TYPE zcocktail_t.
  SOURCE XML xml_str
  RESULT constants = t_cocktail.

And that’s it. An unorthodox but practical and somewhat simple way to store static data in SAP without using the database.

Greetings from Abapinho.

Deixe um comentário

About Abapinho
Abapinho runs on WordPress
Articles (RSS) e Comments (RSS).