Monday, 24 August 2015

Creating XML Tree structure in ABAP!

-------------------------------------------------------------------------------------------------


DATA lr_ixml TYPE REF TO if_ixml,
              lr_doc  TYPE REF TO if_ixml_document,
              lr_root_elem  TYPE REF TO if_ixml_element,
              lr_node1_elem  TYPE REF TO if_ixml_element,
              lr_stream TYPE REF TO if_ixml_stream_factory,
              lr_ostream TYPE REF TO if_ixml_ostream,
              lr_render TYPE REF TO if_ixml_renderer,
              xml_string TYPE string.


lr_ixml cl_ixml=>create).
* Get reference of the document
lr_doc lr_ixml->create_document).
* get reference of the root
lr_root_elem lr_doc->create_simple_element_nsprefix 'asx'
                                                 name 'test'
                                                 parent lr_doc ).
* Add root element attributes
lr_root_elem->set_attribute_nsname =  'asx'
                                prefix 'xmlns'
                                value 'http://www.sap.com/testxml' ).
lr_root_elem->set_attribute_nsname =  'version'
                                value '1.0' ).

* Get reference of the first node of teh root
lr_node1_elem  lr_doc->create_simple_element_nsprefix 'asx'
                                                   name 'names'
                                                   parent lr_root_elem ).

* add node 1 to the document
lr_doc->create_simple_element_nsname 'name'
                                  value 'SAP'
                                  parent lr_node1_elem  ).

lr_stream lr_ixml->create_stream_factory).

lr_ostream lr_stream->create_ostream_cstringxml_string ).
lr_render lr_ixml->create_rendererdocument lr_doc
                                      ostream  lr_ostream ).

lr_render->render).

cl_abap_browser=>show_xml(
  EXPORTING
    xml_string  =  xml_string
    title       =  'Test XML'
    size        cl_abap_browser=>medium ).    " Size (S,M.L,XL)

-------------------------------------------------------------------------------------------------



-------------------------------------------------------------------------------------------------

No comments:

Comments system

Disqus Shortname