12-09-2013 11:46 AM
<DatabaseExportRequestType xmlns="http://cmsg2.vf.com/CMSG2Schema/V9">
</CurriculumSetup>
</TransactionMetaData>
</Export>
</TrainingSetup>
</TransactionMetaData>
</Export>
</IndividualAddress>
</IndividualAddress>
12-10-2013 11:24 AM
Hello,
The best option in my opinion is to use XSLT TRANSFORMATION.
http://help.sap.com/abapdocu_70/en/ABAPCALL_TRANSFORMATION.htm
Best Regards
Yassir
12-10-2013 11:33 AM
Hello,
Follow below steps:
The series of execution would be as follows:
- Open the XML in order to read the data.
- Transfer the contents into an internal table.
- Concatenate the lines of the internal table into a string.
- Convert the string into a hexadecimal string - The ‘X’ string.
- Parse the ‘x’ string to convert the data into an XML table.
- Read the XML table to transfer the data into the table with the required structure.
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.
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
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.
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.
IF sy-subrc NE 0.
MESSAGE ‘Error opening the XML file’ TYPE ‘E’.
ELSE.
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.
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.
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.
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
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.
In case we have an XML string that needs to be converted into an object, then the XML string needs to be parsed. The parsing would convert the string into a table. The internal table that would be returned is g_t_xml_info. G_t_xml_info is of type SMUM_XML_TB. The structure for SMUM_XML_TB is in the screen-shot below:
The table SMUM_XML_TB has four fields: HIER, TYPE, CNAME and CVALUE. The XML data would be transferred into the four fields.
Moving forward, we would come to know that how exactly is the data allocated to the fields of this table.
* This function module is used to parse the XML and get the
* data in the form of a table
CALL FUNCTION 'SMUM_XML_PARSE'
EXPORTING
xml_input = g_xmldata
TABLES
xml_table = g_t_xml_info
return = g_t_return
EXCEPTIONS
OTHERS = 0.
*"If XML parsing is not successful, return table will contain error messages
READ TABLE g_t_return INTO wa_return WITH KEY type = 'E'.
IF sy-subrc EQ 0.
MESSAGE ‘Error converting the input XML file’ TYPE ‘E’.
ELSE.
REFRESH g_t_return.
ENDIF.
We are acquainted with the fields of table g_t_xml_info HIER, TYPE, CNAME and CVALUE.
We now, need to move the data from this table into the required, final table. For instance in the scenario we have been considering, the final table would consist of the three fields HomePernr, UNAME and USERID. The transfer of data from the table g_t_xml_info to our final table would be done in accordance with the values held by the four fields of the g_t_xml_info table.
While parsing, the values are assigned to the table g_t_xml_info. The values are moved depending upon the node being converted to the table.
Let me elaborate about the values held by the table g_t_xml_info.
The field ‘Hier’ would hold the value ‘1’ for the element in the root node. Since, Root node is the first level of the XML hierarchy.
‘Hier’ would hold the value ‘2’ for the fields in the Element node, which is at the second level of hierarchy. In our example EmployeeRequests is at the second level of hierarchy.
It would hold ‘3’ for Element nodes at the third level of hierarchy in the XML file that is EmployeeRequest here.
The value would be ‘4’ for all the Value nodes. Here the value nodes would be HomePernr, UNAME and USERID.
Hence, we conclude that the value of ‘Hier’ depends upon the level of hierarchy that particular node holds in the XML file.
The value that the field ‘Type’ holds for all the elements in the Header of the XML file would be ‘A’. It would be ‘V’ for the value nodes. For all the other nodes, it would hold a blank.
Cname contains the names of the nodes. Hence, considering our example here, if the Hier is ‘2’ the Type would be blank the Cname would be ‘EmployeeRequests’ and the Cvalue would be blank since EmployeeRequests holds no value.
Cvalue contains the values held by the elements of the various nodes of an XML file. Taking an example of our case here-
For HIER ‘4’ the Type would be ‘V’, Cname can be ‘HomePernr’, ‘UNAME’ or ‘USERID’ and the Cvalue would be the values held by these records.
Here is a screen-shot of the values contained in the table g_t_xml_info, considering our example.
Now that we are acquainted with the pattern of values contained in g_t_xml_info, we can move these values to the required structure.
We require only the values contained in the value nodes and we have to transfer these to their respective fields i.e. HomePernr, UNAME and USERID. Since, we need the values only for the value nodes we can directly move the values in the final table for a value of HIER equal to ‘3’.
Note: The value of HIER for which we need the data, can be manipulated in accordance with the position of the value nodes in the hierarchy of XML.
We use the following code snippet to move the data:
* Moving the data from the g_t_xml_info table to the required table
IF NOT g_t_xml_info IS INITIAL.
LOOP AT g_t_xml_info INTO g_s_xml_info WHERE hier EQ 3.
tabix = sy-tabix + 1.
READ TABLE g_t_xml_info INTO g_s_xml_info INDEX tabix.
g_s_employeerequest-homepernr = g_s_xml_info-cvalue.
tabix = tabix + 1.
READ TABLE g_t_xml_info INTO g_s_xml_info INDEX tabix.
g_s_employeerequest-Uname = g_s_xml_info-cvalue.
tabix = tabix + 1.
READ TABLE g_t_xml_info INTO g_s_xml_info INDEX tabix.
g_s_employeerequest-Userid = g_s_xml_info-cvalue.
APPEND g_s_employeerequest TO g_t_employeerequest.
ENDLOOP.
Here, the value ‘3’ for the field ‘Hier’ marks the beginning of a new record. Hence, we move the values to the g_t_employeerequest table whenever the value for ‘Hier’ is ‘3’.
This way we get the required values in the internal table g_t_employeerequest.
Thus, we get the values in our desired structure. We populated the final table in accordance with the location