In the previous post, we finished our framework program that is able to call our previously constructed Simple Transformation to parse the content of the XML into SAP database table, ZCUSTOMER.
Today, I want to share you another challenging use case for processing XML documents, namely how to process XML attributes in the XML document using Simple Transformation. It may sound a bit difficult, but we will see that SAP makes our job really easy. We can perform all mentioned operation below via the source code editor or the visual editor. As you got familiar with the visual editor in the previous posts, now I am going to demonstrate the steps in the source code editor (Note: it doesn't matter which IDE you use, it has the same effect).
Scenario: There are attributes in the XML document
Now, I have changed our XML document a little bit. As you can see, the CUSTOMER_ID is no longer an element, instead now it's an attribute that is located next to the element, CUSTOMER (Note: to avoid duplication in the database table, I have changed the IDs also).
In this case, we need a bit different Simple Transformation that is able to parse the CUSTOMER_ID attribute and save its content into the same CUSTOMER_ID field.
We have existing ABAP Dictionary objects, Simple Transformation, and a framework program. In order to satisfy the new requirements (parsing CUSTOMER_ID as an attribute of the CUSTOMER), we only have to adjust the Simple Transformation, since we are going process the content of the XML with the same way and with the same framework program, and save them into the same database table.
Edit the Simple Transformation
As a first step, let's open the transaction, STRANS and open our Simple Transformation, ZCUSTOMER_ST in edit mode. (Note: this is the last version of our Simple Transformation)
The attributes for first
To change the line, CUSTOMER_ID into an attribute mapping, first we have to move it to the top of the CUSTOMER hierarchy, by replacing it with the line, MANDT, since attributes must not occur after elements, the attributes have to be listed for first.
Define CUSTOMER_ID as an attribute
Now, we can change the line, CUSTOMER_ID into an attribute mapping, by defining it with the keywords attribute and name. So, parsing XML elements, and attributes only differs in these keywords.
Check the result
That's it! Now, if we run our framework program with the modified XML document, we get the same results in the database table, ZCUSTOMER as previously, but now the CUSTOMER_ID comes from the attribute of the CUSTOMER.
Now, as you have completed this demo, you are able to parse simple XML documents with listed elements, and more complex XML documents with attributes also.
Table of Contents