Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member212005
Active Contributor

Recently I was working in a project wherein I had to read XML file in SAP system. I have been using iXML library for quite a long time, when such requirements arise.....however this time I thought of giving a try to Simple Transformations provided by SAP i.e. using XSLT_TOOL.

What would be covered in this tutorial is:

1. An overview of how to use Simple Transformation to read XML Data.

2. How to check for conditions of XML tags when reading XML file.

3. How to skip elements in XML file.

4. How to read attributes of XML tags.

5. How the concept of reading date and time fields work.

Both the XML file and the program has been attached for reference.

Overview

Two terms which are frequently used is "Serialization" and "Deserialization". Serialization refers to the process of converting data into XML while Deserialization refers to the process of reading data from XML file and using it in our program.

To start with, lets take a simple XML file and try to read it. Below is the screenshot of XML file.

This XML file was created manually for testing purpose and it clearly represents "Sales Order" data.

From the screenshot it is visible that <ORDER> tag represents a single order, <ORDER_HEADER> represents order header details while <ORDER_LINE> node of XML is repeated multiple times representing order line details.

Important points to note is:

1. <ORDER> tag can exist multiple times in a single XML file i.e. multiple orders can be sent in an XML file (We are not going to cover an example wherein multiple <ORDER> tags exist)

2. <ORDER_HEADER> tag would always exist once in a single <ORDER>

3. <ORDER_LINE> tag can exist multiple times in a single <ORDER>.

Based on the above 3 points it is clear, that we would require a deep structure in SAP to hold this data. Since there can be multiple orders we have to create a table type for the highest element of the structure.

Below screenshots would help to understand:

To hold various orders i.e. <ORDER> tag, a table type has been created.

The structure for this table type represents <ORDER_HEADER> and <ORDER_LINE> details of XML data

Note: ORDER_HEADER is a structure while ORDER_LINE is a table type, as we can have multiple order lines but only a single header.

Screenshots for ORDER_HEADER and ORDER_LINE are as follows:

After the data dictionary part is completed, now lets move towards creation of a Simple Transformation using XSLT_TOOL

Go to XSLT_TOOL and create a simple transformation and click on (Edit Simple Transformation Graphically button)

Once Inside, click on the "ROOT" node and create a new root as shown in below screenshot: (Here, we need to enter the Table Type name which we had created earlier)

Drag and Drop the new root created to right hand side i.e. towards "Simple Transformation" section. You would see a screen similar to as shown below:

Now, it is important to remember that the "Simple Transformation" should resemble our XML file always in terms of name....this is because transformation does the comparison word by word.

Anything after table symbol represents that the value could repeat multiple times. So, if we look carefully in the above transformation the entire node of 'ZVIN_ORDER_XML' would be repeated multiple times i.e. this represents our <ORDER> tag.

'ZVIN_ORDERL_XML' tag represents our <ORDER_LINE> tag. Hence rename the tags appropriately, so that our new transformations looks as below:

We are almost done now, however now we have certain segments which are repeating like "ORDER" and "ORDER_LINE"

The higher level segments with above names are not needed, so we go to the source code by clicking on "BACK" button and remove redundant nodes by deleting them. The final transformation should be as follows:

If we compare the above transformation with the XML file that we are trying to upload....it is a perfect match.

To call the above transformation, we use 'CALL_TRANSFORMATION' statement of ABAP.

Lets perform a small test and see if the ABAP program is reading XML data or not.

Yes, the program is loading data from XML successfully.

Conditional checks on XML Tags

Now, lets introduce some complexity i.e. we introduce additional fields to our XML file such as VKORG, VTWEG and SPART.

However these tags would not be mandatory in XML file, i.e. sometimes it would be present while sometimes it may be missing (NOTE: XML tags itself might be missing)...In general, if the tags are missing in XML file while the same is mentioned in Simple Transformation then it results in error stating "System Expected Element &&&"

To handle such conditions we can introduce special conditions in the Souce Code of transformation.

Screenshot of new XML file:

In the above screenshot SPART is introduced in XML file, however VKORG and VTWEG may also be present in future files.

To handle this, perform following:

We would need to modify our header data dictionary structure to hold VKORG, VTWEG and SPART data when they are provided in XML file. Make the necessary modification in Simple Transformation using XSLT_TOOL

Introduce conditional checks in Source code, as shown in below screenshot:

Statement <tt:d-cond check="exist(NodeName)"> ensures that if the tags are missing then move forward to next tag else read its data.

When we execute the program with the new XML file, we can see data was loaded successfully even though VKORG and VTWEG tags were missing.

Skip Elements/Tags of XML File

Now, let us handle another situation i.e. there are certain XML tags being passed in XML file which is of no use to me.

Now, either I can ignore the data after reading or SAP also allows us to skip processing of unnecessary tags.

Screenshot of new XML:

In the above XML, two new tags exist namely <OURREF> and <OURCUSTOMER>.

Data from these tags are not needed in SAP and hence we decide to skip processing of these tags by writing separate conditions in Simple Transformation.

Statement <tt:skip count="*" name="Node"> allows us to skip processing of node "Node" which is repeating consecutively any number of times.

* The count value may not always be '*'. Please check SAP help for more details.

How to read attributes of XML tag

Now, there are many cases wherein certain attributes exist in XML tags.

Once of the attributes which I had to encounter was as below:

<Order_Value Currency="HUF">10000</Order_Value>

In the above example the total order value is 10000 and the currency key for the same is "HUF". Now the requirement is that we are not only supposed to read the Order Value of 10000 but also the Currency Key as "HUF"

New XML File:

In the above XML file, we are passing the currency key along with the tag <VBELN> as an attribute. (I know that this is not a good example...consider it only for testing purpose)

Now, the requirement is to read the Currency key from the XML file.

We can do this by adding additional tags in Source Code....as shown below:


We need to introduce a new field in our Header Structure to hold "Currency Key" and then we need to simply pass the value as shown above.

<tt:attribute name="AttributeName" value-ref="NodeName"/> allows us to read the attribute value and pass it to ORDER_HEADER.WAERS field.

Screenshot after reading data in debug mode:

How the concept of reading date and time fields work.

More information on this can be obtained in the below link:

http://help.sap.com/saphelp_erp60_sp/helpdata/en/52/491a40251e2402e10000000a1550b0/content.htm

As per the above link, if the date and time are represented in ISO-8601 standard, the conversion of data in SAP would be made automatically.

For E.g. if there exists a tag <ERDAT>2013-10-10</ERDAT> in the XML file and the corresponding field in ABAP Structure has the datatype as 'D'....then while de-serialization data would be loaded in SAP as 20101010 i.e. internal format.

Additional References:

Below are some useful links which should help in understanding Simple Transformation Process:

SAP Help on various tags to be used in Simple Transformation

http://help.sap.com/saphelp_erp60_sp/helpdata/en/18/d16240e85cef6fe10000000a1550b0/content.htm

SAP Help on CALL TRANSFORMATION statement http://help.sap.com/abapdocu_702/en/abapcall_transformation.htm

Complete Simple Transformation - http://help.sap.com/saphelp_erp60_sp/helpdata/en/1e/c062405c910272e10000000a155106/frameset.htm


Disclaimer: The attached program and XML file are for test purpose only. There might be modifications required in Simple Transformation if the XML file is modified to include additional tags.


2 Comments