Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
raeijpe
Contributor

Introduction

In this weblog I will focus on the possibilities of SAP XML Document Object Model (DOM) processing at the ABAP stack. My objective is to explain how you can use it in abap mapping. For this I split this weblog into three parts. This document is the second-in-the-series and will focus on the conversion from a XML file (back) to an ABAP table. This first part focused on the conversion of an ABAP table into an XML file and the last part will deal with the use of SAP DOM within XI ABAP mapping, introduced with SAP XI 3.0.

The XML

In this weblog we will focus on retrieving data from a local XML file and store it in an internal table.
We will start with an upload of a local XML file into a binary XML table and we will bind it to a XML input stream.


Next we will parse the stream to a XML DOM object using a using an iXML factory.


And finally we have a XML DOM Object and we can access its nodes to fill our table.


The implementation

In the implementation we will explain the steps of the process and at the end of the log you will find a complete source sample.
To upload the XML file, you need the filename and you have to declare an internal binary table to store the uploaded XML file.

  TYPES: t_xml_line(1024) TYPE x.
 
  DATA: l_filename        TYPE string,
        l_xml_table       TYPE TABLE OF t_xml_line,
        l_xml_line        TYPE t_xml_line,
        l_xml_table_size  TYPE i.
        

    CALL METHOD cl_gui_frontend_services=>gui_upload
      EXPORTING
        filename   = l_filename
        filetype   = 'BIN'
      IMPORTING
        filelength = l_xml_table_size
      CHANGING
        data_tab   = l_xml_table
      EXCEPTIONS
        OTHERS     = 1.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

When you have the XML file in a binary table, you can start to process the XML data. To do this we need an iXML factory which will help us create an input stream from the binary table.  

 TYPE-POOLS: ixml.
 DATA: l_ixml TYPE REF TO if_ixml.
 l_ixml = cl_ixml=>create( ).


This iXML factory can create a XML stream factory which actually creates an input stream based on the binary table.

 DATA: l_streamfactory   TYPE REF TO if_ixml_stream_factory,
       l_istream         TYPE REF TO if_ixml_istream.

 l_streamfactory = l_ixml->create_stream_factory( ).

 l_istream = l_streamfactory->create_istream_itable(
                                table = l_xml_table
                                size  = l_xml_table_size ).


Before creating the XML DOM object you have to create an empty document and a parser object. The document object will represent the DOM object and the parser is needed to convert the XML stream into a XML DOM object.

 DATA:  l_document TYPE REF TO if_ixml_document,
         l_parser    TYPE REF TO if_ixml_parser.

 l_document = l_ixml->create_document( ).
 l_parser = l_ixml->create_parser(
                      stream_factory = l_streamfactory
                      istream        = l_istream
                      document       = l_document ).


After having created the parser, the empty document and the input stream, you can start the conversion using the parse method of the parser. If the parsing failes, you  can retrieve the error using the if_xml_parse_error interface.

 DATA: l_parse_error TYPE REF TO if_ixml_parse_error.

 IF l_parser->parse( ) NE 0.
  IF l_parser->num_errors( ) NE 0.
   ...
   l_parse_error = l_parser->get_error( index = l_index ).
   ...
  ENDIF.
 ENDIF.

At this point you have to check if the DOM was really created and if so you can process the document.

 IF l_parser->is_dom_generating( ) EQ 'X'.
  PERFORM process_dom USING l_document.
 ENDIF.

To process it you need to cast the document to a node interface object.

 DATA: node      TYPE REF TO if_ixml_node,
       iterator  TYPE REF TO if_ixml_node_iterator.

 node ?= document.

 CHECK NOT node IS INITIAL.

When having the DOM object as a node object, you can use an iterator to “walk” trough the DOM object. To do this you have to create the iterator of the node object and get the root node using the get_next method of the iterator.

 iterator  = node->create_iterator( ).
 node = iterator->get_next( ).

Scrolling through the DOM, you can fill your internal table. The method get_type will tell you if the node is an element- of a text node.  In the element node you will find the name of the element and the attribute which belong to the element node. In the text node, you will find the value of the element node.

 WHILE NOT node IS INITIAL.
  CASE node->get_type( ).
   WHEN if_ixml_node=>co_node_element.
    name  = node->get_name( ).
    ...
   WHEN if_ixml_node=>co_node_text OR
        if_ixml_node=>co_node_cdata_section.
    value = node->get_value( ).
    ...
  ENDCASE.
  node = iterator->get_next( ).
 ENDWHILE.

The attributes of an element will be available in a node map with the interface if_ixml_named_node_map. This interface can be used to read the names and values of the attributes in the element.

 nodemap = node->get_attributes( ).
 IF NOT nodemap IS INITIAL.
  count = nodemap->get_length( ).
  DO count TIMES.
   index  = sy-index - 1.
   attr   = nodemap->get_item( index ).
   name   = attr->get_name( ).
   prefix = attr->get_namespace_prefix( ).
   value  = attr->get_value( ).
   ...
  ENDDO.
 ENDIF.

This finished the second step-of-three. As mentioned before the next log will focus on the use of SAP DOM within XI ABAP mapping.

A complete example

24 Comments