Abap data parser – open source TAB-delimited text parser
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):
NAME BIRTHDATE
ALEX 01.01.1990
JOHN 02.02.1995
LARA 03.03.2000
… 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 ! =)
Hi,
What is the earliest release that can use this class ?
Regards
It should be 7.02. At least it was the target - I didn't try honestly - all accessible systems had at least 7.31.
Among comparably new feature it uses just string templates. But in case of incompatibilities please post an issue at github.
Thanks for sharing.
It might be useful.
I've reviewed the source code (Haven't tried it yet) and I think that for the internal implementation of conversion from external value to internal value (method parse_field) you may use FM RS_CONV_EX_2_IN.
Thanks for the ref. =)
I'll check it. From the first glance however it relies on currency settings which is less universal. But I'll check, thanks !
Do you mean Decimal notation?
Well, you can always the preliminary conversion trick:
CASE dcpfm.
WHEN ''.
TRANSLATE input USING '. '.
TRANSLATE input USING ',.'.
WHEN 'Y'.
TRANSLATE input USING ',.'.
WHEN 'X'.
TRANSLATE input USING ', '.
ENDCASE.
CONDENSE input NO-GAPS.
From what I saw this FM requires reference to real table/structure field which is not always dynamically available. I think I'll stay with the current code - it is more flexible and allows completely custom structures. But anyway thanks for the tip =)
Your'e welcome.
You are right. FM RS_CONV_EX_2_IN does require table/structure field, I've meant FM RS_CONV_EX_2_IN_NO_DD (From the same family 😆 ).