Conteúdo

Gerar arquivo XML

Chegou a hora de trabalhar com interfaces, legados e tudo mais, e você precisa gerar um XML de sua aplicação. Eis um exemplo facinho para se tomar como base.
REPORT ZSAPeiros.

DATA: it_table              LIKE t001 OCCURS 0.

DATA: l_dom                 TYPE REF TO if_ixml_element,
      m_document            TYPE REF TO if_ixml_document,
      m_xmldoc              TYPE REF TO cl_xml_document,
      g_ixml                TYPE REF TO if_ixml,
      w_result              TYPE i,
      w_rc                  LIKE sy-subrc.


START-OF-SELECTION.
  SELECT * FROM t001 INTO TABLE it_table.


END-OF-SELECTION.
* Inicializa o iXML-Framework
  CLASS cl_ixml DEFINITION LOAD.
  g_ixml = cl_ixml=>create( ).
  CHECK NOT g_ixml IS INITIAL.


* Cria o DOM com dados do SAP
  m_document = g_ixml->create_document( ).
  CHECK NOT m_document IS INITIAL.

* Conversão da tabela interna em DOM
  CALL FUNCTION 'SDIXML_DATA_TO_DOM'
    EXPORTING
      name         = 'IT_TABLE'
      dataobject   = it_table[]
    IMPORTING
      data_as_dom  = l_dom
    CHANGING
      document     = m_document
    EXCEPTIONS
      illegal_name = 1
      OTHERS       = 2.

  IF NOT sy-subrc = 0.
    WRITE: /'Erro DADOS -> DOM: ', sy-subrc.
  ENDIF.

  CHECK NOT l_dom IS INITIAL.


* Apendando o DOM ao documento iXML
  w_rc = m_document->append_child( new_child = l_dom ).

  IF NOT w_rc IS INITIAL.
    WRITE: /'Erro DOM -> iXML: ', w_rc.
  ENDIF.


* Criar XML apartir do DOM no iXML
  CREATE OBJECT m_xmldoc.
  CHECK NOT m_xmldoc IS INITIAL.

  w_rc = m_xmldoc->create_with_dom( document = m_document ).
  IF NOT w_rc IS INITIAL.
    WRITE: /'Erro DOM+iXML -> XML: ', w_rc.
  ENDIF.


* Mostrando o arquivo XML
  CALL METHOD m_xmldoc->display.


* Salvar o arquivo XML
  CALL METHOD m_xmldoc->export_to_file( filename = 'c:\teste.xml' ).