Recently I got a question from one of my reader, Rakesh. He wanted to generate a deep XML from ABAP using the data in the SAP tables. He asked me to help how to do it in ABAP.
As always, you have multiple solutions for this problem in ABAP. In this post, I will share with you a simple approach - my favorite solution - for this problem.
Now we will cover only the ABAP to XML mapping, if you are interested in the XML to ABAP mapping as well, then don't hesitate to leave me a comment below, and I will create a blog post about that one as well!
If you are not familiar with any of these tools, let's read my previous posts first:
Expected Deep XML Structure
The task is to collect the data from the different tables, merge them, and finally display them as a deep structure.
For demonstration purposes I created a table named ZORDER that contains the header information like Order ID, Customer ID, or the Order Date.
I also created another table named ZORDERITEM that contains the item information like the Book ID, Quantity, or the Unit Price.
So the task is to collect the data from these tables, merge them, and finally display them as a deep structure like this:
You can easily produce a deep XML like above if you follow this development workflow:
- Creating necessary ABAP Dictionary objects
- Generating Simple Transformation
- Creating BSP Application 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 your XML holding multiple order items as well.
Then use this table type and create a deep structure, ZORDER_TYPE for holding an order and its order items.
Then wrap this deep structure into a table type, ZORDER_TTYPE enabling your XML holding multiple orders and their order items as well.
Finally let's place this table type into a deep structure, ZORDEREXPORT_TYPE that will be your root structure containing information like the Export Date, Export Time or the Export User, and of course the list of your orders.
Generating Simple Transformation
The next step is to create a new Simple Transformation in the transaction,
In the graphical editor, first let's insert your deep structure, ZORDEREXPORT_TYPE 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 BSP Application for testing
To test the Simple Transformation I simply created a BSP application in
SE80, ZORDEREXPORT, and within that a simple page, orderexport, and placed the source code into the event, OnInitialization.
The following source code is responsible for collecting the data from the tables, merging them together, and displaying them as a deep XML.
After testing this BSP application here is the deep XML data containing the orders and also their order items:
If you know the structure of the XML you have to produce, then replicate this structure in the ABAP Dictionary. The using this structure you can generate a Simple Transformation that will map the ABAP fields to the XML tags.
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!