Skip to content

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

sXML Library, Modify XML Data

Reads, modifies, and writes to XML data.

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT demo_sxml_reader_writer.

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

CLASS sxml_demo IMPLEMENTATION.
  METHOD main.
    DATA(xml) =
     cl_abap_codepage=>convert_to(
       `<text>` &&
       `<line>aaaa</line>` &&
       `<line>bbbb</line>` &&
       `<line>cccc</line>` &&
       `</text>` ).

    DATA(out) = cl_demo_output=>new(
      )->begin_section( 'Original XML-Data'
      )->write_xml( xml ).

    DATA(reader) = cl_sxml_string_reader=>create( xml ).
    DATA(writer) = CAST if_sxml_writer(
                          cl_sxml_string_writer=>create( ) ).
    DO.
      DATA(node) = reader->read_next_node( ).
      IF node IS INITIAL.
        EXIT.
      ENDIF.
      TRY.
          DATA(value_node) = CAST if_sxml_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.

    out->next_section( 'Modified XML-Data'
      )->write_xml(
        CAST cl_sxml_string_writer( writer )->get_output( )
      )->display( ).
  ENDMETHOD.
ENDCLASS.

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

Description

XML data is parsed using object-oriented methods. If the node is a character-like value node, the value in the associated object is transformed to uppercase letters. All read nodes and any modified nodes are rendered using object-oriented methods in the same loop. The result is XML data in which all literal text elements are in uppercase letters. See also the corresponding example for the iXML Library.


Note

Any parts of XML data can be modified in a similar way. For example, the functions to_mixed, from_mixed can be used to convert the names of XML elements between different naming conventions.