XML Files Creation in Abap [PDF]

  • 0 0 0
  • Gefällt Ihnen dieses papier und der download? Sie können Ihre eigene PDF-Datei in wenigen Minuten kostenlos online veröffentlichen! Anmelden
Datei wird geladen, bitte warten...
Zitiervorschau

XML transformation Example SAP ABAP Hi All,

There are several ways you can create XML outbound files in SAP. The method I have shown is with a simple transformation using XSLT_TOOL. XSLT_TOOL is a SAP provided tool/wizard to create XML transformations. In this example we are creating a XML file in the application server with sales order information. XML file holds multiple sales order information with sales order number, sales order type and customer information.  File format.

You require access to these transaction codes to generate this XML transformation. 

SE11 - ABAP Dictionary

 

SE38 - ABAP Editor XSLT_TOOL - Transformation Editor

1. Create table type.

Depending on the XML file format, matching ABAP table type should be created.  Go to transaction SE11. Create underline data type (structure) which match XML "customer" segment.

Create data type which correspond to "Order" segment in XML.

Create table type.

2. Create transformation. Go to transformation XSLT_TOOL.

Provide name and click create. Set description and transformation type.

Select wizard button "Edit Simple Transformation Graphically".

Right click on root element and select "insert new root".

Provide header root element name and table type name.

Drag and drop data root into simple transformation section.

Double click of each element/node and change the name as desired to be shown in XML.

You can set any element as a attribute of a segment as needed. In this example "order-no' is set as a attribute of segment "Order". Right click on the element and select "Change to attribute".

Save and activate. Final transformation.

3. Create outbound program.

Outbound driver program consist of,   

Data selection logic. Program select sales order information on sales order creation date selection (Form select_data). Prepare data to XML format (form prepare_data). Call XML transformation created in XSLT_TOOL (Form download_xml).



Download XML data to file (form download_xml). 

XML file would be downloaded as below.

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 tag represents a single order, represents order header details while node of XML is repeated multiple times representing order line details. Important points to note is: 1. 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 tags exist) 2. tag would always exist once in a single 3. tag can exist multiple times in a single . 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. tag, a table type has been created.

The structure for this table type represents and 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 tag. ‘ZVIN_ORDERL_XML’ tag represents our 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 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 and . 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 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: 10000 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 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. 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 2013-10-10 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.