Where the TRANSFORMATION
delivers such great results in so little steps, the CL_XML_DOCUMENT
and CL_IXML
classes could not satisfy my simple requirements to read/interpret an XML file. So I created my own.
This article describes how a simple XML file can be made available in Abap logic in a workable manner. An example XML file:
<?xml version="1.0" encoding="utf-16"?> <report name="ZABAPCADABRA_XML_PARSER"> <attributes> <title>Simple XML parser</title> <author date="09.10.2015" userid="WMAASDAM">Wim Maasdam</author> <description language="EN">An example local class implementation that enables</description> <description language="EN">access to an XML file</description> </attributes> <abapcoding> <line>CLASS lcl_xml_node DEFINITION.</line> <line> PUBLIC SECTION.</line> <line> types: begin of ty_tagged_value,</line> <line> tag type c length 50,</line> <line> val type c length 150,</line> <line> end of ty_tagged_value,</line> <line> ty_tagged_values type STANDARD TABLE OF ty_tagged_value.</line> <line> data: gw_element type ty_tagged_value,</line> <line> gt_attributes type ty_tagged_values.</line> <line>ENDCLASS.</line> </abapcoding> </report>
The idea is simple: XML is popular because it is easy to understand yet very versatile. The local class that you can implement it in your own coding quite easily. As shown in the above example XML file, attributes and tables are supported. The lcl_easy_xml
class will read a local xml file and parse it in an easily accessable manner. Create the object, upload a file which will be parsed. From the object the attribute gt_xml
will be available, holding an entry for each node on the xml file. Each node can also have multiple attributes, which will be available as tag/value pairs.
The report output shown here shows the output extracted from the lcl_easy_xml
class, which demonstrates how the n-depth of xml content is made available.
Download the example and implement the local classes in your own coding. Add processing methods where you would like them.
Guide to using the easy xml local class
First of all you will need to incorporate 2 local classes in your abap source coding. Then you will need to define a variabele that holds the xml source and create an instance of lcl_easy_xml
. This will look something like this:
data: go_myxml type ref to lcl_easy_xml.
create object go_myxml.
To upload an xml source file simply use go_myxml->upload( 'c:\myfile.xml')
. If your xml source is not a local file, simply set the class attributes on the local class directly and call method parse( )
to prepare the class.
The xml content is parsed into the attribute gt_XML
which is freely accessible on the object. Work areas to access this information are also available on the class, gw_xml_node
, so the content of the gt_XML
attribute can easily be accessed. Alternatively there are a few methods available:
findfirst( tag )
will return the first an instance of a node, as local classfindnext()
as above, but it will search the next node in the same node-path. Returns nothing if no next node is availableattribute( name )
returns the value of the attribute name, or nothing if no value or attribute is available
The example report that is available as download also demonstrate the simple setup of the 3 methods above. In effect, with the exexution of each method a few attributes on the class will be set. Look out for gw_xml_node
, which holds the mose recently processed node and gv_result_found
which indicates whether the last action (or method call) actually produced results. Thus the following bit of coding demonstrates easy-access to the data on the XML file from above:
* Demonstration of the use of findfirst, findnext and attribute: write: /(30) 'The value of Title is', go_myxml->findfirst( 'title' ). write: /(30) 'More specifically', go_myxml->findfirst( 'report/attributes/title' ). write: /(30) 'The first line:', go_myxml->findfirst( 'report/abapcoding/line' ). while go_myxml->gv_result_found = abap_true. write: /(30) '', go_myxml->findnext( ). endwhile. * Focus the go_myxml->gw_xml_node attribute: go_myxml->findfirst( 'report/attributes/author' ). if go_myxml->gv_result_found = abap_true. write: / 'Author user ID is', go_myxml->attribute( 'userid' ). write: / 'And the author''s name:', go_myxml->gw_xml_node-node->gw_element-val. endif.
The output of the above:
The value of Title is Simple XML parser More specifically Simple XML parser The first line: CLASS lcl_xml_node DEFINITION. PUBLIC SECTION. types: begin of ty_tagged_value, tag type c length 50, val type c length 150, end of ty_tagged_value, ty_tagged_values type STANDARD TABLE OF ty_tagged_value. data: gw_element type ty_tagged_value, gt_attributes type ty_tagged_values. Author user ID is WMAASDAM And the author's name: Wim Maasdam