Skip to end of metadata
Go to start of metadata

Many times we need to exchange the data from ABAP to other non-ABAP development or form data for services to access(XI), In all such cases it becomes 

important to build an xml as per XSD of data. The below section explains formation of a sample xml. (Employee data).

Please note that the CALL TRANSFORMATION can also be used to create an xml from the internal table directly.

The process of making an xml object and then an xml from it is known as rendering. (Render data to xml)

Like the other languages in ABAP we have standard ixml interfaces to parse and process the xml, Steps are as follows:

Interface IF_IXML (this is a factory actually) : CL_ixml=>create() – will give me and instance of the Factory. This is the main factory, which will then give me instances of other factories

Then we need to create an XML document (Interface for this is IF_IXML_DOCUMENT) – Use the factory instance obtained above to create the document (……->CREATE_DOCUMENT)

This document is to be parsed. For this we  need:

  • Blank Document instance (obtained from the above factory)
  • oStream – The oStream is an output stream object which receives input type. Typically its an itab of some no. of chars.
  • Encoding object - the encoding type can be decided based on user's choice UTF-8 is recommended here. Once the encoding object is built, it should be set to output stream.
  • We have to create now a Renderer – if_ixml_renderer. The main factory itself can be used to create the renderer (…….….-> create_renderer : Here we have to pass Document, Stream Factory and the stream instances)

Once the renderer instance is ready, We can start creating brand new elements (using  create_element) and add these elements to either main doc or node of xml (user  append_child).

To point to a Node, we need an instance declaration of node type – IF_IXMl_NODE (Node is an element which has child)

To point to a Element, we need an instance declaration of element type – IF_IXMl_ELEMENT

-----------------------------------------------------------CODE STARTS FROM HERE-----------------------------------------------------------------------------

report zprepare_xml.

**--Data Declaration
types my_type type LENGTH 60.
data lif_ixml_stream_factory type ref to if_ixml_stream_factory.
data lt_string type table of my_type.
data ref_ixml type ref to if_ixml .
data ref_ixml_document type ref to if_ixml_document .
data ref_ixml_ostream type ref to if_ixml_ostream .
data ref_ixml_renderer type ref to if_ixml_renderer .
data ref_ixml_encoding type ref to if_ixml_encoding .
data ref_ixml_node_parent type ref to if_ixml_node .
data ref_ixml_element type ref to if_ixml_element .
constants mc_character_set type string value 'TEST*'.       "#EC NOTEXT
data lv_xml type string .       "#EC NOTEXT

**-- Create the Main Factory
ref_ixml cl_ixml=>create).

**-- Create the Initial Document
ref_ixml_document ref_ixml->create_document).

**-- Create an Output Stream
lif_ixml_stream_factory ref_ixml->create_stream_factory).
ref_ixml_ostream lif_ixml_stream_factory->create_ostream_itabletable lt_string ).

**-- Create an Encoding
ref_ixml_encoding ref_ixml->create_encoding(
    byte_order    0
    character_set mc_character_set

**-- Set the Encoding
ref_ixml_ostream->set_encodingencoding ref_ixml_encoding ).

**-- Create a Renderer
ref_ixml_renderer ref_ixml->create_renderer(
    document ref_ixml_document
    ostream  ref_ixml_ostream

**-- Create the Root Node
ref_ixml_element ref_ixml_document->create_element(
    name      'DATA'

**-- Append the Root element to the Document
ref_ixml_document->append_childnew_child ref_ixml_element ).

**-- Define a Parent Node
ref_ixml_node_parent ref_ixml_element.

**--Adding steps under data node
PERFORM add_step USING ref_ixml_document ref_ixml_node_parent 'Anurag' 'Bajaj' 'SD'.
PERFORM add_step USING ref_ixml_document ref_ixml_node_parent 'Cherry' 'Jonsan' 'HR'.
PERFORM add_step USING ref_ixml_document ref_ixml_node_parent 'Schott' 'Themes' 'FIN'.
PERFORM add_step USING ref_ixml_document ref_ixml_node_parent 'Veronica' 'Ivie' 'FIN'.
PERFORM add_step USING ref_ixml_document ref_ixml_node_parent 'Soumya' 'HK' 'SLS'.

**--Convering document to xml
PERFORM convert_dom_to_xml USING ref_ixml_document lv_xml.

**--Displaying xml
    xml_string   =  lv_xml   " XML in String


*&      Form  CONVERT_DOM_TO_XML
*       text
*  -->  p1        text
*  <--  p2        text
form convert_dom_to_xml USING i_ref_dom e_xml.
  data lv_xstring    type        xstring,
         convin        type ref to cl_abap_conv_in_ce..

  call function 'SDIXML_DOM_TO_XML'
          document      i_ref_dom
          xml_as_string lv_xstring
          no_document   1
          others        2.
      if sy-subrc <> 0.
**--Exception Handling


**--convert xstring to string
      call method cl_abap_conv_in_ce=>create
          encoding    'UTF-8'
          endian      'L'
          ignore_cerr 'X'
          replacement '#'
          input       lv_xstring              " Xstring
          conv        convin.

      call method convin->read
          data e_xml.                  " String

*&      Form  ADD_STEP
*       text
*      -->P_0135   text
*      -->P_0136   text
*      -->P_0137   text
form add_step  using  p_ref_ixml_document    type REF TO if_ixml_document
                      p_ref_ixml_node_parent type REF TO if_ixml_node
                      p_first_name p_last_name p_deparment.
**-- Create the 'STEP' Node
data lo_ref_ixml_element     type ref to if_ixml_element .
data lo_ref_ixml_element_sub type ref to if_ixml_element .

lo_ref_ixml_element p_ref_ixml_document->create_element(
    name      'EMPLOYEE'

p_ref_ixml_node_parent->append_childnew_child lo_ref_ixml_element ).

**Add First Name Element
lo_ref_ixml_element_sub p_ref_ixml_document->create_element(
    name      'FIRST_NAME'
lo_ref_ixml_element_sub->set_valuevalue p_first_name ).
lo_ref_ixml_element->append_childnew_child lo_ref_ixml_element_sub ).

**Add Last Name Element
lo_ref_ixml_element_sub p_ref_ixml_document->create_element(
    name      'LAST_NAME'
lo_ref_ixml_element_sub->set_valuevalue p_last_name ).
lo_ref_ixml_element->append_childnew_child lo_ref_ixml_element_sub ).

**Add Department Element
lo_ref_ixml_element_sub p_ref_ixml_document->create_element(
    name      'DEPARTMENT'
lo_ref_ixml_element_sub->set_valuevalue p_deparment ).
lo_ref_ixml_element->append_childnew_child lo_ref_ixml_element_sub ).


---------------------CODE ENDS HERE


  • No labels