[ABAP] XML-Daten in eine Datei (*.xml) schreiben

Variante 1 (lokal über XML-String)

* ixml Factory
  DATA(o_ixml) = cl_ixml=>create( ).

* Encoding UTF-8
  DATA(o_encoding) = o_ixml->create_encoding( character_set = 'UTF-8'
                                              byte_order = if_ixml_encoding=>co_none ).

* DOM Object Model
  DATA(o_doc) = o_ixml->create_document( ).

  ...

* XML-Dokument rendern
  DATA(o_sf) = o_ixml->create_stream_factory( ).

* XML-String
  DATA: lv_xml TYPE string.

* Stream für Ausgabe in XML-String
  DATA(o_stream) = o_sf->create_ostream_cstring( lv_xml ).
  o_stream->set_encoding( encoding = o_encoding ).
  o_stream->set_pretty_print( pretty_print = abap_true ).

* Render-Objekt
  DATA(o_render) = o_ixml->create_renderer( ostream  = o_stream
                                            document = o_doc ).

* XML-String in lv_xml generieren
  DATA(lv_rc) = o_render->render( ).

* Dateigröße in Bytes
  DATA(lv_size) = o_stream->get_num_written_raw( ).
  
* Stream schließen
  o_stream->close( ).

* Wenn alles ok
  IF lv_rc = 0 AND lv_size > 0.
  * XML-String in XML-Document wandeln
    DATA(o_xml_doc) = NEW cl_xml_document( ).
    o_xml_doc->parse_string( lv_xml ).

  * XML-Document als Datei speichern
    o_xml_doc->export_to_file( 'c:\temp\test.xml' ).
  ENDIF.

Variante 2 (lokal über XML-iTab)

* ixml Factory
  DATA(o_ixml) = cl_ixml=>create( ).

* Encoding UTF-8
  DATA(o_encoding) = o_ixml->create_encoding( character_set = 'UTF-8'
                                              byte_order = if_ixml_encoding=>co_none ).

* DOM Object Model
  DATA(o_doc) = o_ixml->create_document( ).

  ...

* XML-Dokument rendern
  DATA(o_sf) = o_ixml->create_stream_factory( ).

* XML-iTab
  DATA: it_xml_out TYPE STANDARD TABLE OF char255 WITH DEFAULT KEY.

* Encoding UTF-8
  DATA(o_encoding) = o_ixml->create_encoding( character_set = 'UTF-8'
                                              byte_order = if_ixml_encoding=>co_none ).

* Stream für Ausgabe in XML-iTab
  DATA(o_stream) = o_sf->create_ostream_itable( table = it_xml_out ).
  o_stream->set_encoding( encoding = o_encoding ).

* Render-Objekt
  DATA(o_render) = o_ixml->create_renderer( ostream  = o_stream
                                            document = o_doc ).

  o_render->set_normalizing( is_normalizing = space ).

* XML-String in it_xml_out generieren
  DATA(lv_rc) = o_render->render( ).

* Dateigröße in Bytes
  DATA(lv_size) = o_stream->get_num_written_raw( ).
  
* Stream schließen
  o_stream->close( ).

* Wenn alles ok
  IF lv_rc = 0 AND lv_size > 0.
* Tabelle als Binär-Daten herunterladen
    cl_gui_frontend_services=>gui_download( EXPORTING
                                              filename = 'c:\temp\test.xml'
                                              filetype = 'BIN'
                                              bin_filesize = lv_size
                                            CHANGING
                                              data_tab = it_xml_out ).
  ENDIF.

Variante 3 (Applikationsserver)

* ixml Factory
  DATA(o_ixml) = cl_ixml=>create( ).

* DOM Object Model
  DATA(o_doc) = o_ixml->create_document( ).
  
  ...
  
* XML-Dokument rendern
  DATA(o_sf) = o_ixml->create_stream_factory( ).

* Encoding UTF-8
  DATA(o_encoding) = o_ixml->create_encoding( character_set = 'UTF-8'
                                              byte_order = if_ixml_encoding=>co_none ).

* Stream für Ausgabe in Datei '/tmp/test/test.xml' auf dem Applikationsserver
  DATA(o_stream) = o_sf->create_ostream_uri( system_id = '/tmp/test/test.xml' ).
  o_stream->set_encoding( encoding = o_encoding ).
  o_stream->set_pretty_print( pretty_print = abap_true ).

* Render-Objekt
  DATA(o_render) = o_ixml->create_renderer( ostream  = o_stream
                                            document = o_doc ).

* XML-String in Datei auf Applikationsserver generieren
  DATA(lv_rc) = o_render->render( ).

* Dateigröße in Bytes
  DATA(lv_size) = o_stream->get_num_written_raw( ).
  
* Stream schließen
  o_stream->close( ).

* Wenn alles ok
  IF lv_rc = 0 AND lv_size > 0.
    ...
  ENDIF.