25/02/2010

Gerando XML Manualmente

Necessidade : Criar Manualmente um XML apartir de uma tabela interna.

Solução : Vamos criar o XML utilizando as interfaces: if_ixml,if_ixml_document,if_ixml_node,if_ixml_element,if_ixml_element,if_ixml_cdata_section e as classe cl_ixml.

Veja Abaixo:


REPORT ZRR_SAMPLE.

*-----------------------------------------------------------------------
* | V.A.R.I.A.B.L.E.S D.E.C.L.A.R.A.T.I.O.N.S.
*-----------------------------------------------------------------------

DATA: ixml TYPE REF TO if_ixml,
document TYPE REF TO if_ixml_document,
parent TYPE REF TO if_ixml_node,
element TYPE REF TO if_ixml_element,
element1 TYPE REF TO if_ixml_element,
cdata TYPE REF TO if_ixml_cdata_section,
xmlfile TYPE dcxmllines,
size TYPE i.

*-----------------------------------------------------------------------
* | C.U.S.T.O.M T.Y.P.E.S D.E.C.L.A.R.A.T.I.O.N.S.
*-----------------------------------------------------------------------

TYPES : BEGIN OF ty_contatos_line,
nome TYPE string,
sobrenome TYPE string,
idade TYPE string,
sexo TYPE string,
END OF ty_contatos_line.

*-----------------------------------------------------------------------
* | C.U.S.T.O.M T.A.B.L.E.S T.Y.P.E.S D.E.C.L.A.R.A.T.I.O.N.S.
*-----------------------------------------------------------------------

TYPES : ty_contatos_tab TYPE STANDARD TABLE OF ty_contatos_line.

*-----------------------------------------------------------------------
* | I.N.T.E.R.N.A.L T.A.B.L.E.S D.E.C.L.A.R.A.T.I.O.N.S.
*-----------------------------------------------------------------------

DATA : it_contatos TYPE ty_contatos_tab.

*-----------------------------------------------------------------------
* | C.U.S.T.O.M S.T.R.U.C.T.U.R.E.S D.E.C.L.A.R.A.T.I.O.N.S.
*-----------------------------------------------------------------------

DATA : st_contatos LIKE LINE OF it_contatos.

*-----------------------------------------------------------------------
* | S.C.R.E.E.N E.L.E.M.E.N.T.S D.E.C.L.A.R.A.T.I.O.N.S.
*-----------------------------------------------------------------------

SELECTION-SCREEN BEGIN OF BLOCK bl01 WITH FRAME TITLE text-001.

SELECTION-SCREEN SKIP 1.

PARAMETER : p_disp RADIOBUTTON GROUP gp01, " DISPLAY XML
p_save RADIOBUTTON GROUP gp01. " SAVE XML

SELECTION-SCREEN END OF BLOCK bl01.

*-----------------------------------------------------------------------
* | S.T.A.R.T O.F S.E.L.E.C.T.I.O.N
*-----------------------------------------------------------------------

START-OF-SELECTION.

PERFORM f_bind_data_into_table.

PERFORM f_genarate_xml.

*-----------------------------------------------------------------------
* |-> Form : F_BIND_DATA_INTO_TABLE
*-----------------------------------------------------------------------
* |-> Text : LOAD VALUES INTO INTERNAL TABLE
*-----------------------------------------------------------------------
FORM f_bind_data_into_table .

CLEAR : it_contatos,
st_contatos.

st_contatos-nome = 'RAFAEL'.
st_contatos-sobrenome = 'RISO'.
st_contatos-idade = '24'.
st_contatos-sexo = 'M'.

INSERT st_contatos INTO TABLE it_contatos.

st_contatos-nome = 'MULHER'.
st_contatos-sobrenome = 'MORANGO'.
st_contatos-idade = '26'.
st_contatos-sexo = 'F'.

INSERT st_contatos INTO TABLE it_contatos.

st_contatos-nome = 'DARTH'.
st_contatos-sobrenome = 'VADER'.
st_contatos-idade = '87'.
st_contatos-sexo = 'M'.

INSERT st_contatos INTO TABLE it_contatos.

ENDFORM. " F_BIND_DATA_INTO_TABLE

*-----------------------------------------------------------------------
* |-> Form : F_GENARATE_XML
*-----------------------------------------------------------------------
* |-> Text : GENERATE XML FROM TABLE DATA
*-----------------------------------------------------------------------
FORM f_genarate_xml .

CLEAR : st_contatos.

* |-> CREATE iXML INSTANCE WITH SINGLETON
ixml = cl_ixml=>create( ).

* |-> CREATE iXML DOCUMENT
document = ixml->create_document( ).

* |-> CREATE A ROOT NODE IN DOCUMENT
element = document->create_element( name = 'CONTATOS' ).

* |-> APPEND NODE TO DOCUMENT
document->append_child( new_child = element ).

parent = element.

element1 = element.

LOOP AT it_contatos INTO st_contatos.

parent = element1.

* |-> CREATE A CHILD NODE IN DOCUMENT
element = document->create_element( name = 'CONTATO' ).

* |-> CREATE A CHILD NODE IN DOCUMENT
parent->append_child( new_child = element ).

parent = element.

* |-> CREATE A CHILD NODE IN DOCUMENT
element = document->create_element( name = 'NOME' ).

* |-> CREATE A CHILD NODE IN DOCUMENT
parent->append_child( new_child = element ).

* |-> SET VALUE TO CURRENT ELEMENT
element->set_value( value = st_contatos-nome ).

* |-> CREATE A CHILD NODE IN DOCUMENT
element = document->create_element( name = 'SOBRENOME' ).

* |-> CREATE A CHILD NODE IN DOCUMENT
parent->append_child( new_child = element ).

* |-> SET VALUE TO CURRENT ELEMENT
element->set_value( value = st_contatos-sobrenome ).

element = document->create_element( name = 'INFORMACOES' ).

* |-> CREATE A CHILD NODE IN DOCUMENT
parent->append_child( new_child = element ).

* |-> SET VALUE TO CURRENT ELEMENT
element->set_value( value = 'OUTRAS INFORMAÇÕES' ).

* |-> SET ATRIBUTE TO CURRENT ELEMENT
element->set_attribute( name = 'IDADE' value = st_contatos-idade ).

* |-> SET ATRIBUTE TO CURRENT ELEMENT
element->set_attribute( name = 'SEXO' value = st_contatos-sexo ).

* |-> CLEAR VALUES IN STRUCTURE
CLEAR st_contatos.

ENDLOOP.

IF p_disp EQ 'X'.

* |-> DISPLAY XML DOCUMENT
CALL FUNCTION 'SDIXML_DOM_TO_SCREEN'
EXPORTING
document = document
title = 'order'.

ELSE.

* |-> SAVE XML DOCUMENT IN DRIVE C
CALL FUNCTION 'SDIXML_DOM_TO_XML'
EXPORTING
document = document
pretty_print = 'X'
IMPORTING
size = size
TABLES
xml_as_table = xmlfile.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
bin_filesize = size
filename = 'C:\RR_XML_SAMPLE.xml'
filetype = 'BIN'
TABLES
data_tab = xmlfile
EXCEPTIONS
OTHERS = 1.

ENDIF.

ENDFORM. " F_GENARATE_XML


Tela:



XML:





RAFAEL
RISO
OUTRAS INFORMAÇÕES


MULHER
MORANGO
OUTRAS INFORMAÇÕES


DARTH
VADER
OUTRAS INFORMAÇÕES


Nenhum comentário:

Postar um comentário