[ABAP] IDOC als XML anzeigen

* IDoc-Nummer
PARAMETERS: p_idoc TYPE edidc-docnum MATCHCODE OBJECT edi_docnum DEFAULT '12345'.

START-OF-SELECTION.

  DATA: lv_action TYPE sy-ucomm.

* Anzeigen eines IDocs in XML-Format mit XSL
  CALL FUNCTION 'IDOC_XML_TRANSFORM'
    EXPORTING
      docnum                        = p_idoc
*     actions                       =
*     styleid                       = 'IE5' " default IE5, siehe auch Tabelle IDOCSTYLE
    IMPORTING
      action                        = lv_action
    EXCEPTIONS
      no_idoc_xml_loaded            = 1
      no_idoc_xml_display_available = 2
      error_loading_object          = 3
      OTHERS                        = 4.

  IF sy-subrc NE 0.
    WRITE: / 'Fehler:', sy-subrc.
  ELSE.
    WRITE: / 'Action:', lv_action.
  ENDIF.

[ABAP] XML-Editor in der SAPGUI anzeigen, XML-Daten editieren

DATA(it_xml) = VALUE string_table( ( |<?xml version="1.0" encoding="utf-8"?>| )
                                   ( |<asx:abap version="1.0" xmlns:asx="http://www.sap.com/abapxml">| )
                                   ( |<asx:values>| )
                                   ( |<TEXT>Hello world!</TEXT>| )
                                   ( |</asx:values>| )
                                   ( |</asx:abap>| )
                                 ).

* XML Viewer and Editor
* XML-Editor im aktuellen Modus im SAPGUI anzeigen
DATA(o_xml) = NEW cl_proxy_xml_edit( cl_gui_container=>default_screen ).

* XML in Editor übergeben
o_xml->set_text( it_xml ).
* Editmode einschalten
o_xml->set_change_mode( abap_true ).
* XML formatieren
o_xml->pretty_print( ).
* Editor visible setzen
o_xml->set_visible( abap_true ).

* Ausgabe auf cl_gui_container=>default_screen erzwingen
WRITE: space.

[ABAP] MS-Excel-Datei mit Hilfe der Klasse cl_ehfnd_xlsx lesen

TRY.
    DATA: lv_rc TYPE i.
    DATA: it_files TYPE filetable.
    DATA: lv_action TYPE i.

* FileOpen-Dialog aufrufen
    cl_gui_frontend_services=>file_open_dialog( EXPORTING file_filter = |xlsx (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
                                                CHANGING  file_table  = it_files
                                                          rc          = lv_rc
                                                          user_action = lv_action ).

    IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
      IF lines( it_files ) > 0.
* ersten Tabelleneintrag lesen
        DATA: lv_filesize TYPE w3param-cont_len.
        DATA: lv_filetype TYPE w3param-cont_type.
        DATA: it_bin_data TYPE w3mimetabtype.

* Excel-Datei auf Appl. Server hochladen (binary)
        cl_gui_frontend_services=>gui_upload( EXPORTING filename   = |{ it_files[ 1 ]-filename }|
                                                        filetype   = 'BIN'
                                              IMPORTING filelength = lv_filesize
                                              CHANGING  data_tab   = it_bin_data ).

* solix -> xstring
        DATA(lv_bin_data) = cl_bcs_convert=>solix_to_xstring( it_solix = it_bin_data ).

* XLSX Handling: Kapselt cl_xlsx_document
        DATA(o_excel) = cl_ehfnd_xlsx=>get_instance( ).

* XLSX Workbook (XString --> XML)
        DATA(o_doc) = o_excel->load_doc( iv_file_data = lv_bin_data ).

* XLSX Sheets des Workbooks holen
        DATA(it_sheets) = o_doc->get_sheets( ).

        LOOP AT it_sheets ASSIGNING FIELD-SYMBOL(<sheet>).
          WRITE: / <sheet>-name, <sheet>-sheet_id, <sheet>-rid.
        ENDLOOP.

        ULINE.

* XLSX Sheet: erste Sheet holen
        DATA(o_sheet) = o_doc->get_sheet_by_id( iv_sheet_id = 1 ).
* max. Zeilenzahl der Sheet
        DATA(lv_maxrow) = o_sheet->get_last_row_number( ).

        DATA(lv_row) = 1.

        DO lv_maxrow TIMES.
* max. Spaltenzahl der akt. Zeile
          DATA(lv_cols_in_row) = o_sheet->get_last_column_number_in_row( lv_row ).
          DATA(lv_col) = 1.

          DO lv_cols_in_row TIMES.
* Inhalt der akt. Zelle
            DATA(lv_value) = o_sheet->get_cell_content( EXPORTING iv_row    = lv_row
                                                                  iv_column = lv_col ).

            WRITE: lv_value.

            lv_col = lv_col + 1.
          ENDDO.

          lv_row = lv_row + 1.

          WRITE: /.
        ENDDO.

      ENDIF.
    ENDIF.

  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
ENDTRY.

[ABAP] Beispiel für Verwendung der Klasse cl_xlsx_document

* Workbook
PARAMETERS: p_wb RADIOBUTTON GROUP grp DEFAULT 'X'.
* Sharedstrings
PARAMETERS: p_str RADIOBUTTON GROUP grp.
* Sheetpart
PARAMETERS: p_sh RADIOBUTTON GROUP grp.
* Tablepart
PARAMETERS: p_tab RADIOBUTTON GROUP grp.

START-OF-SELECTION.

  TRY.
      DATA: lv_rc TYPE i.
      DATA: it_files TYPE filetable.
      DATA: lv_action TYPE i.

* FileOpen-Dialog aufrufen
      cl_gui_frontend_services=>file_open_dialog( EXPORTING file_filter = |xlsx (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
                                                  CHANGING  file_table  = it_files
                                                            rc          = lv_rc
                                                            user_action = lv_action ).

      IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
        IF lines( it_files ) > 0.
* ersten Tabelleneintrag lesen
          DATA: lv_filesize TYPE w3param-cont_len.
          DATA: lv_filetype TYPE w3param-cont_type.
          DATA: it_bin_data TYPE w3mimetabtype.

* Excel-Datei auf Appl. Server hochladen (binary)
          cl_gui_frontend_services=>gui_upload( EXPORTING filename   = |{ it_files[ 1 ]-filename }|
                                                          filetype   = 'BIN'
                                                IMPORTING filelength = lv_filesize
                                                CHANGING  data_tab   = it_bin_data ).

* solix -> xstring
          DATA(lv_bin_data) = cl_bcs_convert=>solix_to_xstring( it_solix = it_bin_data ).

**********************************************************************
* Excel-Objekt
**********************************************************************
          DATA(o_excel) = cl_xlsx_document=>load_document( lv_bin_data ).

          DATA(o_workbook) = o_excel->get_workbookpart( ).
          DATA(lv_workbook_xstr) = o_workbook->get_data( ).

          DATA(o_sharedstrings) = o_workbook->get_sharedstringspart( ).
          DATA(lv_sharedstrings_xstr) = o_sharedstrings->get_data( ).

          DATA(o_sheets) = o_workbook->get_worksheetparts( ).
          IF o_sheets->get_count( ) > 0.
            DATA(o_sheet) = CAST cl_xlsx_worksheetpart( o_sheets->get_part( 0 ) ).

            DATA(lv_sheet_xstr) = o_sheet->get_data( ).

            DATA(o_tables) = o_sheet->get_tableparts( ).
            IF o_tables->get_count( ) > 0.
              DATA(o_table) = CAST cl_xlsx_tablepart( o_tables->get_part( 0 ) ).
              DATA(lv_table_xstr) = o_table->get_data( ).
            ENDIF.
          ENDIF.

* XML Doc
          DATA(o_doc) = NEW cl_xml_document( ).

* xstring --> xml
          CASE abap_true.
            WHEN p_wb.
              o_doc->parse_xstring( lv_workbook_xstr ).
            WHEN p_str.
              o_doc->parse_xstring( lv_sharedstrings_xstr ).
            WHEN p_sh.
              o_doc->parse_xstring( lv_sheet_xstr ).
            WHEN p_tab.
              o_doc->parse_xstring( lv_table_xstr ).
          ENDCASE.

* Inhalt des XML-Documents in string wandeln
          o_doc->render_2_string( EXPORTING pretty_print = abap_true
                                  IMPORTING retcode      = DATA(lv_rc2)
                                            size         = DATA(lv_size2)
                                            stream       = DATA(lv_xml) ).

          IF lv_rc2 = 0.
* XML-String anzeigen
            cl_soap_xml_helper=>xml_show( sdoc  = lv_xml
                                          html  = abap_true
                                          title = 'XML Anzeige' ).
          ENDIF.

        ENDIF.
      ENDIF.

    CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
  ENDTRY.

[ABAP] XML-Daten anzeigen

* XML-String
DATA(lv_xml_string) = |<?xml version="1.0" encoding="utf-8"?><asx:abap version="1.0" xmlns:asx="http://www.sap.com/abapxml"><asx:values><TEXT>Hello world!</TEXT></asx:values></asx:abap>|.

* Display XML document
cl_soap_xml_helper=>xml_show(
                              sdoc = lv_xml_string  " XML as character string
*                              xdoc =               " XML as binary string
                              html = abap_false     " HTML or XML
*                              new_window =         " New SAPGUI
                              title = 'XML Anzeige' " DEFAULT 'Document display'(001)
                            ).

[ABAP] ABAP-Proxy-Objekt in XML wandeln und anzeigen

Variante 1 (ABAP_TO_XML_XSTRING)

TRY.
* Proxyobjekt
    DATA(lv_proxy_obj) = VALUE zgenerated_proxy_obj( p_par1 = 'VALUE1'
                                                     p_par2 = 'VALUE2'
                                                   ).

* Proxy-Objekt
    DATA(lv_datatype) = CONV prx_r3name( 'ZGENERATED_PROXY_OBJ' ).

* Simple Transformation mit Namespace
* xml_header
* no
* without_encoding
* full
    DATA(lv_xml) = cl_proxy_xml_transform=>abap_to_xml_xstring( abap_data  = lv_proxy_obj
                                                                ddic_type  = lv_datatype
                                                                xml_header = 'full' " no, without_encoding, full
                                                              ).

* Transformation
    CALL TRANSFORMATION id SOURCE XML lv_xml RESULT XML lv_xml.

* XML anzeigen
    cl_srtg_helper=>write_utf8_xmldoc( doc              = lv_xml
                                       use_html_control = abap_true " Anzeige im Browserfenster
                                     ).

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 2 (CL_SXML_STRING_WRITER)

TRY.
* Proxyobjekt
    DATA(lv_proxy_obj) = VALUE zgenerated_proxy_obj( p_par1 = 'VALUE1'
                                                     p_par2 = 'VALUE2'
                                                   ).

* Proxy-Objekt
    DATA(lv_datatype) = CONV prx_r3name( 'ZGENERATED_PROXY_OBJ' ).

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

* Simple Transformation mit Namespace
    cl_proxy_xml_transform=>abap_to_xml( abap_data  = lv_proxy_obj
                                         ddic_type  = lv_datatype
                                         xml_writer = o_xml_writer
                                       ).

    DATA(lv_xml) = o_xml_writer->get_output( ).

* Transformation
    CALL TRANSFORMATION id SOURCE XML lv_xml RESULT XML lv_xml.

* XML anzeigen
    cl_srtg_helper=>write_utf8_xmldoc( doc              = lv_xml
                                       use_html_control = abap_true " Anzeige im Browserfenster
                                     ).

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] XML-Datei einlesen und in String wandeln

TRY.
    DATA(o_doc) = NEW cl_xml_document( ).
* XML-Datei einlesen
    IF o_doc->import_from_file( 'c:\temp\temp.xml' ) = 0.
* Inhalt der XML-Document in String wandeln
      o_doc->render_2_string( EXPORTING
                                pretty_print = abap_true
                              IMPORTING
                                retcode = DATA(lv_rc)
                                size    = DATA(lv_size)
                                stream  = DATA(lv_xml_string) ).
      IF lv_rc = 0.
        WRITE: / lv_xml_string.
      ENDIF.
    ENDIF.
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] RegEx: Bestimmte Nodes (Submatches) in einem XML-String finden

* XML
DATA(lv_xml) = |<person>| &&
               |  <name>Udo</name>| &&
               |  <age>25</age>| &&
               |</person>| &&
               |<person>| &&
               |  <name>Ede</name>| &&
               |  <age>34</age>| &&
               |</person>| &&
               |<person>| &&
               |  <name />| &&
               |  <age>78</age>| &&
               |</person>|.

* Alle Nodes mit <name>...</name> finden
DATA(matcher) = cl_abap_matcher=>create( pattern     = '<name>([[:alnum:]]*)</name>'
                                         text        = lv_xml
                                         ignore_case = abap_true ).

* Alle Suchergebnisse ausgeben
WHILE abap_true = matcher->find_next( ).
  WRITE: / matcher->get_submatch( 1 ).
ENDWHILE.

[ABAP] Interne Tabelle als Excel-Datei (*.xlsx) speichern (cl_fdt_xl_spreadsheet)

* Mit Hilfe der Klasse cl_fdt_xl_spreadsheet können über XML-Transformationen
* Daten aus SAP in XLSX-Dateien geschrieben werden
TRY.
* Testdaten lesen
    SELECT * FROM t001 INTO TABLE @DATA(it_t001).

    IF sy-subrc = 0.
* Header erzeugen
      DATA: it_columns TYPE if_fdt_doc_spreadsheet=>t_column.
      DATA: lv_head TYPE t001.
      DATA(o_desc) = CAST cl_abap_structdescr( cl_abap_structdescr=>describe_by_data( lv_head ) ).

      LOOP AT o_desc->get_components( ) ASSIGNING FIELD-SYMBOL(<c>).
        IF <c> IS ASSIGNED.
          IF <c>-type->kind = cl_abap_typedescr=>kind_elem.
            APPEND VALUE #( id           = sy-tabix
                            name         = <c>-name
                            display_name = <c>-name
                            is_result    = abap_true
                            type         = <c>-type ) TO it_columns.
          ENDIF.
        ENDIF.
      ENDLOOP.

* itab + header -> XML -> xstring
* Achtung: Speicherintensiv und rel. langsam! Es sollten keine großen Datenmengen verarbeitet werden.
      DATA(lv_bin_data) = cl_fdt_xl_spreadsheet=>if_fdt_doc_spreadsheet~create_document( columns      = it_columns " optional
                                                                                         itab         = REF #( it_t001 )
                                                                                         iv_call_type = if_fdt_doc_spreadsheet=>gc_call_dec_table ).
      IF xstrlen( lv_bin_data ) > 0.
        DATA: lv_action TYPE i.
        DATA: lv_filename TYPE string.
        DATA: lv_fullpath TYPE string.
        DATA: lv_path TYPE string.

* Save-Dialog
        cl_gui_frontend_services=>file_save_dialog( EXPORTING
                                                      default_file_name = 'Excel.xlsx'
                                                      default_extension = 'xlsx'
                                                      file_filter       = |Excel-Datei (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
                                                    CHANGING
                                                      filename          = lv_filename
                                                      path              = lv_path
                                                      fullpath          = lv_fullpath
                                                      user_action       = lv_action ).

        IF lv_action EQ cl_gui_frontend_services=>action_ok.
* XSTRING -> SOLIX (RAW)
          DATA(it_raw_data) = cl_bcs_convert=>xstring_to_solix( EXPORTING iv_xstring = lv_bin_data ).

* Datei lokal speichern
          cl_gui_frontend_services=>gui_download( EXPORTING
                                                    filename     = lv_fullpath
                                                    filetype     = 'BIN'
                                                    bin_filesize = xstrlen( lv_bin_data )
                                                  CHANGING
                                                    data_tab     = it_raw_data ).


        ENDIF.
      ENDIF.
    ENDIF.
  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.