Hi Community !I’d like to share a piece of code which might be useful for someone. It is called abap data parser. Its purpose is parsing of TAB-delimited text into an arbitrary flat structure or internal table. Why TAB-delimited? This is the format which is used automatically if you copy (clipboard) something from Excel – this creates some opportunities for good program usability.
So what does it do. Let’s say we have this data in a form of string (CRLF as a line delimiter, TAB as a field delimiter):
… and a corresponding data type and internal table.
types: begin of my_table_type, name type char10, birthdate type datum, end of my_table_type. data lt_container type my_table_type.
To parse the string into the container table just add the following code:
lcl_data_parser=>create( lt_container )->parse( exporting i_data = lv_some_string_with_data importing e_container = lt_container ).
The class supports some additional features, in particular, “unstrict mode” which allow to skip field of the target structure in text – useful when you need to load just several certain fields of a huge data structure (like standard tables in SAP). Let’s consider our data type has additional field, unnecessary in the current context:
types: begin of my_table_type, name type char10, city type char40, " << New field, but still just 2 in the text birthdate type datum, end of my_table_type. ... lcl_data_parser=>create( i_pattern = lt_container i_amount_format = ' .' " specify thousand and decimal delimiters )->parse( exporting i_data = lv_some_string_with_data i_strict = abap_false " missing city field will not throw an error i_has_head = abap_true " headers in the first line of the text importing e_container = lt_container ).
Another feature: i_has_head parameter above means that the first line contains tech names of the fields – then the parser uses it to identify existing fields and their order (which may be flexible then).
Cases of usage
- we (our company) use the code for some of our company’s products – like this one
- we use it in the mockup loader – another our openly published tool for unit testing (actually the data parser was a part of mockup loader initially)
- as a tool for mass uploads for some z-tables of some other our products
The code is free to use under MIT licence. Project home page is https://github.com/sbcgua/abap_data_parser
Installation can be done manually – just 1 include to to install – or with abapGit tool (the most convenient way).
I hope you find this useful ! =)