Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Data Interfaces and Communication Interfaces →  ABAP and XML →  Class Libraries for XML →  iXML Library →  iXML Library, Examples 

iXML Library, Down Casts

Down cast options for the interfaces in iXML Library.

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT demo_ixml_casting.

CLASS ixml_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS ixml_demo IMPLEMENTATION.
  METHOD main.
    DATA(ixml)           = cl_ixml=>create( ).
    DATA(stream_factory) = ixml->create_stream_factory( ).

    DATA(istream)        = stream_factory->create_istream_string(
      `<texts>`                             &&
      `  <text1 format="bold">aaa</text1>`   &&
      `  <text2 format="italic">bbb</text2>` &&
      `</texts>` ).

    DATA(document)       = ixml->create_document( ).
    DATA(parser)         = ixml->create_parser(
                             stream_factory = stream_factory
                            istream        = istream
                            document       = document ).

    DATA(rc) = parser->parse( ).

    IF rc <> ixml_mr_parser_ok.
      ... "Error handling
      RETURN.
    ENDIF.

    DATA(iterator) = document->create_iterator( ).
    DO.
      DATA(node) = iterator->get_next( ).
      IF node IS INITIAL.
        EXIT.
      ENDIF.
      DATA element TYPE REF TO if_ixml_element.
      "Normal down cast
      TRY.
          element = CAST #( node ).
        CATCH cx_sy_move_cast_error .
          cl_demo_output=>write_text(
            |{ node->get_name( ) } is not an element| ).
      ENDTRY.
      "Special down cast
      element ?=  node->query_interface( ixml_iid_element ).
      IF element IS INITIAL.
        cl_demo_output=>write_text(
          |{ node->get_name( ) } is not an element| ).
      ENDIF.
    ENDDO.

    cl_demo_output=>display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  ixml_demo=>main( ).

Description

An iterator is created for all nodes of a parsed XML and used to iterate. To identify nodes that are not elements, down casts are performed from IF_IXML_NODE to IF_IXML_ELEMENT. Both the usual down casts with the casting operator CAST and a TRY control structure are demonstrated and down casts using the method QUERY_INTERFACE of the interface IF_IXML_UNKNOWN.