Use Data from Wikipedia
h3. Implement ABAP Program to fill Table ZCITYMAYORS with Mayors Finally we can now fill the Table ZCITYMAYORS also with Mayors. Together with the Transformation this little piece of ABAP does all the work: REPORT zget_mayors_for_cities. DATA: it_citymayors TYPE TABLE OF zcitymayors, wa_citymayors LIKE LINE OF it_citymayors, mayor TYPE full_name, trash TYPE string. PARAMETERS: s_city TYPE s_city LOWER CASE. SELECT * FROM zcitymayors INTO TABLE it_citymayors WHERE city LIKE s_city. * HTTP Client according to * BSP: Create a weather magnet using xml feed from weather.com DATA: client TYPE REF TO if_http_client, url TYPE string, xml TYPE xstring, c_xml TYPE string, city TYPE string. * Converter DATA: l_convin TYPE REF TO cl_abap_conv_in_ce. LOOP AT it_citymayors INTO wa_citymayors. * Use the Progress Indicator to show the user which City is processed CALL FUNCTION ‘SAPGUI_PROGRESS_INDICATOR’ EXPORTING percentage = sy-index text = wa_citymayors-city. city = wa_citymayors-city. * Spaces have to be replaced by _ in the URL REPLACE FIRST OCCURRENCE OF space IN city WITH ‘_’. CONCATENATE ‘http://de.wikipedia.org/wiki/Spezial:Export/‘ city INTO url. ****Create the HTTP client
CALL METHOD cl_http_client=>create_by_url
url = url
client = client
OTHERS = 1.
xml = client->response->get_data( ).
WRITE: / ‘HTTP Connection error: ‘, city.
* Wikipedia does not provide a encoding with the returned XML * so we have to do the conversion manually TRY. CALL METHOD cl_abap_conv_in_ce=>create EXPORTING encoding = ‘UTF-8’ input = xml endian = ‘L’ RECEIVING conv = l_convin. CALL METHOD l_convin->read IMPORTING data = c_xml. CATCH cx_root. WRITE: / ‘Problem during Character conversion: ‘, city. ENDTRY. ****Transform XML to ABAP Values
CALL TRANSFORMATION zwikipedia_mayor_to_abap
SOURCE XML c_xml
RESULT mayor = mayor.
WRITE: / ‘Data loss during transformation: ‘, city.
* Some Mayors already have pecial Pages REPLACE FIRST OCCURRENCE OF ‘[[‘ IN mayor WITH ”. REPLACE FIRST OCCURRENCE OF ‘]]’ IN mayor WITH ”. * Some Mayors are members of a Party SPLIT mayor AT ‘(‘ INTO mayor trash. wa_citymayors-mayor = mayor. WRITE: / wa_citymayors-city. * Update Database IF NOT wa_citymayors-mayor IS INITIAL. UPDATE zcitymayors FROM wa_citymayors. WRITE: wa_citymayors-mayor. ENDIF. ENDLOOP. The result running this Program with the Parameter ‘Tr%’ the result in table zcitymayors is: