Providing Enterprise Data Simply via SAP BSP - Data Retrieval in XML


Overview

In the last few lessons, we talked about the BSP application itself, the data retrieval in JSON, and how to handle URL parameters. Today, I want to give some help to the developers, who want to retrieve their data simply in XML. To achieve this, we are going to perform a few modifications only.

Today lessons

How to retrieve data in XML format


Set MIME Type to XML

The first modification is very simple, let's change the MIME type of our page to simply application/xml.


Modify Existing Data Transform and Retrieval

In the source code, we only need to modify three lines of code (actually modifying the line 28 should be enough, but as you know me, I put great emphasis on the readability, so we are going to rename the variable json_writer to xml_writer as well):

  • rename variable json_writer to xml_writer (line 28),
  • change the type to if_sxml=>co_xt_xml10 (line 28),
  • replace variable json_writer to xml_writer (line 33),
  • replace variable json_writer to xml_writer (line 41).
DATA range_cityfrom TYPE RANGE OF s_from_cit.
DATA range_cityto   TYPE RANGE OF s_to_city.

IF p_cityfrom IS NOT INITIAL.
  range_cityfrom = VALUE #(
    (
      sign   = 'I'
      option = 'EQ'
      low    = p_cityfrom
    )
  ).
ENDIF.

IF p_cityto IS NOT INITIAL.
  range_cityto = VALUE #(
    (
      sign   = 'I'
      option = 'EQ'
      low    = p_cityto
    )
  ).
ENDIF.

SELECT * FROM spfli INTO TABLE @DATA(flight_schedules)
  WHERE cityfrom IN range_cityfrom AND
        cityto   IN range_cityto.

DATA(xml_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_xml10 ).

TRY.
  CALL TRANSFORMATION id
    SOURCE flightschedules = flight_schedules
    RESULT XML xml_writer.

  CATCH cx_xslt_format_error INTO DATA(xslt_format_error).
    " error handling
ENDTRY.

CALL METHOD response->if_http_entity~set_data
  EXPORTING
    data = xml_writer->get_output( ).


Test Your BSP Application

Now comes the best part of building something, running the app itself! For this, simply let's right-click on your BSP application and choose the option, Test!

The result should look like this:


Summary

Of course, there are many possible use cases of BSP applications, but now, for us, it's only a very simple and handy tool that we use to achieve our goals, namely building lightweight and flexible web services without SAP Netweaver Gateway.

Have you got the XML result after running your BSP app? I hope, you could follow me, and everything went fine. If not, then don't hesitate to leave me a comment below!


blog comments powered by Disqus