|Home • Trainings •Quiz • Tips • Tutorials • Functional • Cert Q's • Interview Q's • Jobs • Testimonials • Advertise • Contact Us|
Guide towards a simple conversion of an XML file to ABAP Internal table, using XML parsing
By Aastha Mehrotra, Larsen & Toubro Infotech
SAP ECC 6.0. And further.
This article elaborates the conversion process of an XML file residing in the SAP Application server, into an ABAP internal table.
An effortless approach, unlike the other intricate methods available for conversion, has been presented here. In this particular concept, initially, the XML file is converted into a string. Hence, we use the XML parsing technique to convert that string into an ‘x’ string and finally into an internal table.
A simple conversion idea, along with the supporting code blocks, has been demonstrated in this document.
Various practices and procedures are available for XML-ABAP conversion. However, I discovered that, none of those takes us entirely across the process of converting an XML residing in the Application Server into an internal table in ABAP. Certain other methods, which might actually take us across, are difficult to comprehend and involve a number of complicated steps.
I used this method while working on an object which required me to
This document shall let you know the entire process involving the evolution of the XML file in the Application server to an Internal Table.
The XML file
Following is an archetype of the XML file which needs to be converted into an internal table in ABAP.
This XML file carries data for three fields- HomePernr, UNAME and USERID. We need to convert the XML file into an internal table comprising of these three fields, and holding the three records from XML.
We would be considering the example of the following file through the course of this document. This would assist us convert any XML containing any number of fields and records into an internal table, in an uncomplicated manner.
Note: FileFeed happens to be the root node for the XML.
EmployeeRequests is the element node containing the separate Employee records within it.
Employeerequest is the element node for each record.
HomePERNR, UNAME and USERID are the value nodes containing the values for the records.
Step wise conversion of the XML to Internal Table
The entire mechanism would be elaborated by this document in several steps, supported with the appropriate code snippets.
The series of execution would be as follows:
Note: The code snippets are actually parts of a single code, in a sequence. Hence, these can be conjoined together to achieve the entire code for XML_ABAP conversion.
We shall advance with the specification of the step wise
conversion process. However, we must be aware of the variables to be declared,
in advance. This would enhance our understanding of the steps and would provide
clarity to the context.
Type declarations for the variables
Following are the declarations for all the variables to be
used in the code snippets throughout the document. This would help us avoid all
the confusion pertaining to the type of variables used.
Declaring the file type
Declaring the structure for the XML internal table
TYPES: BEGIN OF ty_xml,
raw(2000) TYPE c,
END OF ty_xml.
Declaring the XML internal table
TYPE TABLE OF ty_xml INITIAL SIZE 0.
Declaring the work area for the XML internal table
Declaring the string to contain the data for the XML internal table
Declaring the string to contain x string
Declaring the table to contain the parsed data
TYPE TABLE OF smum_xmltb INITIAL SIZE 0.
Declaring the work area for the internal table containing the parsed data
LIKE LINE OF g_t_xml_info.
Declaring the table to contain the returned messages from the parsing FM
TYPE STANDARD TABLE OF bapiret2.
Declaring the work area for the return table
LIKE LINE OF g_t_return.
Declaring the structure for the table containing fields in the XML file
TYPES: BEGIN OF struc_people,
Uname(4) TYPE c,
END OF struc_people.
Declaring the internal table containing the fields in the XML file
TYPE TABLE OF struc_people.
Declaring the work area for the internal table containing the fields in
DATA: g_s_employeerequest LIKE LINE OF g_t_employeerequest.
1. Open the XML to read data
The first breakthrough would
be to open the XML file and read the data using the OPEN DATASET statement.
The addition IN TEXT MODE, to
the OPEN DATASET opens the file as a text file. The addition ENCODING defines
how the characters are represented in the text file. While writing the data into
a text file, the content of a data object is converted to the representation
entered after ENCODING, and transferred to the file.
* Open the XML file for reading data
OPEN DATASET g_unixfilename FOR INPUT IN
TEXT MODE ENCODING DEFAULT.
sy-subrc NE 0.
MESSAGE ‘Error opening the XML file’ TYPE ‘E’.
2. Transfer the contents into an internal table of a specific type.
The next furtherance would be to move the contents of the file to an internal table. Following is the piece of code which would help us Read the data from the file into an internal table.
We use the READ DATASET statement to read the data from the file.
* Transfer the contents from the file to the
work area of the internal table
READ DATASET g_unixfilename INTO wa_xml_tab.
IF sy-subrc EQ 0.
Append the contents of the work area to the internal table
APPEND wa_xml_tab TO g_t_xml_tab.
* Close the file after reading the data
Concatenate the lines of
the internal table into a string.
Next, we move the lines of the internal table into a string. Hence, we get a string containing the data of the entire XML file.
*Transfer the contents from the internal
table to a string
IF NOT g_t_xml_tab IS INITIAL.
CONCATENATE LINES OF g_t_xml_tab INTO g_str SEPARATED BY space.
4. Converting the normal string into an ‘X’ string.
We need to convert the string thus formed into a ‘x’
string or a hexadecimal string using the function Module 'SCMS_STRING_TO_XSTRING'.The
type xstring allows a hexadecimal display of byte chains instead of the
presentation to the base of 64.
* The function module is used to convert
string to xstring
text = g_str
buffer = g_xmldata
failed = 1
OTHERS = 2.
MESSAGE ‘Error in the XML file’ TYPE ‘E’.
Please send us your feedback/suggestions at webmaster@SAPTechnical.COM
©2006-2007 SAPTechnical.COM. All rights reserved.
product names are trademarks of their respective companies. SAPTechnical.COM
is in no way affiliated with SAP AG.
Graphic Design by Round the Bend Wizards