Skip to Content

A story about Twitter, XML and WD4A


Like many of us, I’m a kind of addicted to Twitter. But a few weeks ago, the admins of my client cuts the connection to Twitter and all of the known anonymanizers like “agentanon“. My hands began to tremble, my work became poorer and poorer (just a joke!).

Two lucky circumstances:

  • first free weekend since many month
  • my SAP PRD server is already up and connected to the internet, because I have a presentation on Monday

Why don’t turn a problem into a challenge and develop \ my own “ABAP twitter client” ?

Okay, with the help of the twitter API wiki I’ve started with a tiny program like this to see the data format of the twitter response:

DATA: gr_client TYPE REF TO if_http_client
    , gv_content TYPE string

    host = ''
    client = gr_client

  name = '~request_uri'
  value = '/statuses/public_timeline.xml'

  username = 'se38'
  password = 'not_my_password'

gr_client->send( ).
gr_client->receive( ).
gv_content = gr_client->response->get_cdata( ).
gr_client->close( ).

The response looked like this:


Not really complicated I thought and I coded a \ corresponding data structure:

     ,   id TYPE string
     ,   name TYPE string
     ,   screen_name TYPE string
     ,   description TYPE string
     ,   location TYPE string
     ,   profile_image_url TYPE string
     ,   url TYPE string
     ,   protected TYPE string
     ,   followers_count TYPE i
     , END OF ts_user

     , BEGIN OF ts_status
     ,   created_at TYPE string
     ,   id TYPE string
     ,   text TYPE string
     ,   source TYPE string
     ,   truncated TYPE string
     ,   in_reply_to_status_id TYPE string
     ,   in_reply_to_user_id TYPE string
     ,   favorited TYPE string
     ,   user TYPE ts_user
     , END OF ts_status

DATA:  gt_statuses TYPE TABLE OF ts_status.

Because of the simple structure I have decided not to use simple transformation but the class CL_XML_DOCUMENT, that I have used before for creating an XML document out of a DDIC structure.

After hours of trying to parse the twitter response into my structure without success, I tried the vice versa way: filling my structure with test data and creating an XML document. The result:


Do you see the difference?

The “status”-node became “item” and all other tags are in upper case.

Using REGEX within ABAP and much “Hirnschmalz” I transformed the twitter response into the CL_XML_DOCUMENT conform input.

SPLIT gv_content AT cl_abap_char_utilities=>newline INTO TABLE gt_data.

LOOP AT gt_data
  ASSIGNING <gv_data>.

  IF sy-tabix > 1.

    FIND ALL OCCURRENCES OF REGEX '<[^>]*>' IN <gv_data> RESULTS gt_results.

    LOOP AT gt_results
      ASSIGNING <gv_result>.

      ASSIGN <gv_data>+<gv_result>-offset(<gv_result>-length) TO <gv_tag>.





With this input the parsing works as expected:

DATA: gr_xml_doc TYPE REF TO cl_xml_document
       , gv_rc      TYPE sysubrc

CREATE OBJECT gr_xml_doc.
CHECK gr_xml_doc IS BOUND.

gv_rc = gr_xml_doc->parse_table( gt_data ).

    retcode = gv_rc
    dataobject = gt_statuses

Be careful here: for GT_DATA don’t use a “TYPE TABLE OF STRING”, else:

  1. For the tag replacements you cannot use offsets
  2. The CL_XMS_DOCUMENT produces a dump 😉

Most of the work done (I thought!), now the WD4A GUI

First build the context like the above defined \ structure, bind the table to the context, embed a table into the view, bind the \ context to this table, etc. etc. etc.

But no:

lo_nd_statuses->bind_table( new_items = gt_statuses ).

The status fields were filled, but not the user substructure!

And again, the world would be so boring without these little hurdles 😉

New internal table without substructure, mapping the fields to the new table, creating new context:


The rest is some kind of finger exercise and not part of this blog.

Conclusion and questions:

  • It works:-)
  • Why we have to use the “item” tag ?
  • Why we have to use upper case tags ?
  • Why we cannot bind a table with substructures into a context with substructures ?
  • Was a nice “project”, had a lot of fun!

If someone has a better/smarter/quicker way to develop such kind of XML-WD4A-Bindings, or has the answers for the mentioned questions, please don’t hesitate to comment this blog.

By the way: here is the result:



You must be Logged on to comment or reply to a post.
  • Hi Uwe,
    Thanks for the interesting blog. I like reading about those kind of hurdles and how they are overcome. 🙂
    One observation about your ABAP data structures: Now you have typed GT_STATUSES to TABLE OF TS_STATUS. If instead you give it one named element STATUS which is typed to TABLE OF TS_STATUS, your XML document would probably come out with the desired structure, thus saving you the nasty search and replace business.
    Cheers & I look forward to your next blog,
    • Hi Thorsten,
      If I remember correctly, the named structure "status" was one of the tasks, which I have already tried yesterday. Unfortunately this doesn't work neither.
  • although I stopped twitter use due to high time consumption of "twitter practice" I will test your application, great job. Thanks for sharing.
    • Hi Steven,
      the only missing part in the blog is the "send" routine. Will post the code later (only have a Webgui connection to my server in the moment).
      Distribute the code snippets to the appropriate WD4A methods and you have a working application 😉
      But like I mentioned in the blog: to create the web application was not the problem (and not part of this blog, there are much better blogs about WD4A development).
      Regards, Uwe
    • And here's the missing part:

      *--- send message ---*
            host = ''
            client = lr_client

          name = '~request_uri'
          value = '/statuses/update.xml'

          name  = '~request_method'
          value = 'POST'

          name  = 'status'
          value = lv_twitt

          username = lv_name
          password = lv_password

        lr_client->send( ).
        lr_client->receive( ).
        lv_content = lr_client->response->get_cdata( ).
        lr_client->close( ).

  • Already told you on Twitter...This is brilliant! I really love how you manage to bring Twitter to the SAP world...Great job! You have already made a place into my "Blag's best blog picks for 2009"!


  • believe it or not, you can work at SAP without knowing how to code ABAP -- but this sort of thing makes me want to learn! ABAP!

    thanks for this post,

  • Hi, very nice blog! Im new on this so I was wondering since you mentioned that gt_data shouldn't be a type table of string, what should it be then? Pardon the ignorance. Thanks.
      • And gt_data is supposed to be gt_data type table of tv_data? was trying to download the nugget, but i can't seem to make it work. Please be kind enough to post the data declaration of gt_data. Thanks.:)
        • You are right with "gt_data type table of tv_data".
          What is the problem with the nugget? The download or the installation of the nugget?
          You can contact me via twitter (@se38) 😉
          Regards, Uwe
  • Hi,

    I'm trying to rebuild your project and I get the tweets from the twitter server. That works really perfectly.

    But I have an other simple problem:

    - How can I get line breaks in the result table playced in the webDynpro view???

    Greets and merry xmas

    Dirk Voigt

  • That's ... frustrating? Got a similar problem here. 75% are just gone, and the blog was featured on the Custom Development -> Java landing page of the old SCN.

    Lucky me: I write my blogs 1st as a DOC and then copy & paste the blog-worthy content.

    • I'm writing blogs also in Word first. But after the blogs where published, I always delete them on my PC. Why hold a copy if we have them on SCN 😉

      (already in contact with Oliver Kohl to rescue my blogs)

      • I always keep copies of blog posts, whether here on SCN or elsewhere, separately. I save them into Evernote where I have both a cloud-based copy and a local copy on my laptop. I'm afraid I just don't trust any service, whether SAP, WordPress or Google (my current personal blog is on Blogger) to not trash my content.

        One of my Golden rules - you can never have too many backups.

      • Just in case rescuing doesn't work, you might be able to find a copy on one of the notorious rip-off sites that steal blog content from SCN to attract page views and make money from showing Google Ads. Remember #blogtheft?



        • Most of these site integrate by iFrame, so that content is gone there too. And in my case: nobody really copied my content 🙁

          Think that is a sign I should finally switch to ABAP ...