Let me share an experience. I was challenged with a task to read huge XML into ABAP and use it for mass upload of some information. It was not my first time with XML in ABAP, I am regularly using CALL TRANSFORMATION to generate formatted Excel files (I don`t want to import ABAP2XSLX, what would be the first option to produce nicely formatted outputs to Excel, into my customers` systems. Can you tell me how you use ABAP2XSLX not being a customer?). But it was my first time to upload into ABAP.
Side note: If you don`t know the ABAP2XSLX project, check abap2xslx Code Exchange homepage here. If you want to start with a fancy video introduction of the project, check the blog by Rui Nogueira here. If you don`t want to import Z-code into your or your customer`s system and would like to know more about my transformation way of exporting to Excel, check my blog which describes it here: Happy reporting with Excel II.
So back to the topic: you have an XML file you`re supposed to read into ABAP structures and tables and you don`t know where to start. One could use a wizard to help with the task, right? Fortunately there is such a thing. Look for the magic wand icon in the transformation screen in SE80 (or alternately go to XSLT_TOOL transaction, open or create a transformation and go from there). Note that this blog is about SIMPLE TRANSFORMATIONs, not about XSLT programs (your transformation object in SE80 or XSLT_TOOL must be of type simple transformation).
What you see here are two panes: left for data structures in ABAP and right for the XML tags in the file. First thing you`re supposed to do is to specify a root. Ugh? Root? What is that?
You need to have ABAP dictionary structures and tables defined which will hold the information after you read it from the XML file. These you have to create manually. So before you start using the XML wizard, I suggest you start creating your structures in SE11 or SE80. Combine the data dictionary objects into a tree, that will approximately look like the XML file you`re going to read into it. The more it “emulates” the input file the better. Natural, no special description needed.
Now let me explain why I suggest the use of the XML tool. Those informed about CALL TRANSFORMATION know there is the “standard” transformation called “id”. “Id” stands for “identity” here. That means that the XML file content and the DDIC structures fit together 100%. With my task that was not the case.
Parts of XML dealing with similar things were “implemented” differently, some nodes had attributes, some didn`t, some elements were obligatory, some not. I didn`t have XSD, I didn`t have any other information except the “example file”. When you want to use the “identity” transformation, you anyway need to create your DDIC structures, so this part stays with both options. To make it as clear as possible: this blog helps you with mapping the XML nodes to DDIC structures.
Let`s assume you have your structure created in ABAP dictionary now and want to create a mapping. You now have a structure you can use as the new root in XSLT_TOOL (right mouse click on the left pane > Insert New Root > provide root name and type you created as the “main/ top-level” type). Left pane gets populated according to your structure defined. You don`t have a thing on the right side yet.
It`s a mapping. Have you ever used attributes mapping in WebDynpro for example? Drag and drop works. So you drag the root from the left pane and drop it on the right. Now the program creates what I mentioned above as “identity” transformation. It created a “program” that would read the XML file where every tag is called the same as your DDIC type. But as I said: ours is not the case. We need to be able to change the names of the tags (and other “attributes”) so the transformation fits on our XML. To do that double-click the node on the right and you can change its name.
Another typical task is to map your ABAP field to an attribute of a XML tag rather than to its value. Then you need to right click on the node and say: “Change to Attribute”.
Not a replacement for a documentation, but a shortcut for sure
You might be asking yourself why I explain something that can be read in the documentation. Easy: I had to learn the hard way despite the existence of the documentation. I also realized that I need less that 5% of the available features. So if I went and read the whole documentation, I would spend a week reading about cloud castles, something that can be very tricky to imagine. With this blog I am hoping to spare the Community the time and effort of reading through everything plus… I want to add some “typical needs”.
Typical problem: You use the wizard, save, activate and when want to start the wizard again or activate again, you get a success (???) message saying “Namespace prefix “” is not defined”. Then you need to go to the source code tab of the transformation and note that there are two similar, but somewhat strange lines at the beginning. You need to delete the second line (with extra space before the question mark) to make the thing work again.
Typical wish will also be the need to mark some nodes optional. Here I admit I didn`t find a way how to do it via the wizard (it must be possible, I just didn`t have enough time to look for it). So I went to the transformation source code tab and change the thing there.
The code goes like this: (if there is something to be bound to the node, process it, and otherwise carry on; note that you have to “process” every tag that appears in the XML input stream, which means if you don`t care, ok, but you have to process it too).
< MyTagB >
<!– process the lines if there are any –>
And that is enough for you to process complex XML files into ABAP. At least it was enough for me. Happy XML parsing.