Skip to end of metadata
Go to start of metadata

ABAP data can be converted into XML using different ways. Using iXML Library,  XSLT Transformations or some generic Function Modules ( SAP_CONVERT_TO_XML_FORMAT ). A sample usage of iXML library is described here. This object oriented iXML library provides more flexibility for an ABAP developer to generate custom XML formats. This will be useful to generate XML with custom element names.

SAP iXML Library

The iXML library is an implementation of three core XML services:

  1. XML Parser
  2. XML DOM
  3. XML Renderer

Following example uses XML DOM and Renderer. XML Parser can be used for reading XML data into ABAP. This example creates an XML DOM structure from scratch and builds the XML structure element by element. Renderer is used to generate the comple XML file in Application server using the completed XML DOM structure. Otherwise output can be written to a string or table using interface if_ixml_ostream.

Sample XML

<?xml version="1.0"?>
  <Employee Type="FT">

Sample Code

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
*& Report  YV_IXML_WRITE



  " Appserver Windows file. Not from Desktop (presentation server)
  p_fname TYPE string DEFAULT 'I:\USR\SAP\PUT\FILE.XML'. 

  " Appserver UNIX file
  " p_fname TYPE string DEFAULT '/usr/SAP/PUT/FILE.XML'.  

DATA: lo_ixml TYPE REF TO if_ixml.

* Create iXML object
lo_ixml = cl_ixml=>create( ).

***  Build iXML DOM  *************************************************
* Sample XML contents.
*  <?xml version="1.0"?>
*  <Company>
*   <CompanyName>SAP</CompanyName>
*   <Employee Type="FT">
*    <EmployeeName>James</EmployeeName>
*    <EmployeeNumber>007</EmployeeNumber>
*   </Employee>
*  </Company>

DATA: lo_document TYPE REF TO if_ixml_document.
DATA: lo_company  TYPE REF TO if_ixml_element.
DATA: lo_employee TYPE REF TO if_ixml_element.

lo_document = lo_ixml->create_document( ).

lo_company  = lo_document->create_simple_element(
                                    name    = 'Company'
                                    parent  = lo_document ).

lo_document->create_simple_element( name    = 'CompanyName'
                                    parent  = lo_company
                                    value   = 'SAP' ).

lo_employee = lo_document->create_simple_element(
                                    name    = 'Employee'
                                    parent  = lo_company ).

lo_employee->set_attribute(           name  = 'Type'
                                      value = 'FT' ).

lo_document->create_simple_element( name    = 'EmployeeName'
                                    parent  = lo_employee
                                    value   = 'James' ).

lo_document->create_simple_element( name    = 'EmployeeNumber'
                                    parent  = lo_employee
                                    value   = '007' ).

*** Create Output Stream and Render **********************************

      lo_streamfactory  TYPE REF TO if_ixml_stream_factory,
      lo_ostream        TYPE REF TO if_ixml_ostream,
      lo_renderer       TYPE REF TO if_ixml_renderer,
      lv_rc             TYPE i,
      lv_xml_size       TYPE i.

* Create Stream Factory
lo_streamfactory = lo_ixml->create_stream_factory( ).

* Crate Output Stream
lo_ostream  = lo_streamfactory->create_ostream_uri( system_id = p_fname ).

* Craete renderer
lo_renderer = lo_ixml->create_renderer( ostream  = lo_ostream
                                        document = lo_document ).
* Set Pretty Print
lo_ostream->set_pretty_print( 'X' ).

* Render
lv_rc = lo_renderer->render( ).

* Get XML file size
lv_xml_size = lo_ostream->get_num_written_raw( ).

* Display Output
WRITE : 'XML File: ', p_fname, lv_xml_size,  'Bytes'.

Execute Program


Transaction AL11

New file.xml