Update
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
- [ABAP SQL](#abap-sql)
|
||||
- [Introduction](#introduction)
|
||||
- [Excursion: Database Tables and Views](#excursion-database-tables-and-views)
|
||||
- [Reading Data Using SELECT](#reading-data-using-select)
|
||||
- [Basic Syntax](#basic-syntax)
|
||||
- [Using SELECT for Multiple Purposes](#using-select-for-multiple-purposes)
|
||||
@@ -46,11 +47,12 @@
|
||||
- ABAP SQL statements use the ABAP SQL interface. This interface transforms all ABAP SQL statements that access the standard database of an AS ABAP to platform-dependent SQL and forwards the results to the database system.
|
||||
- Generally bear in mind the [performance notes](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_sql_perfo.htm) when using
|
||||
ABAP SQL. The considerations there are not relevant for this cheat sheet since
|
||||
the focus is on syntactical options.
|
||||
the focus is on syntax options.
|
||||
|
||||
## Excursion: Database Tables and Views
|
||||
|
||||
<details>
|
||||
<summary>Excursion: Database Tables and Views</summary>
|
||||
<summary>Expand to view the details</summary>
|
||||
<br>
|
||||
|
||||
This section provides bullet points on database tables and views which contain persisted data. Note that the code snippets in this cheat sheet focus on database tables as [data source](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abendata_source_glosry.htm "Glossary Entry") for ABAP SQL statements.
|
||||
|
||||
@@ -270,7 +270,7 @@ MODIFY zdemo_abap_carr FROM TABLE @( VALUE #(
|
||||
> **💡 Note**<br>
|
||||
> Some of the additions and concepts mentioned here are
|
||||
also valid for other constructor expressions further down but not
|
||||
necessarily mentioned explicitly. See the details on the syntactical
|
||||
necessarily mentioned explicitly. See the details on the syntax
|
||||
options of the constructor operators in the ABAP Keyword Documentation.
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
@@ -976,8 +976,7 @@ The type properties are represented by attributes that are accessible through th
|
||||
> - For each type category (elementary type, table, and so on), there is a type description class (e.g. `CL_ABAP_STRUCTDESCR` for structures, as shown in the hierarchy tree above) that has special attributes (i.e. the properties of the respective types).
|
||||
> - References to type description objects can be used, for example, after the `TYPE HANDLE` addition of the `CREATE DATA` and `ASSIGN` statements.
|
||||
|
||||
The following examples show the retrieval of type information. Instead of the
|
||||
cumbersome extra declaration of data reference variables, you can use
|
||||
The following examples show the retrieval of type information. Instead of the extra declaration of data reference variables, you can use
|
||||
[inline declarations](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abeninline_declaration_glosry.htm "Glossary Entry").
|
||||
[Method chaining](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenmethod_chaining_glosry.htm "Glossary Entry")
|
||||
comes in handy, too.
|
||||
|
||||
@@ -1358,7 +1358,7 @@ REPLACE ALL OCCURRENCES OF `Z`
|
||||
[`replace`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenreplace_functions.htm),
|
||||
allows you to store the result of a substring replacement in a separate
|
||||
variable.
|
||||
- What makes it particularly powerful in particular is that it
|
||||
- What makes it especially powerful is that it
|
||||
returns a value, so it can be used at almost any [read
|
||||
positions](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenread_position_glosry.htm "Glossary Entry").
|
||||
- The parameters of the `replace` string functions are similar to those of the `find` function. In addition, there is the `with` parameter for the replacement. Setting `occ` to `0` means that all occurrences are respected for the replacement.
|
||||
|
||||
@@ -538,7 +538,7 @@ by containing the keys, it also contains the component group
|
||||
[`%key`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapderived_types_key.htm)
|
||||
in the case above). The F2 information in ADT helps you find out about
|
||||
the available components in a variable. The image below shows the
|
||||
details of `%data` when clicking the `derived type`
|
||||
details of `%data` when clicking the *derived type*
|
||||
link in the first ADT F2 information screen.
|
||||
|
||||

|
||||
@@ -586,7 +586,7 @@ Bullet points on selected `%` components:
|
||||
instead of `%key`. `%tky` includes
|
||||
`%key` and also the draft indicator
|
||||
`%is_draft`. When using `%tky` in non-draft
|
||||
scenarios, you are prepared for a later, potential switch to a
|
||||
scenarios, you are prepared for a potential, later switch to a
|
||||
draft scenario. In doing so, you can avoid lots of adaptations
|
||||
in your code by manually adding the indicator.
|
||||
- [`%control`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapderived_types_control.htm)
|
||||
@@ -1347,8 +1347,7 @@ The following restrictions apply to operations and/or statements in the individu
|
||||
|[Dynpro](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abendynpro_glosry.htm) processing (e.g. `SET SCREEN`, `CALL SCREEN`, `LEAVE SCREEN`, `CALL DIALOG`, `SUPPRESS DIALOG`, `MESSAGE` without `INTO`, `WRITE`, `STOP`) <br><br>(unrestricted ABAP language scope)|- |- |- |Not allowed in ABAP behavior implementations. Results in a runtime error. |
|
||||
|Transaction processing (`CALL TRANSACTION`, `LEAVE TRANSACTION`) <br><br>(unrestricted ABAP language scope)| -| -| - |Not allowed to prevent (unwanted) integration of other LUWs. |
|
||||
|Raising an exception (`RAISE EXCEPTION`) |-| -| - |It is not allowed to leave a RAP transaction this way. |
|
||||
|Report processing (`SUBMIT ...`) <br><br>(unrestricted ABAP language scope)|- |- |- |Not allowed in transactional contexts. Results in a syntax or runtime error.
|
||||
`SUBMIT ... AND RETURN` does not currently return an error, but it leads to potentially unwanted screen processing, and because of the missing return channel, there is no proper error handling. |
|
||||
|Report processing (`SUBMIT ...`) <br><br>(unrestricted ABAP language scope)|- |- |- |Not allowed in transactional contexts. Results in a syntax or runtime error. <br>`SUBMIT ... AND RETURN` does not currently return an error, but it leads to potentially unwanted screen processing, and because of the missing return channel, there is no proper error handling. |
|
||||
|
||||
|
||||
</details>
|
||||
|
||||
@@ -116,8 +116,8 @@ It provides references to more detailed information on the topic.
|
||||
ref2 = REF #( num1 ).
|
||||
|
||||
DATA str_itab TYPE string_table.
|
||||
"The following statements are invalid (the pointless WRITE statement here
|
||||
"just represents invalid classic ABAP UI-related statements)
|
||||
"The following statements are invalid. The pointless WRITE statement here
|
||||
"just represents invalid classic ABAP UI-related statements.
|
||||
READ REPORT 'ZCL_DEMO_ABAP_UNIT_TEST=======CCAU' INTO str_itab.
|
||||
WRITE 'hi'.
|
||||
BREAK-POINT.
|
||||
@@ -127,27 +127,27 @@ It provides references to more detailed information on the topic.
|
||||
|
||||
2) If available to you, you have accessed an on-premise ABAP system using ADT. It is assumed that the latest ABAP release is available.
|
||||
|
||||
a) Check API Status information
|
||||
a) Checking API Status information
|
||||
- Choose `CTRL + SHIFT + A` to open the search in ADT. Search the class `CL_ABAP_RANDOM_INT`. Once you have opened the class, check the *Properties* tab and find the API status information.
|
||||
|
||||
b) Create an example class
|
||||
b) Creating an example class
|
||||
- Create a global class and insert the code from above. Depending on the name of the class you created, replace the class name in the snippet.
|
||||
- If you have not imported the ABAP cheat sheet GitHub repository, remove the lines of code using artifacts from that repository, i.e. change the `SELECT` and `READ REPORT` statements. You should not see any syntax errors. Activate the class.
|
||||
- Run the class with *F9*. The code should have been processed up to the `BREAK-POINT` statement and the debugger should have started. You may want to check the content of the variables in the debugger. Choose *Terminate* to exit the debugger.
|
||||
- So, unlike in the case of ABAP Cloud above, the code should not cause any problems (other than the fact that it does not make a lot of sense).
|
||||
- For the example class created, check the information in the *Properties* tab. Choose *General*. The *ABAP Language Version* is maintained as *Standard ABAP*:
|
||||
For the example class created, check the information in the *Properties* tab. Choose *General*. The *ABAP Language Version* is maintained as *Standard ABAP*:
|
||||

|
||||
- If you have not imported the ABAP cheat sheet GitHub repository, remove the lines of code using artifacts from that repository, i.e. remove the statements using objects starting with `Z...`. You should not see any syntax errors. Activate the class.
|
||||
- Run the class with *F9*. The code should have been processed up to the `BREAK-POINT` statement and the debugger should have started. You may want to check the content of the variables in the debugger. Choose *Terminate* to exit the debugger.
|
||||
- So, unlike in the case of ABAP Cloud above, you should be able to activate and run the code (which does not represent a meaningful code example).
|
||||
|
||||
c) Verify that your code in classic ABAP is cloud-ready
|
||||
c) Verifying cloud-readiness of your code in classic ABAP
|
||||
- You have walked through b), created a class, inserted the code from above, and activated the class. The *ABAP Language Version* is maintained as *Standard ABAP* in the *Properties* tab.
|
||||
- Verifying if your code is cloud-ready
|
||||
- You can use ATC check variant `ABAP_CLOUD_READINESS` for this purpose.
|
||||
- For example, in your class, right-click and choose *Run As* → *4 ABAP Test Cockpit With...*. Enter `ABAP_CLOUD_READINESS` in the pop-up window and choose *Ok*. The ATC check run is started.
|
||||
- As a result of the ATC check run (note that it may take a while to complete), the *ATC Problems* tab in ADT should display results. In this case, these are the errors and warnings mentioned above, indicating that the code is not cloud-ready in various places. Double-click on the findings for more detailed information.
|
||||
|
||||
d) Develop in classic ABAP in a cloud-ready manner
|
||||
d) Cloud-ready development in classic ABAP
|
||||
- You have walked through b), created a class, inserted the code from above, and activated the class. The *ABAP Language Version* is maintained as *Standard ABAP* in the *Properties* tab under *General*.
|
||||
- Suppose you want to develop in a cloud-ready manner and use ABAP for Cloud Development, i.e. the restricted ABAP language version, in classic ABAP (e.g. an on-premise ABAP system that allows the unrestricted ABAP language version).
|
||||
- Suppose you want to do cloud-ready development and use ABAP for Cloud Development, i.e. the restricted ABAP language version, in classic ABAP (e.g. an on-premise ABAP system that allows the unrestricted ABAP language version).
|
||||
- Open the *Properties* tab and choose *General* for this purpose.
|
||||
- Choose the *Edit...* button to the right of the *ABAP Language Version* property.
|
||||
- Select *ABAP for Cloud Development* in the pop-up window and choose *Ok*.
|
||||
|
||||
@@ -441,7 +441,7 @@ SELECTION-SCREEN BEGIN OF BLOCK bl WITH FRAME.
|
||||
PARAMETERS: pa1 RADIOBUTTON GROUP gr MODIF ID mbl,
|
||||
pa2 RADIOBUTTON GROUP gr MODIF ID mbl,
|
||||
pa3 RADIOBUTTON GROUP gr,
|
||||
pa4 RADIOBUTTON GROUP gr..
|
||||
pa4 RADIOBUTTON GROUP gr.
|
||||
SELECTION-SCREEN END OF BLOCK bl.
|
||||
|
||||
"Suppose you want to modify the elements before displaying, e.g. make the
|
||||
|
||||
@@ -173,7 +173,7 @@ ENDIF.
|
||||
- more formats are possible (standard XML 1.0, but also XOP, binary XML, and JSON).
|
||||
- no document is created in DOM format (if you do not need to a DOM representation and DTDs, sXML is a more performant alternative to iXML).
|
||||
|
||||
Rendering XML data using sXML:
|
||||
Creating XML data using sXML:
|
||||
|
||||
```abap
|
||||
"************** Token-based rendering **************
|
||||
@@ -201,7 +201,7 @@ TRY.
|
||||
writer->open_element( name = 'carrier' ).
|
||||
writer->write_value( 'LH' ).
|
||||
writer->close_element( ).
|
||||
writer->open_element( name = 'flightNumber' ).
|
||||
writer->open_element( name = 'flightnumber' ).
|
||||
writer->write_value( '400' ).
|
||||
writer->close_element( ).
|
||||
writer->close_element( ).
|
||||
@@ -209,7 +209,7 @@ TRY.
|
||||
writer->open_element( name = 'carrier' ).
|
||||
writer->write_value( 'DL' ).
|
||||
writer->close_element( ).
|
||||
writer->open_element( name = 'flightNumber' ).
|
||||
writer->open_element( name = 'flightnumber' ).
|
||||
writer->write_value( '1984' ).
|
||||
writer->close_element( ).
|
||||
writer->close_element( ).
|
||||
@@ -395,7 +395,7 @@ To perform transformations in ABAP, you can use:
|
||||
- SAP-delivered and predefined XSL transformation with the name `ID`
|
||||
- Used to read and write the asXML (and also asJSON) format.
|
||||
- When the transformation ID is called, the resulting intermediate formats (asXML, asJSON) are the direct output.
|
||||
- When used, for example, to transform ABAP data, such as a structure, to XML, the transformation does not change the structure itself. You can, however, change the structure by implementing you own XSLT.
|
||||
- When used, for example, to transform ABAP data, such as a structure, to XML, the transformation does not change the structure itself. You can, however, change the structure by implementing your own XSLT.
|
||||
- Simple Transformation (ST)
|
||||
- Repository objects written in an SAP-specific programming language for transformations between XML formats and ABAP data
|
||||
|
||||
@@ -424,7 +424,7 @@ Possible transformations, some of which are covered in the example:
|
||||
|
||||
The following code snippets demonstrate a selection of possible syntax options when using [`CALL TRANSFORMATION`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapcall_transformation.htm) statements.
|
||||
|
||||
Specifying transformations
|
||||
**Specifying transformations**
|
||||
|
||||
```abap
|
||||
"Specifying the name of an XSL or Simple Transformation statically
|
||||
@@ -445,7 +445,8 @@ CATCH cx_invalid_transformation.
|
||||
ENDTRY.
|
||||
```
|
||||
|
||||
Specifying the source of the transformation
|
||||
**Specifying the source of the transformation**
|
||||
|
||||
There are multiple options. Check the executable example to see them in action.
|
||||
|
||||
```abap
|
||||
@@ -476,7 +477,8 @@ CALL TRANSFORMATION ... SOURCE (srctab)
|
||||
RESULT ...
|
||||
```
|
||||
|
||||
Specifying the result of the transformation
|
||||
**Specifying the result of the transformation**
|
||||
|
||||
There are multiple options. Check the executable example to see them in action.
|
||||
|
||||
```abap
|
||||
@@ -547,7 +549,7 @@ json_wr_cast->set_option( option = if_sxml_writer=>co_opt_linebreaks ).
|
||||
json_wr_cast->set_option( option = if_sxml_writer=>co_opt_indent ).
|
||||
|
||||
CALL TRANSFORMATION id SOURCE itab = str_table
|
||||
RESULT XML json_wr.
|
||||
RESULT XML json_wr.
|
||||
|
||||
DATA(json_formatted) = cl_abap_conv_codepage=>create_in( )->convert( json_wr->get_output( ) ).
|
||||
|
||||
@@ -565,16 +567,16 @@ DATA(json_formatted) = cl_abap_conv_codepage=>create_in( )->convert( json_wr->ge
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
## Excursion: Converting string <-> xstring
|
||||
In the code snippets above and in the exexcutable example, many operations are performed using binary data.
|
||||
In the code snippets above and in the executable example, many operations are performed using binary data.
|
||||
This excursion shows the conversion of string <-> xstring using a codepage. The examples use UTF-8.
|
||||
For example, you can use the `cl_abap_conv_codepage` class and the [XCO library](https://help.sap.com/docs/SAP_S4HANA_CLOUD/0f69f8fb28ac4bf48d2b57b9637e81fa/702b5328be1a4bc4852ce29b09506b04.html?locale=en-US).
|
||||
For example, you can use the `cl_abap_conv_codepage` class and the [XCO library](https://help.sap.com/docs/SAP_S4HANA_CLOUD/0f69f8fb28ac4bf48d2b57b9637e81fa/702b5328be1a4bc4852ce29b09506b04.html?locale=en-US). The executable example also covers an excursion regarding compressing and decompressing of binary data.
|
||||
|
||||
```abap
|
||||
DATA(xml_string) = `<TXT>ABAP</TXT>`.
|
||||
|
||||
"string -> xstring
|
||||
"Note: UTF-8 is used by default. Here, it is specified explicitly.
|
||||
"Exceptions are caught with cx_sy_conversion_codepage exception.
|
||||
"Exception class that can be used: cx_sy_conversion_codepage
|
||||
TRY.
|
||||
DATA(conv_xstring) = cl_abap_conv_codepage=>create_out( codepage = `UTF-8` )->convert( xml_string ).
|
||||
CATCH cx_sy_conversion_codepage.
|
||||
@@ -610,9 +612,10 @@ DATA(conv_string_xco) = xco_cp=>xstring( conv_xstring_xco
|
||||
- [ST Examples](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenst_abexas.htm)
|
||||
- [asXML](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_xslt_asxml.htm)
|
||||
- [asJSON](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_asjson.htm)
|
||||
- [JSON Examples](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_json_abexas.htm)
|
||||
- [`CALL TRANSFORMATION`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapcall_transformation.htm)
|
||||
- [`CALL TRANSFORMATION` Examples](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abencall_transformation_abexas.htm)
|
||||
- [JSON Examples](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_json_abexas.htm)
|
||||
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
@@ -620,8 +623,16 @@ DATA(conv_string_xco) = xco_cp=>xstring( conv_xstring_xco
|
||||
[zcl_demo_abap_xml_json](./src/zcl_demo_abap_xml_json.clas.abap)
|
||||
|
||||
> **💡 Note**<br>
|
||||
> - The steps to import and run the code are outlined [here](README.md#-getting-started-with-the-examples).
|
||||
> - The executable example ...
|
||||
> - does not cover all facets, techniques, and syntax options.
|
||||
> - uses demo XSLT and ST programs (which are not intended to be role models for proper XSLT/ST design), apart from snippets that use the predefined identity transformation (ID).
|
||||
> - provides a rough overview of how to work with XML and JSON in ABAP using simple contexts.
|
||||
> - covers the following topics in simple contexts:
|
||||
> - Creating/Parsing XML Data Using iXML
|
||||
> - Creating/Parsing XML Data Using sXML
|
||||
> - XML Transformations using XSLT and Simple Transformations
|
||||
> - Creating/Parsing XML Data Using iXML
|
||||
> - Serializations (ABAP -> XML) and Deserialization (XML -> ABAP) using the identity transformation ID (elementary types, structures, internal tables, data and object references)
|
||||
> - `CALL TRANSFORMATION` syntax options, sources and targets of transformations
|
||||
> - Dealing with JSON data
|
||||
> - Excursions: Converting string <-> xstring, compressing and decompressing binary data
|
||||
> - uses, apart from the predefined identity transformation (ID), demo XSLT and ST programs. They are not intended to be role models for proper XSLT/ST design.
|
||||
> - The steps to import and run the code are outlined [here](README.md#🎬-getting-started-with-the-examples).
|
||||
> - [Disclaimer](README.md#⚠️-disclaimer)
|
||||
@@ -79,14 +79,14 @@ ABAP cheat sheets[^1] ...
|
||||
|[SAP LUW](17_SAP_LUW.md)|Provides a high-level overview of the [SAP LUW](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abensap_luw_glosry.htm) concept that deals with data consistency with a focus on SAP LUW-related statements <br> 💡 Several statements covered in the cheat sheet and the executable example are only relevant to [classic ABAP](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenclassic_abap_glosry.htm).|Program `ZDEMO_ABAP_SAP_LUW`|
|
||||
|[Dynpro](18_Dynpro.md)|Provides a high-level overview of dynpro topics with a focus on dynpro-related statements <br> 💡 The content of this cheat sheet and the executable example are only relevant to [classic ABAP](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenclassic_abap_glosry.htm).|Program `ZDEMO_ABAP_DYNPRO`|
|
||||
|[Selection Screens and Classic Lists](20_Selection_Screens_Lists.md)|Provides a high-level overview of selection screens and classic lists with a focus on related statements <br> 💡 The content of this cheat sheet and the executable examples are only relevant to [classic ABAP](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenclassic_abap_glosry.htm).|Program `ZDEMO_ABAP_SELSCR_LISTS_INTRO` (the "intro" program, from which the other related example programs can be started)|
|
||||
|[Working with XML and JSON in ABAP](21_XML_JSON.md)|Covers processing XML using class libraries, XML transformations using XSLT and Simple Transformations (ST), serializations (ABAP to XML) and deserializations (XML to ABAP), `CALL TRANSFORMATION` syntax, dealing with JSON data|[zcl_demo_abap_xml_json](./src/zcl_demo_abap_xml_json.clas.abap)|
|
||||
|[Working with XML and JSON in ABAP](21_XML_JSON.md)|Covers processing XML using class libraries, XML transformations using XSLT and Simple Transformations (ST), serializations (ABAP to XML) and deserializations (XML to ABAP), dealing with JSON data|[zcl_demo_abap_xml_json](./src/zcl_demo_abap_xml_json.clas.abap)|
|
||||
|
||||
<br>
|
||||
|
||||
## 🎬 Getting Started with the Examples
|
||||
|
||||
The main focus of the ABAP Cheat Sheets is ABAP Cloud. The examples in the *main* branch of the repository are designed to be imported into the SAP BTP ABAP environment.
|
||||
For classic ABAP, you can find examples in the other branches of the repository that you can import into your SAP system. Just select the appropriate version (*v757* stands for ABAP version 7.57). Check the information in the following collapsible sections for your system environment and perform the required steps.
|
||||
For classic ABAP, you can find examples in the other branches of the repository that you can import into your sandbox SAP system. Just select the appropriate version (*v757* stands for ABAP version 7.57). Check the information in the following collapsible sections for your system environment and perform the required steps.
|
||||
|
||||
<details>
|
||||
<summary>1) General info</summary>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* ABAP cheat sheet: Internal tables
|
||||
*
|
||||
* -------------------------- PURPOSE ----------------------------------
|
||||
* - Example to demonstrate various syntactical options for working with
|
||||
* - Example to demonstrate various syntax options for working with
|
||||
* internal tables.
|
||||
* - Topics covered: Creating, filling, reading from, sorting, modifying
|
||||
* internal tables
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* ABAP cheat sheet: ABAP object orientation
|
||||
*
|
||||
* -------------------------- PURPOSE ----------------------------------
|
||||
* - Example to demonstrate various syntactical options and concepts related
|
||||
* - Example to demonstrate various syntax options and concepts related
|
||||
* to ABAP object orientation.
|
||||
* - Topics covered: Working with objects and components, method redefinition
|
||||
* in inheritance, working with interfaces, upcast and downcast, concepts
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* ABAP cheat sheet: ABAP SQL
|
||||
*
|
||||
* -------------------------- PURPOSE ----------------------------------
|
||||
* - Example to demonstrate various syntactical options for working with
|
||||
* - Example to demonstrate various syntax options for working with
|
||||
* persisted data in database tables using ABAP SQL.
|
||||
* - Topics covered: reading from database tables using SELECT, changing
|
||||
* data in database tables using INSERT, UPDATE, MODIFY and DELETE
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* ABAP cheat sheet: String processing
|
||||
*
|
||||
* -------------------------- PURPOSE ----------------------------------
|
||||
* - Example to demonstrate various syntactical options for processing
|
||||
* - Example to demonstrate various syntax options for processing
|
||||
* character strings.
|
||||
* - Topics covered: Creating strings and assigning values, chaining strings,
|
||||
* string templates, concatenating/splitting/modifying strings, searching
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* ABAP cheat sheet: Structures
|
||||
*
|
||||
* -------------------------- PURPOSE ----------------------------------
|
||||
* - Example to demonstrate various syntactical options for working with
|
||||
* - Example to demonstrate various syntax options for working with
|
||||
* structures.
|
||||
* - Topics covered: creating structures and structured types, variants
|
||||
* of structures, accessing components of structures, filling structures,
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
*
|
||||
* -------------------------- PURPOSE ----------------------------------
|
||||
* - Example that demonstrates working with XML and JSON in ABAP.
|
||||
* - The following topics are covered:
|
||||
* - Converting string <-> xstring
|
||||
* - The following topics are covered:*
|
||||
* - Processing XML using class libraries (iXML, sXML)
|
||||
* - XML Transformations using XSLT and Simple Transformations
|
||||
* - CALL TRANSFORMATION syntax
|
||||
* - Dealing with JSON data
|
||||
* - Excursions: Converting string <-> xstring, compressing and
|
||||
* decompressing binary data
|
||||
*
|
||||
* ----------------------- GETTING STARTED -----------------------------
|
||||
* - Open the class with the ABAP development tools for Eclipse (ADT).
|
||||
@@ -47,7 +48,10 @@ CLASS zcl_demo_abap_xml_json DEFINITION
|
||||
CREATE PUBLIC .
|
||||
|
||||
PUBLIC SECTION.
|
||||
INTERFACES if_oo_adt_classrun.
|
||||
INTERFACES: if_oo_adt_classrun,
|
||||
"This interface is implemented for serializing and
|
||||
"deserialzing instances of classes (objects).
|
||||
if_serializable_object.
|
||||
CLASS-METHODS class_constructor.
|
||||
|
||||
PROTECTED SECTION.
|
||||
@@ -59,6 +63,26 @@ CLASS zcl_demo_abap_xml_json DEFINITION
|
||||
TYPES c50_tab_type TYPE TABLE OF c50 WITH EMPTY KEY.
|
||||
TYPES x30 TYPE x LENGTH 30.
|
||||
TYPES x30_tab_type TYPE TABLE OF x30 WITH EMPTY KEY.
|
||||
|
||||
"The following instance attribues and methods are used for serializing and
|
||||
"deserialzing instances of classes (objects)
|
||||
DATA: attr_string_a TYPE string,
|
||||
attr_string_b TYPE string,
|
||||
attr_concat_string TYPE string,
|
||||
attr_lowercase_str TYPE string.
|
||||
METHODS: concatenate_string,
|
||||
lowercase_string,
|
||||
"The following method can only have output parameters.
|
||||
"For each output parameter of the serialize_helper method, you must specify
|
||||
"an identically named input parameter of the deserialize_helper method
|
||||
"with the same type.
|
||||
serialize_helper EXPORTING attr_string_a TYPE string
|
||||
attr_string_b TYPE string
|
||||
attr_concat_string TYPE string,
|
||||
"This method can only have input parameters.
|
||||
deserialize_helper IMPORTING attr_string_a TYPE string
|
||||
attr_string_b TYPE string
|
||||
attr_concat_string TYPE string.
|
||||
ENDCLASS.
|
||||
|
||||
|
||||
@@ -67,7 +91,7 @@ CLASS zcl_demo_abap_xml_json IMPLEMENTATION.
|
||||
|
||||
METHOD if_oo_adt_classrun~main.
|
||||
|
||||
out->write( |ABAP Cheat Sheet Example: XML\n\n| ).
|
||||
out->write( |ABAP Cheat Sheet Example: Working with XML and JSON in ABAP\n\n| ).
|
||||
out->write( |1) Excursion: Converting string <-> xstring| ).
|
||||
"In the following examples, many operations are performed using binary data.
|
||||
"This excursion shows the conversion of string to xstring and the other way round
|
||||
@@ -869,9 +893,7 @@ CLASS zcl_demo_abap_xml_json IMPLEMENTATION.
|
||||
|
||||
************************************************************************
|
||||
|
||||
out->write( zcl_demo_abap_aux=>heading( `17) References` ) ).
|
||||
"The example demonstrates a data reference. For (serializable) object references,
|
||||
"check the ABAP Keyword Documentation.
|
||||
out->write( zcl_demo_abap_aux=>heading( `17) Data References` ) ).
|
||||
|
||||
DATA(dref_a) = NEW i( 123 ).
|
||||
|
||||
@@ -896,7 +918,75 @@ CLASS zcl_demo_abap_xml_json IMPLEMENTATION.
|
||||
|
||||
************************************************************************
|
||||
|
||||
out->write( zcl_demo_abap_aux=>heading( `18) CALL TRANSFORMATION Syntax: Specifying Transformations` ) ).
|
||||
out->write( zcl_demo_abap_aux=>heading( `18) Object References` ) ).
|
||||
"The following example demonstrates the serialization and deserialization of
|
||||
"instances of classes (objects). For example, to serialize instance attributes,
|
||||
"classes must implement the if_serializable_object interface. By default, all instance
|
||||
"attributes of an object are serialized, regardless of their visibility section.
|
||||
"However, you can change this behavior using the serialize_helper and deserialize_helper
|
||||
"instance methods. As a result of the transformation, you get an asXML representation of
|
||||
"the object.
|
||||
"The example is implemented as follows:
|
||||
"- The class implements the if_serializable_object interface.
|
||||
"- There are 4 instance attributes of type string.
|
||||
"- There are two instance methods:
|
||||
" - One method concatenates two of the strings and assigns the resulting string to
|
||||
" another string.
|
||||
" - Another method concatenates two strings and converts the string to lowercase.
|
||||
" The result is assigned to another string.
|
||||
"- Serialization preserves instance attribute values in the asXML representation of the
|
||||
" object.
|
||||
"- During deserialization, the instance attribute values are transformed back and can
|
||||
" be accessed.
|
||||
"- The example includes the implementation of the serialize_helper and deserialize_helper
|
||||
" instance methods. Without implementation, all instance attributes would be
|
||||
" serialized/deserialized. The sample implementation limits serialization/deserialization.
|
||||
" The fourth instance attribute, which is converted to a lowercase string when calling the
|
||||
" method, is not part of the serialization/deserialization. See the implementation of the
|
||||
" serialize_helper and deserialize_helper instance methods. They explicitly specify what
|
||||
" to serialize and deserialize.
|
||||
"- Note: For each output parameter of the serialize_helper method, you must specify an
|
||||
" identically-named input parameter of the deserialize_helper method. The parameters must
|
||||
" have the same type.
|
||||
|
||||
DATA(oref_a) = NEW zcl_demo_abap_xml_json( ).
|
||||
oref_a->attr_string_a = `AB`.
|
||||
oref_a->attr_string_b = `AP`.
|
||||
oref_a->concatenate_string( ).
|
||||
oref_a->lowercase_string( ).
|
||||
|
||||
out->write( `Value of instance attribute attr_lowercase_str for the created instance (before serialization/deserialization):` ).
|
||||
out->write( oref_a->attr_lowercase_str ).
|
||||
out->write( |\n| ).
|
||||
|
||||
"ABAP -> XML
|
||||
DATA xml_oref_a TYPE xstring.
|
||||
CALL TRANSFORMATION id SOURCE oref = oref_a
|
||||
RESULT XML xml_oref_a.
|
||||
|
||||
DATA(conv_xml_oref_a) = cl_abap_conv_codepage=>create_in( )->convert( xml_oref_a ).
|
||||
|
||||
out->write( `ABAP -> XML` ).
|
||||
out->write( format( conv_xml_oref_a ) ).
|
||||
out->write( |\n| ).
|
||||
|
||||
"XML -> ABAP
|
||||
DATA oref_b LIKE oref_a.
|
||||
CALL TRANSFORMATION id SOURCE XML xml_oref_a
|
||||
RESULT oref = oref_b.
|
||||
|
||||
out->write( `XML -> ABAP` ).
|
||||
out->write( oref_b->attr_string_a ).
|
||||
out->write( oref_b->attr_string_b ).
|
||||
out->write( oref_b->attr_concat_string ).
|
||||
out->write( |\n| ).
|
||||
IF oref_b->attr_lowercase_str IS INITIAL.
|
||||
out->write( `The instance attribute attr_lowercase_str is initial. The serialization/deserialization is restricted.` ).
|
||||
ENDIF.
|
||||
|
||||
************************************************************************
|
||||
|
||||
out->write( zcl_demo_abap_aux=>heading( `19) CALL TRANSFORMATION Syntax: Specifying Transformations` ) ).
|
||||
"As already covered in the examples above, transformations are specified after
|
||||
"CALL TRANSFORMATION. They are either ...
|
||||
|
||||
@@ -931,7 +1021,7 @@ CLASS zcl_demo_abap_xml_json IMPLEMENTATION.
|
||||
|
||||
************************************************************************
|
||||
|
||||
out->write( zcl_demo_abap_aux=>heading( `19) CALL TRANSFORMATION Syntax: Sources of Transformations` ) ).
|
||||
out->write( zcl_demo_abap_aux=>heading( `20) CALL TRANSFORMATION Syntax: Sources of Transformations` ) ).
|
||||
"The following examples use the predefined identity transformation ID.
|
||||
"The result is asXML data and stored in a variable of type xstring.
|
||||
"Multiple options and variants are possible. The examples cover a selection.
|
||||
@@ -1060,7 +1150,7 @@ CLASS zcl_demo_abap_xml_json IMPLEMENTATION.
|
||||
|
||||
*************************************************************************
|
||||
|
||||
out->write( zcl_demo_abap_aux=>heading( `20) CALL TRANSFORMATION Syntax: Results of Transformations` ) ).
|
||||
out->write( zcl_demo_abap_aux=>heading( `21) CALL TRANSFORMATION Syntax: Results of Transformations` ) ).
|
||||
"As in the examples above, the predefined identity transformation is used here.
|
||||
|
||||
"Creating demo XML data to be used in the example as the source.
|
||||
@@ -1171,7 +1261,7 @@ CLASS zcl_demo_abap_xml_json IMPLEMENTATION.
|
||||
|
||||
************************************************************************
|
||||
|
||||
out->write( zcl_demo_abap_aux=>heading( `21) Dealing with JSON Data` ) ).
|
||||
out->write( zcl_demo_abap_aux=>heading( `22) Dealing with JSON Data` ) ).
|
||||
"Note: When the identity transformation ID is used, the format is asJSON.
|
||||
|
||||
"Elementary type
|
||||
@@ -1336,6 +1426,45 @@ CLASS zcl_demo_abap_xml_json IMPLEMENTATION.
|
||||
DATA(xml2json_sxml) = cl_abap_conv_codepage=>create_in( )->convert( json_wr->get_output( ) ).
|
||||
out->write( `XML -> JSON using sXML` ).
|
||||
out->write( xml2json_sxml ).
|
||||
|
||||
************************************************************************
|
||||
|
||||
out->write( zcl_demo_abap_aux=>heading( `23) Excursion: Compressing and Decompressing Binary Data` ) ).
|
||||
"You may want to process or store binary data. The data can be very large.
|
||||
"You can compress the data in gzip format and decompress it for further processing using
|
||||
"the cl_abap_gzip class. Check out appropriate exceptions to be caught. The simple example
|
||||
"just specifies cx_root. See the class documentation for more information.
|
||||
"This example uses a data object of type xstring from a previous example.
|
||||
|
||||
"Compressing binary data
|
||||
DATA xstr_comp TYPE xstring.
|
||||
TRY.
|
||||
cl_abap_gzip=>compress_binary( EXPORTING raw_in = xml_oref_a
|
||||
IMPORTING gzip_out = xstr_comp ).
|
||||
CATCH cx_root INTO DATA(error_comp).
|
||||
out->write( error_comp->get_text( ) ).
|
||||
ENDTRY.
|
||||
|
||||
"Decompressing binary data
|
||||
DATA xstr_decomp TYPE xstring.
|
||||
TRY.
|
||||
cl_abap_gzip=>decompress_binary( EXPORTING gzip_in = xstr_comp
|
||||
IMPORTING raw_out = xstr_decomp ).
|
||||
|
||||
CATCH cx_root INTO DATA(error_decomp).
|
||||
out->write( error_decomp->get_text( ) ).
|
||||
ENDTRY.
|
||||
|
||||
"Checking the xstring length of the variables used and comparing result
|
||||
DATA(strlen_original_xstring) = xstrlen( xml_oref_a ).
|
||||
out->write( |Length of original binary data object: { strlen_original_xstring }| ).
|
||||
DATA(strlen_comp) = xstrlen( xstr_comp ).
|
||||
out->write( |Length of compressed binary data object: { strlen_comp }| ).
|
||||
DATA(strlen_decomp) = xstrlen( xstr_decomp ).
|
||||
out->write( |Length of decompressed binary data object: { strlen_decomp }| ).
|
||||
IF xml_oref_a = xstr_decomp.
|
||||
out->write( `The decompressed binary data object has the same value as the original binary data object.` ).
|
||||
ENDIF.
|
||||
ENDMETHOD.
|
||||
METHOD format.
|
||||
TRY.
|
||||
@@ -1359,4 +1488,24 @@ CLASS zcl_demo_abap_xml_json IMPLEMENTATION.
|
||||
zcl_demo_abap_aux=>fill_dbtabs( ).
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
METHOD concatenate_string.
|
||||
attr_concat_string = attr_string_a && attr_string_b.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD deserialize_helper.
|
||||
me->attr_string_a = attr_string_a.
|
||||
me->attr_string_b = attr_string_b.
|
||||
me->attr_concat_string = attr_concat_string.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD serialize_helper.
|
||||
attr_string_a = me->attr_string_a.
|
||||
attr_string_b = me->attr_string_b.
|
||||
attr_concat_string = me->attr_concat_string.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD lowercase_string.
|
||||
attr_lowercase_str = to_lower( attr_string_a && attr_string_b ).
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
Reference in New Issue
Block a user