Skip to Content

XML to ABAP conversion and vice-versa

During my recent assignment at client’s place, the work
schedule was very tight and from first day itself we have to start working on
objects. I was given the responsibility to do a POC on XML to ABAP conversion and vice-versa. The area
was totally new to me as I had never heard anything related to XML in SAP.I was
surprised and feeling bad for my luck that why I got this object and now I am going to get
screwed badly but thanks to our ocean of information “SDN” for its tonnes of information.

                 I started researching all the posts
related to XML conversion and one thing I found common on all of them that they
were taking about XSLT transaction(oops again a new thing..).I tried using this
transaction but I was not able to convert as per client’s requirement.

The data was coming from the legacy system based on XML files, one XML
file was having fieldcatalog and relating to this there can be 1000’s of XML
files containing actual data. The datastage was using BAPI’s to load these
files into SAP tables. The motif was to pick these files from tables and
convert into internal tables and report screen has to be given where user can
see the data and he can change the data which is not in the correct format
(e.g. Time, date etc.).After the changes again the changed the data was getting
converted to XML and stored in the SAP tables. The biggest challenge was to
read the XML file based on fieldcatalog (there can be any number of fields) and
based on that prepare the tables so that data XML files can be put into this.

After a lot of research I came to conclusion that, object oriented
programming is the best approach to achieve this.


<u>Here is the final analysis and report:</u>Pre-requisite: a sample XML document              





  h1. Order Status



      1. Part No. 0816, Units 2, Unit Price 14.50 USD


      2. Part No. 1504, Units 5, Unit Price 09.95 USD




    Methods used for conversion:

    The i_XML
    factory method is used which helps in implementing 3 core XML services:

    a)XML parser* :*  The XML parser’s task is to
    take an XML document – which is basically a chunk of text data – analyze it for
    syntactical correctness and create an in-memory representation of the data and
    the data’s structure in two ways:

      1. As “events”, which
        means that the parser passes control back to the calling application whenever the next piece
        of information has been parsed from the XML document (e.g. here’s a new
        element, here’s a new attribute etc.).
      2. As “DOM” tree, which
        means that the parser creates a tree of object instances in memory. The tree is
        reflecting the structure of the XML document and the tree nodes – the object
        instances – represent the logical elements and their data in the document. The
        DOM (Document Object Model) defines interfaces to traverse, query and modify
        the tree and therefore the underlying XML document.

    b)XML DOM 1.0: DOM – or
    Document Object Model – is a definition of classes and interfaces that represent
    the structure and content of an XML document. For each logical construct in an
    XML document there is corresponding class or interface in the DOM. Here’s an
    example: elements are represented as objects of the Element class, attributes
    of elements are represented as objects of the Attribute class, the text data of
    an XML document is represented as objects of the Text class .

    c)XML renderer: create an XML document by writing
    the appropriate bytes into a byte stream which can be used for uploading or
    downloading into the file.


    *Parsing an XML document using Event based
    parsing is used

    Event-based parsing of an XML
    document allows to process data from that document in smaller chunks and in the
    order in which it is found by the parser. Whenever the parser has found a new
    logical element – e.g. an element, an attribute, text data, a comment, a
    processing instruction etc. – it will signal the occurrence to the application
    in a corresponding event. With i_XML the event is not just a point in time, but
    an interface, the if_ixml_event interface. You can use this interface to obtain
    the associated data – e.g. the element’s name or the attribute’s value etc. –
    and trigger the further processing in your application.

    In event-based parsing, the
    parser will return events to the application one by one. Whenever a new event
    has been found, the parser will return from the parsing call and let to do the
    job. In order to keep the parser going – i.e. parsing the complete document –
    call the parse method again once processing the previous event completes.

    <u>Iterator approach</u>

    For this iterator-like approach,
    explicitly tell for which events the “iterator” is supposed to stop.
    This is done by calling the if_ixml_parser::set_event_subscription method and
    passing an OR-combined list of event types.

    Iterators have three features when used with the DOM:

    • They
      allow to hide the internal workings of the data structures they work on
    • By doing
      so they allow to implement iterator-internal optimizations to utilize
      specific aspects of the data structure over which they iterate

    <u>XML TO ABAP:</u> <br />

    <!Session data>

    You must be Logged on to comment or reply to a post.
    • I have used CALL TRANSFORMATION ID to do this type of processing with good results (and quick).<br/><br/>code looks like this:<br/><br/>"  EXPORTING<br/>"     VALUE(XML_RESULTS) TYPE  STRING ABAP

      • Hi Joe,

        I tried using call transformation and it will also work for normal fixed transformations.
            But using call transformation you cannot achieve typical client's requirement because in some cases the structure XML can change multiple times and if you are using call transformation then you need to handle each of them separately.
        Using this method you can change according to your structure with great ease.
                      At the end of the day everything depends on your requirement and comfortableness to use any method.

        Thanks and Regards,

    • I ma trying to convert an XML file into SAP internal table in 4.6c,Do u have any sample code that i can see and work on. I tried using Call transformation, but this method id not available in 4.6c.

      Can you please help me with this??