|
||||
| Home • Quiz • Tips • Tutorials • Functional • Cert Q's • Interview Q's • Jobs • Testimonials • Advertise • Contact Us | ||||
Document Categories:
What's New?
Contribute?Sample SpecsWhat's Hot? |
Guide towards a simple conversion of an XML file to ABAP Internal table, using XML parsingBy Aastha Mehrotra, Larsen & Toubro Infotech Applies to: SAP ECC 6.0. And further. Summary 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. Introduction 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 fileFollowing 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 TableThe 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 variablesFollowing 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 DATA: g_unixfilename
TYPE zpathfile.
"UNIX
file path. *
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 DATA: g_t_xml_tab
TYPE TABLE OF ty_xml INITIAL SIZE 0. *
Declaring the work area for the XML internal table DATA: wa_xml_tab
TYPE ty_xml. *
Declaring the string to contain the data for the XML internal table DATA: g_str
TYPE string. *
Declaring the string to contain x string DATA: g_xmldata
TYPE xstring. *
Declaring the table to contain the parsed data DATA: g_t_xml_info
TYPE TABLE OF smum_xmltb INITIAL SIZE 0. *
Declaring the work area for the internal table containing the parsed data DATA: g_s_xml_info
LIKE LINE OF g_t_xml_info. *
Declaring the table to contain the returned messages from the parsing FM DATA: g_t_return
TYPE STANDARD TABLE OF bapiret2. *
Declaring the work area for the return table DATA: wa_return
LIKE LINE OF g_t_return. *
Declaring the structure for the table containing fields in the XML file TYPES: BEGIN OF struc_people,
homepernr(8),
Uname(4) TYPE c,
Userid(32),
END OF struc_people. *
Declaring the internal table containing the fields in the XML file DATA: g_t_employeerequest
TYPE TABLE OF struc_people. *
Declaring the work area for the internal table containing the fields in
the *
XML file DATA: g_s_employeerequest LIKE LINE OF g_t_employeerequest. 1. Open the XML to read dataThe 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. IF
sy-subrc NE 0.
MESSAGE ‘Error opening the XML file’ TYPE ‘E’. ELSE. 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. continued........
DO. * 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.
CONDENSE wa_xml_tab. *
Append the contents of the work area to the internal table
APPEND wa_xml_tab TO g_t_xml_tab.
ELSE.
EXIT.
ENDIF.
ENDDO. ENDIF. * Close the file after reading the data CLOSE
DATASET g_unixfilename. 3.
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. continued........
*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. ENDIF. 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. continued........
* The function module is used to convert
string to xstring CALL
FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = g_str
IMPORTING
buffer = g_xmldata
EXCEPTIONS
failed = 1
OTHERS = 2. IF
sy-subrc<> 0.
MESSAGE ‘Error in the XML file’ TYPE ‘E’. ENDIF. |
|
||
|
Please send us your feedback/suggestions at webmaster@SAPTechnical.COM Home • Contribute • About Us • Privacy • Terms Of Use • Disclaimer • Safe • Companies: Advertise on SAPTechnical.COM | Post Job • Contact Us ©2006-2007 SAPTechnical.COM. All rights reserved. All
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 |
||||