Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

How to convert xml to ABAP data?

Hi Expert,

I need to write a transformation and convert an XML file into ABAP data.

The XML looks like following:

<?xml version="1.0" ?>

- <ServiceProtocol version="2.1">

- <CmdTest CheckId="123456789">

- <ExceptionMessage>

<id>003</id>

<text>Test</text>

- <arguments>

<argument>2.2</argument>

<argument>2.1</argument>

</arguments>

<creator>Hello</creator>

</ExceptionMessage>

</CmdTest>

</ServiceProtocol>

I have no knowledge in transformation. Can some one help me on this or provide deatail guideline on the conversion of XML to ABAP data.

Thanks in advance,

Best Regards, Johnney.

Former Member replied

Hi Johnney,

It's actually not that difficult. For that little XML of yours, I would suggest a Simple Transformation, because it's ... simple. And fast.

You should do some reading in the online documentation, and then it's trial-and-error. To accelerate your trialling, I've prepared a little example for you. Which you can use as follows.

Call SE80, and go Workbench > Edit object... , and create a "Transformation" (you give it a suitable name, and click on the "Create" button; in the pop-up, you specify Transformation type = "Simple transformation").

In the source code, you replace everything between "<tt:transform>" and "</tt:transform>" with the following bit of code:

  <tt:root name="PROTOCOL_VERSION"/>
  <tt:root name="CHECK_ID"/>
  <tt:root name="MESSAGE_ID"/>
  <tt:root name="MESSAGE_TEXT"/>
  <tt:root name="CREATOR"/>
  <tt:root name="T_ARGUMENTS"/>

  <tt:template>
    <ServiceProtocol>
      <tt:attribute name="version" value-ref="PROTOCOL_VERSION"/>

      <CmdTest>
        <tt:attribute name="CheckId" value-ref="CHECK_ID"/>

        <ExceptionMessage>
          <id>
            <tt:value ref="MESSAGE_ID"/>
          </id>
          <text>
            <tt:value ref="MESSAGE_TEXT"/>
          </text>

          <arguments>
            <tt:loop name="Arguments" ref="T_ARGUMENTS">
              <argument>
                <tt:value ref="$Arguments.ARGUMENT"/>
              </argument>
            </tt:loop>
          </arguments>

          <creator>
            <tt:value ref="CREATOR"/>
          </creator>

        </ExceptionMessage>
      </CmdTest>
    </ServiceProtocol>
  </tt:template>

Your simple transformation is ready; all you have to do is call it from a little program, more or less as follows:

  DATA:
    l_prot_version TYPE string,
    l_check_id     TYPE string,
    l_msg_id       TYPE string,
    l_msg_text     TYPE string,
    l_creator      TYPE string,
    BEGIN OF ls_arguments,
      argument TYPE string,
    END OF ls_arguments,
    lt_arguments LIKE STANDARD TABLE OF ls_arguments.
  TRY.
      CALL TRANSFORMATION your_transformation_name
      SOURCE XML l_xml_string
      RESULT     protocol_version = l_prot_version
                 check_id         = l_check_id
                 message_id       = l_msg_id
                 message_text     = l_msg_text
                 t_arguments      = lt_arguments
                 creator          = l_creator.
    CATCH cx_st_error.
*     Your error handling comes here...
  ENDTRY.

The variables starting with "l_" should be character fields of appropriate length, or character strings, while "lt_arguments" should be a standard table with a single field called "ARGUMENT" (otherwise you'd have to change the name in the transformation, in the line that says "<tt:value ref="$Arguments.ARGUMENT"/>").

There's more pitfalls for you, like the encoding of the XML string, but you'll find examples of how to handle that if you look for class CL_ABAP_CONV_IN_CE, either here in the forum or in your system (or in the online help, of course).

Oh, and you should definitely catch and handle CX_ST_ERROR - transformations dump easily if something is wrong with the data.

Hope this helps - sure took longer than planned...

Rainer

1 View this answer in context
Not what you were looking for? View more on this topic or Ask a question