Utility to build HTML within ABAP

By | January 30, 2013 | ABAP Objects, Utilities | 13,808 | 6

Introducing a small utility class to build HTML code within ABAP. This utility wraps the logic for build HTML Tags and string manipulation within itself for simple client access.


Recently, I needed to generate an email notification using HTML. The email notification would contain the data in Table format. I don’t like to concatenate and build HTML string right within my code. So, I decided to divide my HTML email format into two parts:

  • Template with “static” data – like Main CSS, headers, tables headers etc
  • Place holders for “dynamic” data – Like table details

I still need to prepare HTML for the dynamic part of the template. Once I have the data, I can replace it in my HTML template where my placeholder is. So, I thought of creating a small class which would have very basic logic to prepare an HTML for any HTML tag.

Class Documentation

I designed this class with these methods:

  • CREATE_ELEMENT: Create any HTML element. Here you need to pass HTML tag as the parameter. This is should be the very first to get called after object instantiation
  • ADD_ATTRIBUTE: Add attribute to a HTML Tag like CSS Class, Style, ID, etc.
  • ADD_INNER_HTML: Add Inner HTML to the tag. Here you specify the HTML content which is visible on the document. Like text of link on a tag
  • APPEND_CHILD: Append HTML object to parent HTML object. Like Creating a P tag within a DIV tag
  • GET_HTML: This method will return you the HTML text. Method traverse through all child nodes of any given node. Method is based on composite design pattern, so you only need to call this method for your parent or root node. If you need to get any specific HTML, you can call it for any node.

Class has these private attributes which are getting accessed via above mention methods:

  • TAG: This contains type of the tag. e.g. DIV which is set by CREATE_ELEMENT
  • INNER_HTML: This contains the Visible text of the HTML tag set by ADD_INNER_HTML
  • ATTR: Table containing pair of attributes with name of attribute and a value
  • CHILDREN: Table containing all child nodes for a given node.

Class Code lines

Code lines for Class definition & implementation

class LCL_HTML

*       CLASS lcl_html DEFINITION
*       HTML Tag class
      create_element IMPORTING tag TYPE string,   " new element
      add_attribute IMPORTING attr TYPE string    " add any attribute
                              val  TYPE string,
      add_inner_html IMPORTING val TYPE string,   " Inner text
      append_child                                " Add a child
        IMPORTING io_tag TYPE REF TO lcl_html,
        RETURNING value(rv_string) TYPE string.   " Get HTML string
    DATA: tag TYPE string.
    DATA: inner_html TYPE string.
      BEGIN OF ty_attr,
        attr TYPE string,
        val  TYPE string,
      END OF ty_attr.
    DATA: attrs TYPE STANDARD TABLE OF ty_attr.
    DATA: children TYPE STANDARD TABLE OF REF TO lcl_html.
ENDCLASS.                    "lcl_html DEFINITION
  METHOD create_element.
    me->tag = tag.
  ENDMETHOD.                    "create_element
  METHOD append_child.
    APPEND io_tag TO me->children.
  ENDMETHOD.                    "append_child
  METHOD add_attribute.
    DATA: ls_attr LIKE LINE OF me->attrs.
    ls_attr-attr = attr.
    ls_attr-val  = val.
    APPEND ls_attr TO me->attrs.
  ENDMETHOD.                    "add_attribute
  METHOD add_inner_html.
    me->inner_html = val.
  ENDMETHOD.                    "add_inner_html
  METHOD get_html.
    DATA: lv_string TYPE string.
    DATA: lv_attr_val TYPE string.
    DATA: ls_attr LIKE LINE OF me->attrs.
    DATA: lo_child TYPE REF TO lcl_html.
    DATA: lv_child_html TYPE string.
*   opening bracket
    CONCATENATE `<` me->tag INTO lv_string.
    LOOP AT me->attrs INTO ls_attr.
      CONCATENATE ls_attr-attr `='` ls_attr-val `'`
        INTO lv_attr_val.
      CONCATENATE lv_string lv_attr_val
        INTO lv_string SEPARATED BY space.
    CONCATENATE lv_string `>` INTO lv_string.
*   inner html
    CONCATENATE lv_string me->inner_html INTO lv_string.
*   child
    LOOP AT me->children INTO lo_child.
      lv_child_html = lo_child->get_html( ).
      CONCATENATE lv_string lv_child_html
        INTO lv_string.
      CLEAR lv_child_html.
*   closing
    CONCATENATE lv_string `</` me->tag `>`
     INTO lv_string.
*   back the HTML
    rv_string = lv_string.
  ENDMETHOD.                    "get_html
ENDCLASS.                    "lcl_html IMPLEMENTATION

Sample Program to generate HTML code

Sample program to generate some HTML code

Sample program using LCL_HTML

  data: lo_div type REF TO lcl_html.
  DATA: lo_tab TYPE REF TO lcl_html.
  DATA: lo_tr  TYPE REF TO lcl_html.
  DATA: lo_td  TYPE REF TO lcl_html.
  create OBJECT lo_Div.
  lo_div->create_element( 'div' ).
  lo_div->add_attribute( attr = 'id' val = 'mydiv' ).
  lo_tab->create_element( 'table' ).
  lo_tab->add_attribute( attr = 'class' val = 'maintab' ).
  lo_tr->create_element( 'tr' ).
    CREATE OBJECT lo_td.
    lo_td->create_element( 'td' ).
    lo_td->add_inner_html( 'Col' ).
    lo_tr->append_child( lo_td ).
  lo_tab->append_child( lo_tr ).
  lo_div->append_child( lo_tab ).
  DATA: lv_html TYPE string.
  lv_html = lo_div->get_html( ).
  WRITE: lv_html.

This code will generate output like this, which is well formatted HTML.

<div id='mydiv'><table class='maintab'><tr><td>Col</td><td>Col</td><td>Col</td><td>Col</td><td>Col</td></tr></table></div>

I have created a Project HTMLinABAP on google code. If you wish to make any changes, drop me a line and I would make you a team member.

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{273 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 273 articles.

Load comments


  • Valeriy

    There is XSLT-transformation for purpose that you wrote.

  • Pavel

    Nice! Few days ago I considered creating the same class. Copy+Paste and my laziness were stronger though. :)

  • Hello Valeriy,

    I tried to search for a good example using XSLT to generate the HTML but couldn’t generate the way I wanted. Do you know how I can achieve that?

    Naimesh Patel

  • Krishna kammaje

    Great post!
    XSLT transformation in ABAP will only convert between XML to XML, XML to ABAP, ABAP to XML, or ABAP to ABAP.

  • Skrishna


    There are couple of classes in package SDYNAMICDOCUMENTS for creating HTML output. Wonder if they could have been used. Not sure if they would have helped exactly the way you needed.

  • Hello Skrishna,

    The classes in the package SDYNAMICDOCUMENTS looks prominent. I would need to try to use it in the example and see if it provides the needed HTML string.

    Thanks for pointing it out.

    Naimesh Patel

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.