This blog summarizes some important  facts you need to know, if you want to deal with XML-data in ABAP programs.

In ABAP you can work with XML-data in mainly the following ways:

  • Working with class based libraries
  • Calling transformations
  • Serializing and deserializing ABAP-data

For a complete overview see https://help.sap.com/abapdocu_750/en/index.htm?file=abenabap_xml.htm.

ABAP-Libraries for XML

ABAP offers two libraries for working with XML-data:

  • iXML-Library
  • sXML-Library

iXML-Library

The interfaces and classes of the iXML-Library (“i” stands for integrated) from package SIXML allow you to

  • validate and parse XML-data given in XML 1.0 format (that is the one with angle brackets) into a DOM representation
  • edit the DOM representation or create one from scratch
  • render a DOM representation into XML 1.0 format

The benefit of the DOM representation of the iXML-Library is that you can access all parts of the XML-data directly and that DTDs are supported. The drawback is the memory consumption of the DOM that can become up to ten times the actual size of the document. Besides the main feature of parsing XML-data into a DOM in one go, the iXML-Library offers some more functionality as sequential parsing and token based parsing. All that is made available in a lot of interfaces and methods.

Simple Example

Using the iXML-Library for parsing XML-data into a DOM, modifying the DOM by setting the values of all text elements to upper case, and rendering the DOM back into an XML string. The XML-data cannot be accessed directly but you must use input and output streams.

DATA xml TYPE xstring.
xml =
cl_abap_codepage=>convert_to(
  `` &&
  `aaaa` &&
  `bbbb` &&
  `cccc` &&
  `` ).

“Access iXML-Library

DATA ixml TYPE REF TO if_ixml.
DATA stream_factory TYPE REF TO if_ixml_stream_factory.
DATA document TYPE REF TO if_ixml_document.
ixml = cl_ixml=>create( ).

stream_factory = ixml->create_stream_factory( ).

document = ixml->create_document( ).

“Parse XML-data into DOM
IF
ixml->create_parser(

  document = document
  stream_factory = stream_factory
  istream = stream_factory->create_istream_xstring( string = xml )
  )->parse( ) <> 0.
  RETURN.
ENDIF.

“Iterate DOM and modify text elements
DATA
iterator TYPE REF TO if_ixml_node_iterator.

iterator = document->create_iterator( ).
DATA node TYPE REF TO if_ixml_node.
DO.
  node = iterator->get_next( ).
  IF node IS INITIAL.
    EXIT.
  ENDIF.
  IF node->get_type( ) = if_ixml_node=>co_node_text.
    node->set_value( to_upper( node->get_value( ) ) ).
  ENDIF.
ENDDO.

“Render DOM into xstring
CLEAR
xml.

document->render(
  ostream = ixml->create_stream_factory(
  )->create_ostream_xstring(
  string = xml ) ).

cl_abap_browser=>show_xml( xml_xstring = xml ).

sXML-Library

The interfaces and classes of the sXML-Library  (“s” stands for serial) from package SXML allow you to

  • create token based or object oriented XML-readers to parse XML-data given in several formats into several targets
  • create token based or object oriented XML-writers to to render from several sources into XML-data of several formats

The sXML library supports the following formats that can be specified when creating an XML-writer (an XML-reader recognizes the format automatically):

  • XML 1.0, default, character data with angle bracket tags
  • XOP,  XML-binary Optimized Packaging where raw data are stored in external byte strings
  • Binary XML, SAP-specific binary XML-format that suppresses redundant information
  • JSON, XML-representation of JSON-data based on the SAP-specific JSON-XML that defines a mapping between JSON and XML

In contrast to the iXML-Library, no DOM is created and DTDs are not supported. XML-data are processed sequentially from top to bottom and you have access to the current node only. If no parallel access to different nodes of a DOM and no DTDs are needed, sXML is a highly performant alternative to the iXML-Library supporting more formats and enabling the access to JSON-data in ABAP.

Simple Example

Using an object oriented XML-reader and an object oriented XML-writer the same XML-data as in the iXML-example are parsed, modified and rendered within in a single loop.

DATA xml TYPE xstring.
xml =
cl_abap_codepage=>convert_to(
  `` &&
  `aaaa` &&
  `bbbb` &&
  `cccc` &&
  `` ).

“Render, modify and parse XML-data
DATA
reader TYPE REF TO if_sxml_reader.
DATA writer TYPE REF TO if_sxml_writer.
DATA node  TYPE REF TO if_sxml_node.
DATA value_node  TYPE REF TO if_sxml_value_node.
reader = cl_sxml_string_reader=>create( xml ).
writer = cl_sxml_string_writer=>create( ).
DO.
  node = reader->read_next_node( ).
  IF node IS INITIAL.
    EXIT.
  ENDIF.
  TRY.
      value_node ?=  node.
      IF value_node->value_type = if_sxml_value=>co_vt_text.
        value_node->set_value(
          to_upper( value_node->get_value( ) ) ).
      ENDIF.
    CATCH cx_sy_move_cast_error.
  ENDTRY.
  writer->write_node( node ).
ENDDO.

DATA output TYPE REF TO cl_sxml_string_writer.
output ?= writer.
cl_abap_browser=>show_xml( xml_xstring = output->get_output( ) ).

Transformations between XML-Data

In order to perform transformations between XML-data you can use XSLT-programs. XSLT-programs are written in standard XSLT 1.0, are stored as repository objects, and can be called with:

CALL TRANSFORMATION xslt SOURCE XML xml_source
                         RESULT XML xml_result.

The transformations are processed by an XSLT-processor in the ABAP runtime environment that allows some specials, as calling ABAP methods from XSLT.

The sources xml_source can be specified as

  • strings, xstrings, internal tables with linetype string or xstring
  • reference variables pointing to input streams, DOMs or nodes of DOMs of the iXML-Library
  • reference variables pointing to XML-readers of the sXML-Library

The sources can contain valid XML- and also JSON-data. The latter is handled automatically as if it were JSON-XML-data, i.e. JSON is implicitly mapped to a SAP-specific XML-format.

The results xml_result can be specified as

  • strings, xstrings, internal tables with linetype string or xstring
  • reference variables pointing to output streams or DOMs of the iXML-Library
  • reference variables pointing to XML-writers of the sXML-Library

The results are XML-data that are written in the target format. If the target is an XML-writer of the sXML-Library created for the JSON-format, the XML-data must be delivered in JSON-XML.

It might be worth to note that any XSL-transformation called with CALL TRANSFORMATION invokes the iXML-Library and works on a fully fledged DOM. Therefore, for very large data sets it might be better to work sequentially with sXML instead.

Simple Example

Using an XSLT-program the same XML-data as in the iXML-example and sXML-example are transformed. The result is the same as above, but the work is outsourced to the transformation.

DATA xml TYPE xstring.
xml =
cl_abap_codepage=>convert_to(
  `` &&
  `aaaa` &&
  `bbbb` &&
  `cccc` &&
  `` ).

CALL TRANSFORMATION demo_lower_upper
                    SOURCE XML xml
                    RESULT XML xml.

cl_abap_browser=>show_xml( xml_xstring = xml ).

The transformation demo_lower_upper is as follows (excuse me if the XSLT-code is crappy, in fact I don’t speak XSLT, but the example works):

  xmlns:xsl=https://www.w3.org/1999/XSL/Transform version=“1.0”>
  “smallcase”
select=“‘abcdefghijklmnopqrstuvwxyz’”/>
  “uppercase” select=“‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’”/>
 
 

Serializations and Deserializations

Last but not least, you want to serialize your ABAP-data into XML (or JSON) or deserialize XML (or JSON)  back to ABAP. You do so by using CALL TRANSFORMATION again but you specify ABAP-data as sources or as result.

For serializations and derserializations, you can use two kinds of transformations:

  • XSLT
  • ST (SAP’s own Simple Transformations)

Serializations and Deserializations with XSLT

As seen abaove, XSLT transforms XML to XML. How can XSLT handle ABAP-data? The trickis, that when calling an XSLT-program for an ABAP-source, the ABAP-data are serialized internally into an SAP-specific canonical XML-format called asXML and that the XSLT works on this XML-data. From XML to ABAP it is the other way around. The XML-source must be transformed in such a way that the result is asXML, that then can be deserialized internally into ABAP-data. In order to see the asXML-data, you can use the predefined identity transformation ID that does not change the XML-data. When serializing with ID, the XML-ouptut is asXML. When deserializing with ID, the XML-Input must be asXML. (When working with JSON, basically the same is true with the difference that a further mapping, namely from asXML to JSON-XML must take place; this mapping can be selfdefined or is implicitly done when using ID).

As mentioned above, XSLT works on a fully fledged DOM. This can have negative impact on memory consumption and performance. If that is an issue, it can be better to work with Simple Trnasformations where no DOM is involved. The predefined transformation ID works on a DOM if objects of the iXML-Library are used as source or as result.

Simple Example

Here, the same XSLT-program as above is called for ABAP-data. As a result the contents of all text elements of the asXML representing the internal table are transformed to upper case.

DATA itab TYPE TABLE OF string.
APPEND `aaaa` TO itab.
APPEND `bbbb` TO itab.
APPEND `cccc` TO itab.

DATA xml TYPE xstring.
CALL TRANSFORMATION demo_upper_lower
                    SOURCE itab = itab
                    RESULT XML xml.
cl_abap_browser=>show_xml( xml_xstring = xml ).

Serializations and Deserializations with ST

Simple Transformations cannot transform XML to XML. There only purpose is to serialize and deserialize ABAP-data and they work on ABAP-inputs and ABAP–outputs directly. You don’t need the full asXML-format when working with ST  (only the mapping of elementary value formats is taken from asXML). If an ST-template produces JSON-XML from an ABAP-input, its output can of course also be passed to a JSON-Writer in order to produce JSON-data and vice versa (see ABAP 2 JSON and JSON 2 ABAP with ST).

Simple transformations do not work on a DOM and do not invoke the iXML-Library internally. Instead, the rendering and parsing mechanisms of the sXML-Library are used. With other words, simple transformations process the data sequentially. As a consequence, simple transformations are less powerful but much faster and they need less memory compared to XSLT.

Examples

See https://help.sap.com/abapdocu_750/en/index.htm?file=abenabap_st.htm

New NetWeaver Information at SAP.com

Very Helpfull

User Rating: Be the first one !