Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
anup_deshmukh4
Active Contributor

Hello Experts,

    Just like we have WE09 Transaction where we have IDOC content based Idoc searching in the database I would like to share a code snippit for the

XML Proxy  messages Content base Message Searching Program .

It becomes very hectic  when it comes to content based XML message Searching for inboud as well as outbound messages recieved from PI to ECC.

People general go to sxmb_moni to check and then filter by Interface name space date and time stamp.

Say if you have a Purchase Order inbound Proxy Interface ( for example PI - >  ECC ) and you want to know how many messages were just recieved  /  posted / or in error  within a specified Date and time Range for a SPECIFIC Vendor.  How are we going to do it ?

The program Frame work goes as follows.

1. Selection screen contains Interface Namespace / Date / Time stamp OR just bunch of XML Id

2. Obligatory field  :  The Token in the Pay load  . Say if you have a Purchase Order Inbound Interface then you must have some mandatory Vendor Field or similar kind of  Identifier in the same like<XYZ fileld  >.....</XYZ field> .....<VENDOR>0123456789</VENDOR> ......now <VENDOR> will be Our Token .

( Resembles to the Segment Field in the IDOC Packed in case of proxy its a Palyload token )

3. The Token Value Value Field  :  In the above Example for vendor the token value will be  0123456789 Optional coz.. if its blank it will fetch all the token with value .

4. Read the XML ID from the master table.

5. Extract the payload .

6. Retrive the Token Value

7. Display in a Report.

( Please Feel free to correct the errors and update with still better solutions or methods )

Hear goes the Code for same .


*{code}


REPORT  zread_xml_tokens.


TYPE-POOLS : slis, sdydo.


" Data Declaration


DATA : persist_main TYPE REF TO cl_xms_persist.


DATA : xms_msg_main TYPE REF TO if_xms_message .
DATA : xmb_msg_main TYPE REF TO if_xms_message_xmb.


DATA : payload      TYPE sxms_mmfpayloads.
DATA : wa_payload  LIKE LINE OF payload.
DATA : content_main TYPE TABLE OF string.
DATA : wa_str      TYPE string.
DATA : wa_msgid TYPE sxmsmguid.


DATA : lt_msgid TYPE TABLE OF sxmsmguid.
DATA : lw_msgid TYPE sxmsmguid.


" String variables to get the token value
DATA : str1 TYPE string,
      str2 TYPE string,
      l_token TYPE string,
      token TYPE string,
      str5 TYPE string.


DATA : lt_str TYPE TABLE OF string."Table to hold the splitted values
DATA : lv_string TYPE string.


**********************************************************************
" Data declaration for ALV
DATA: x_layout    TYPE slis_layout_alv.
DATA: ls_cellcolor TYPE lvc_s_scol.
DATA: ws_title    TYPE lvc_title.
*ws_title = text-027.
CONSTANTS : c_tab      TYPE char15 VALUE 'LT_FINAL'.


DATA : it_fieldcat TYPE slis_t_fieldcat_alv,      "Field catalog with field descriptions
      wa_fieldcat TYPE slis_fieldcat_alv.        "Work Area for field catalogue


DATA : text1 TYPE sdydo_text_element.
**********************************************************************


TYPES : BEGIN OF ty_final,
        xml_id TYPE sxmsmguid,
        value  TYPE string,
        cellcolor    TYPE lvc_t_scol,          "Color of column
        END OF ty_final.


DATA : lt_final TYPE TABLE OF ty_final,
      wa_final TYPE          ty_final.


" Index variables
DATA : l_index    TYPE sy-tabix.
DATA : id_index TYPE sy-tabix.
DATA : temp_index TYPE sy-tabix.


"Handling the exception
DATA : zcx_root TYPE REF TO cx_root.


DATA : lv_vers TYPE sxmslsqnbr.


TYPES : BEGIN OF ty_msgid,
        msgguid TYPE sxmsmguid,
        rest_vers TYPE sxmslsqnbr,
        END OF ty_msgid.


DATA : it_msgid TYPE TABLE OF ty_msgid,
      wa_msgid1 TYPE        ty_msgid.


DATA : s_timestamp TYPE tzntstmpl.
DATA : e_timestamp TYPE tzntstmpl.


DATA : range TYPE RANGE OF tzntstmpl WITH HEADER LINE.



TYPES : BEGIN OF ty_idtab,
        msgguid TYPE sxmsmguid,
        pid  TYPE sxmspid,
        rest_vers TYPE sxmslsqnbr,
        END OF ty_idtab.


DATA : it_idtab TYPE TABLE OF ty_idtab,
      wa_idtab TYPE      ty_idtab.
DATA : lv_dbcnt TYPE sy-dbcnt.
DATA : lv_final TYPE sy-dbcnt.
**********************************************************************


" Radio Button Screen
SELECTION-SCREEN: BEGIN OF BLOCK rd WITH FRAME TITLE text-001.
PARAMETERS : r1 RADIOBUTTON GROUP rad1 USER-COMMAND s_msg_id DEFAULT 'X',
            r2 RADIOBUTTON GROUP rad1.
SELECTION-SCREEN: END OF BLOCK rd.


"XML ID screen
SELECTION-SCREEN: BEGIN OF BLOCK xml WITH FRAME TITLE text-003.
SELECT-OPTIONS : s_msg_id FOR wa_msgid NO INTERVALS OBLIGATORY MODIF ID sc1.
SELECTION-SCREEN: END OF BLOCK xml.


"Namespace and date range screen
SELECTION-SCREEN: BEGIN OF BLOCK ns WITH FRAME TITLE text-004.
PARAMETERS :  int_ns  TYPE rm_oifns  MATCHCODE OBJECT s_interface_i MODIF ID sc2.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN : COMMENT 1(12) text-006 MODIF ID sc2.
PARAMETERS : s_date  TYPE sy-datum MODIF ID sc2.
SELECTION-SCREEN : COMMENT 26(12) text-008 MODIF ID sc2.
PARAMETERS : s_time TYPE sy-uzeit MODIF ID sc2.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN : COMMENT 1(12) text-007 MODIF ID sc2.
PARAMETERS : e_date TYPE sy-datum DEFAULT sy-datum MODIF ID sc2.
SELECTION-SCREEN : COMMENT 26(12) text-009 MODIF ID sc2.
PARAMETERS : e_time TYPE sy-uzeit DEFAULT sy-uzeit MODIF ID sc2.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: END OF BLOCK ns.


"Token and its Value screen
SELECTION-SCREEN: BEGIN OF BLOCK tk WITH FRAME TITLE text-005.
PARAMETERS    : p_token1 TYPE string LOWER CASE." OBLIGATORY. " The token for which the value has to be fetched
PARAMETERS    : p_value  TYPE string.
SELECTION-SCREEN: END OF BLOCK tk.


"Toggling between mode of selection


AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.
    IF screen-group1 = 'SC1' AND r2 = 'X'.
      screen-active = '0'.
      MODIFY SCREEN.
      CONTINUE.
    ENDIF.
    IF screen-group1 = 'SC2' AND r1 = 'X'.
      screen-active = '0'.
      MODIFY SCREEN.
      CONTINUE.
    ENDIF.
  ENDLOOP.


*AT SELECTION-SCREEN.
*  IF r1 = 'X' AND screen-group1 = 'SC1'.
*    screen-required = '1'.
*    MODIFY SCREEN.
*  ENDIF.



START-OF-SELECTION.


  IF p_token1 IS INITIAL.
    MESSAGE 'Provide a Token Value' TYPE 'I' DISPLAY LIKE 'E'.
*    LEAVE TO CURRENT TRANSACTION.
    RETURN.
  ENDIF.


  " Namespace based selection
  IF r2 = 'X'.
    IF int_ns IS INITIAL OR s_date IS INITIAL OR  e_date IS INITIAL.
      MESSAGE 'Either Interface name or start date or end date is missing!' TYPE 'I' DISPLAY LIKE 'E'.
      RETURN.
    ENDIF.
    CONVERT DATE s_date TIME s_time
      INTO TIME STAMP s_timestamp TIME ZONE sy-zonlo.


    CONVERT DATE e_date TIME e_time
        INTO TIME STAMP e_timestamp TIME ZONE sy-zonlo.


    MOVE s_timestamp TO range-low.
    MOVE e_timestamp TO range-high.
    MOVE 'I' TO range-sign.
    MOVE 'BT' TO range-option.
    APPEND range.


    SELECT sxmspmast~msgguid sxmspmast~pid sxmspmast~rest_vers
    FROM ( sxmspemas
          INNER JOIN sxmspmast
          ON  sxmspmast~msgguid = sxmspemas~msgguid
          AND sxmspmast~pid = sxmspemas~pid ) INTO TABLE it_idtab
        WHERE sxmspemas~ib_name EQ int_ns
          AND sxmspmast~inittimest IN range.
  ENDIF.


  " XML ID based selection
  IF r1 = 'X'.


*    "Storing all the message IDs in an internal table
*    LOOP AT s_msg_id.
*      MOVE s_msg_id-low TO lw_msgid.
*      APPEND lw_msgid TO lt_msgid.
*    ENDLOOP.
    IF s_msg_id IS NOT INITIAL.
      SELECT msgguid pid rest_vers FROM sxmspmast
                                  INTO TABLE it_idtab
                                  WHERE msgguid IN s_msg_id.
    ELSE.
      MESSAGE 'Provide XML IDs' TYPE 'I' DISPLAY LIKE 'E'.
*      LEAVE TO CURRENT TRANSACTION.
      RETURN.
    ENDIF.


  ENDIF.


  "Loop to process all the message IDs


  IF it_idtab IS NOT INITIAL.
    LOOP AT it_idtab INTO wa_idtab.
      id_index = sy-tabix.
      TRY.


          CREATE OBJECT persist_main.


          CALL METHOD persist_main->read_msg_all
            EXPORTING
              im_msgguid = wa_idtab-msgguid
              im_pid    = wa_idtab-pid
              im_version = wa_idtab-rest_vers
            IMPORTING
              ex_message = xms_msg_main.


          xmb_msg_main ?= xms_msg_main.


          CALL METHOD xmb_msg_main->get_main_payloads
            RECEIVING
              return = payload.


          IF payload IS NOT INITIAL.
            READ TABLE payload INTO wa_payload INDEX 1.


            "Fetched the payload of XML ID
            CALL METHOD wa_payload-mainpayload->gettextcontent
              RECEIVING
                return = wa_str.


            data : temp_input TYPE ZCL_MT_IDDOC_PROCS_UPDATE.
            data : target TYPE string.
*
            data : object TYPE REF TO ZCL_CL_SI_CREATE_IDDOCUMENT_PR.
* Hear...the token is extracted using simple string Operations One can very well use XSLT *transformations.


            CONDENSE wa_str.
            TRANSLATE wa_str TO UPPER CASE.
            TRANSLATE p_token1 TO UPPER CASE.
            "Break the string from initial token. Eg. <Customer>
            REFRESH lt_str.
            SPLIT wa_str AT p_token1 INTO TABLE lt_str.


            "Building the end token
            CLEAR l_token.
            MOVE p_token1 TO l_token.
            REPLACE FIRST OCCURRENCE OF '<' IN l_token WITH '</'.


            IF lt_str IS NOT INITIAL.
              LOOP AT lt_str INTO wa_str FROM 2.
                CLEAR: token, str5, l_index.
                l_index = sy-tabix.
                "Splitting the string at end token to get the token value
                SPLIT wa_str AT l_token INTO token str5.


                wa_final-value  = token.


                temp_index = id_index MOD 2.
                IF temp_index EQ 0.
                  ls_cellcolor-fname = 'XML_ID'.
                  ls_cellcolor-color-col = 3.
                  APPEND ls_cellcolor TO wa_final-cellcolor.


                  ls_cellcolor-fname = 'VALUE'.
                  ls_cellcolor-color-col = 3.
                  APPEND ls_cellcolor TO wa_final-cellcolor.
                ELSE.
                  ls_cellcolor-fname = 'XML_ID'.
                  ls_cellcolor-color-col = 6.
                  APPEND ls_cellcolor TO wa_final-cellcolor.


                  ls_cellcolor-fname = 'VALUE'.
                  ls_cellcolor-color-col = 6.
                  APPEND ls_cellcolor TO wa_final-cellcolor.
                ENDIF.
                wa_final-xml_id = wa_idtab-msgguid.


*        CONCATENATE '*' p_value '*' INTO lv_string.
                IF  p_value IS NOT INITIAL
                AND token CP p_value.
                  APPEND wa_final TO lt_final.
                ELSEIF p_value IS INITIAL.
                  APPEND wa_final TO lt_final.
                ENDIF.


                CLEAR wa_final.
              ENDLOOP.
            ENDIF.
          ELSE.
            wa_final-xml_id = wa_idtab-msgguid.
            wa_final-value = 'Unable to retrieve payload'.
            APPEND wa_final TO lt_final.
            CLEAR wa_final.
          ENDIF.


          CLEAR wa_idtab.
          CLEAR wa_str.
        CATCH zcx_root.
        CATCH cx_xms_syserr_persist.
          wa_final-xml_id = wa_idtab-msgguid.
          wa_final-value = 'Unable to retrieve message'.
          APPEND wa_final TO lt_final.
          CLEAR wa_final.
      ENDTRY.
    ENDLOOP.
  ELSE.
    MESSAGE 'No records selected!' TYPE 'I' DISPLAY LIKE 'E'.
*    LEAVE TO CURRENT TRANSACTION.
    RETURN.
  ENDIF.
  IF lt_final IS NOT INITIAL.
    DESCRIBE TABLE it_idtab LINES lv_dbcnt. " Holds value of total records.
    DESCRIBE TABLE lt_final LINES lv_final. " Records matching the criteria
    PERFORM build_fieldcat USING '1' 'XML_ID' text-002.


    PERFORM build_fieldcat USING '2' 'VALUE'  p_token1.


    x_layout-coltab_fieldname = 'CELLCOLOR'.


    PERFORM display_output.
  ELSE.
    MESSAGE 'No tokens extracted' TYPE 'I' DISPLAY LIKE 'E'.
*    LEAVE TO CURRENT TRANSACTION.
    RETURN.
  ENDIF.



*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCAT
*&---------------------------------------------------------------------*
*      text
*----------------------------------------------------------------------*
*      -->P_0294  text
*      -->P_0295  text
*      -->P_TEXT_002  text
*----------------------------------------------------------------------*
FORM build_fieldcat  USING    value(col_pos) TYPE any
                              value(fnam) TYPE any
                              text    TYPE any.


  wa_fieldcat-col_pos  = col_pos.
  wa_fieldcat-fieldname = fnam.
  wa_fieldcat-tabname  = c_tab.
  wa_fieldcat-seltext_l = text.
  wa_fieldcat-no_zero = 'X'.
  wa_fieldcat-outputlen = 30.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR  wa_fieldcat.


ENDFORM.                    " BUILD_FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_OUTPUT
*&---------------------------------------------------------------------*
*      text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_output .


  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program          = sy-repid
      I_CALLBACK_USER_COMMAND    = 'USER_COMMAND'
      i_callback_html_top_of_page = 'HTML_TOP_OF_PAGE'
      i_grid_title                = ws_title
      is_layout                  = x_layout
      it_fieldcat                = it_fieldcat[]
    TABLES
      t_outtab                    = lt_final
    EXCEPTIONS
      program_error              = 1
      OTHERS                      = 2.


  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.


ENDFORM.                    " DISPLAY_OUTPUT
*&---------------------------------------------------------------------*
*&      Form  HTML_TOP_OF_PAGE
*&---------------------------------------------------------------------*
*      text
*----------------------------------------------------------------------*
*      -->DOCUMENT  text
*----------------------------------------------------------------------*
FORM html_top_of_page USING document TYPE REF TO cl_dd_document.


  DATA : lv_string TYPE string.
  DATA : lc_char TYPE char20.
  DATA : i_dtfrm(10) TYPE c,
        i_dtto(10)  TYPE c.
  CALL METHOD document->add_gap
    EXPORTING
      width = 15.


  CLEAR lv_string.


  IF r2 IS NOT INITIAL.
    CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
      EXPORTING
        date_internal            = s_date
      IMPORTING
        date_external            = i_dtfrm
      EXCEPTIONS
        date_internal_is_invalid = 1
        OTHERS                  = 2.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.


    CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
      EXPORTING
        date_internal            = e_date
      IMPORTING
        date_external            = i_dtto
      EXCEPTIONS
        date_internal_is_invalid = 1
        OTHERS                  = 2.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
if int_ns is NOT INITIAL.
    CONCATENATE text-012 space i_dtfrm 'to' i_dtto 'for interface' int_ns  INTO lv_string SEPARATED BY space.
  ELSE.
    CONCATENATE text-012 space i_dtfrm 'to' i_dtto  INTO lv_string SEPARATED BY space.
    endif.


    MOVE lv_string TO text1.
    CALL METHOD document->add_text
      EXPORTING
        text        = text1
        sap_fontsize = cl_dd_document=>large
        sap_emphasis = text-011.


    CALL METHOD document->new_line
      EXPORTING
        repeat = '1'.
  ENDIF.


  CLEAR: lc_char, lv_string.
  MOVE lv_dbcnt TO lc_char.


  CONCATENATE text-010 ': ' lc_char INTO lv_string.


  CALL METHOD document->add_gap
    EXPORTING
      width = 15.


  MOVE lv_string TO text1.
*  text1 =  text-010.
  CALL METHOD document->add_text
    EXPORTING
      text        = text1
      sap_fontsize = cl_dd_document=>large
      sap_emphasis = text-011.


    CALL METHOD document->new_line.


    CLEAR: lc_char, lv_string.
    MOVE lv_final TO lc_char.


    CONCATENATE text-013 ': ' lc_char INTO lv_string.


    CALL METHOD document->add_gap
      EXPORTING
        width = 15.


    MOVE lv_string TO text1.


    CALL METHOD document->add_text
      EXPORTING
        text        = text1
        sap_fontsize = cl_dd_document=>large
        sap_emphasis = text-011.



ENDFORM.                    "HTML_TOP_OF_PAGE


FORM USER_COMMAND  USING r_ucomm LIKE sy-ucomm
                                  rs_selfield TYPE slis_selfield.


  READ TABLE lt_final INTO wa_final INDEX rs_selfield-tabindex.
  if sy-subrc eq 0.


  SUBMIT  RSXMB_DISPLAY_MSG_VERS_NEW  WITH MSGGUID = wa_final-xml_id
    AND RETURN.


  ENDIF.
ENDFORM.



*{/Code}


*Text elements
*----------------------------------------------------------
* 001 Select the criteria for search
* 002 XML ID
* 003 Input XMLIds
* 004 Input Namespace and date range
* 005 Input Token To look for
* 006 Start Date:
* 007 End Date:
* 008 Start Time:
* 009 End Time:
* 010 Total records found
* 011 Strong
* 012 Data records from:
* 013 Total Tokens found



*Selection texts
*----------------------------------------------------------
* E_DATE        End Date
* E_TIME        End Time
* INT_NS        Interface namespace
* P_TOKEN1        Token to look for
* P_VALUE        Value to be compared
* R1        XMLbased
* R2        Namespace and date based
* S_DATE        Start Date
* S_MSG_ID        Message ID
* S_TIME        Start Time



*Messages
*----------------------------------------------------------
*
* Message class: Hard coded
*  Provide a Token Value



3 Comments