Simple way to import deep XML in ABAP


Overview

After I published the previous post Simple way to generate deep XML in ABAP, many of you indicated that would learn about the XML to ABAP mapping as well.

As always, I will share with you a simple approach - my favorite one - using the tool Simple Transformation.

If you are not familiar with this tool, let's read this post first: Simple XML Processing in ABAP Part I. – Overview


Expected Deep Structure

The task is to import the data from an XML file, process them and finally insert them into different database tables.

For demonstration purposes we will use the table named ZORDER that contains the header information like Order ID, Customer ID, or the Order Date.

We will also use another table named ZORDERITEM that contains the item information like the Book ID, Quantity, or the Unit Price.

This is the content of the XML file that we want to import to the database tables I showed you previously:


Development Workflow

You can easily import a deep XML like above if you follow this development workflow:
  1. Creating necessary ABAP Dictionary objects
  2. Generating Simple Transformation
  3. Creating a simple ABAP program (in SE38) for testing


Creating necessary ABAP Dictionary objects

First create a simple flat structure, ZORDERITEM_TYPE for holding an order item.

Then wrap this flat structure into a table type, ZORDERITEM_TTYPE enabling you to store multiple order items as well.

Then use this table type and create a deep structure, ZORDER_TYPE for importing an order and its order items.

Then wrap this deep structure into a table type, ZORDER_TTYPE enabling you to store multiple orders and their order items as well.


Generating Simple Transformation

The next step is to create a new Simple Transformation in the transaction, STRANS.

In the graphical editor, first let's insert your deep structure, ZORDER_TTYPE as a data root.

Then using this data root let's generate the Simple Transformation via drag&drop the data root from the left side to the right side of the screen.

After the generation you only need to adjust two tags in the Simple Transformation. You need to simply rename the tag ZORDER_TYPE to ORDER, then the tag ZORDERITEM_TYPE to ORDERITEM. That's all!

Here is the final source code of the generated Simple Transformation:


Creating a simple ABAP program (in SE38) for testing

To test the Simple Transformation, let's simply create an ABAP program in SE38, ZORDERIMPORT.

The following source code is responsible for

  1. uploading the data of an XML file to a string internal table,
  2. converting the data in the string internal table to a flat string,
  3. parsing the flat string to a deep structure in the memory,
  4. extract the data from the deep structure,
  5. and inserting the the database.


Running the ABAP program in debug mode

If you check the program in debug mode after the Simple Transformation parsed the XML content into the deep structure, this is what you see.

This is the content of the header table.

This is the item table of the 1st order.

This is the item table of the 2st order.


Running the ABAP program

After running this ABAP program here you can see the inserted records in the database tables.

Header data

Item data


Summary

If you know the structure of the XML you have to import, then replicate this structure in the ABAP Dictionary. Then using this structure you can generate a Simple Transformation that will map the XML tags to the ABAP fields.

If you have any question about this topic, then don't hesitate to leave me a comment below!

If you liked it, please share it! Thanks!

blog comments powered by Disqus