Posted: Sat Jan 31, 2009 7:41 pm Post subject: XML
Code:
REPORT zcrm_dd_program NO STANDARD PAGE HEADING.
TYPE-POOLS: slis.
TYPES: BEGIN OF ty_excel_field,
excel_index TYPE sy-index,
field_name TYPE string,
field_value TYPE string,
END OF ty_excel_field.
DATA: is_excel_field TYPE ty_excel_field.
DATA: it_excel_field TYPE TABLE OF ty_excel_field.
*Internal tables to hold table information
DATA: it_tables TYPE TABLE OF dd02v.
DATA: is_tables TYPE dd02v.
DATA: it_tabl_inf_tmp TYPE dd02v.
DATA: it_tabl_technics TYPE TABLE OF dd09l.
DATA: is_tabl_technics TYPE dd09l.
DATA: is_tabl_technics_tmp TYPE dd09l.
DATA: it_tabl_fields TYPE comt_gen_dd03p.
DATA: it_tabl_fields_tmp TYPE comt_gen_dd03p.
DATA: is_tabl_fields TYPE dd03p.
*Internal tables to hold domain information
DATA: it_domains TYPE TABLE OF dd01v.
DATA: is_domains TYPE dd01v.
DATA: is_domains_tmp TYPE dd01v.
DATA: it_doma_values TYPE comt_gen_dd07v.
DATA: it_doma_values_tmp TYPE comt_gen_dd07v.
DATA: is_doma_values TYPE dd07v.
*Internal tables to hold data element information
DATA: it_dataelements TYPE TABLE OF dd04v.
DATA: is_dataelements TYPE dd04v.
DATA: is_dataelements_tmp TYPE dd04v.
*Internal table to hold messages
TYPES: BEGIN OF ty_messages,
mandt TYPE sy-mandt,
msgid TYPE sy-msgid,
msgty TYPE sy-msgty,
msgno TYPE sy-msgno,
msgv1 TYPE sy-msgv1,
msgv2 TYPE sy-msgv2,
msgv3 TYPE sy-msgv3,
msgv4 TYPE sy-msgv4,
text TYPE t100-text,
END OF ty_messages.
DATA: is_messages TYPE ty_messages.
DATA: it_messages TYPE TABLE OF ty_messages.
*Variables to hold message information
DATA: v_msgid TYPE sy-msgid,
v_msgty TYPE sy-msgty,
v_msgno TYPE sy-msgno,
v_msgv1 TYPE sy-msgv1,
v_msgv2 TYPE sy-msgv2,
v_msgv3 TYPE sy-msgv3,
v_msgv4 TYPE sy-msgv4,
v_text TYPE t100-text.
*Variables
DATA: line(100) TYPE c.
DATA: v_table_name_check TYPE tbatg-tabname.
DATA: v_ext TYPE ddrefstruc-flag.
DATA: v_create(1) TYPE c.
*Field symbols
FIELD-SYMBOLS:<fs_value> TYPE ANY.
*Constants
CONSTANTS: c_x(1) VALUE 'X',
c_space(1) VALUE '',
c_yes(1) VALUE 'Y'.
DATA: v_exist(1) TYPE c.
*---------------------------------------------------------------------*
*End of Data declarations for table maintenance generator
*---------------------------------------------------------------------*
DATA: v_current_cell_index TYPE sy-index.
DATA: v_merged_cells TYPE sy-index.
DATA: v_an_integer_string TYPE string.
DATA: v_an_int TYPE i.
*EXCEL XML file name
DATA: v_rlgrap_excel_xml_file TYPE string.
*Main iXML factory
DATA: g_ixml TYPE REF TO if_ixml.
*XML data bytes size
DATA: v_xml_code_bytes TYPE i.
*XML data
DATA: v_xml_data(500) OCCURS 0.
*Stream factory.
DATA: g_streamfactory TYPE REF TO if_ixml_stream_factory.
*XML istream
DATA: g_istream TYPE REF TO if_ixml_istream.
*XML parser
DATA: g_parser TYPE REF TO if_ixml_parser.
*XML DOM object
DATA: g_obj_dom_xml TYPE REF TO if_ixml_document.
*EXCEL root element
DATA: g_obj_excel_workbook TYPE REF TO if_ixml_element.
*Excel XML workbook name
DATA: g_excel_workbook_name TYPE string.
*To hold the all the Worksheet under the Excel's Workbook element
DATA: g_obj_worksheet_collections TYPE REF TO if_ixml_node_collection.
*To hold the iterator for the Worksheet elements collection
DATA: g_obj_worksheet_itr TYPE REF TO if_ixml_node_iterator.
*To hold a Worksheet node.
DATA: v_obj_a_worksheet_node TYPE REF TO if_ixml_node.
*To hold a Worksheet element.
DATA: v_obj_a_worksheet_element TYPE REF TO if_ixml_element.
*To hold the Worksheet name
DATA: v_worksheet_ss_name TYPE string.
*To hold a Worksheet/Table element.
DATA: v_obj_a_table_element TYPE REF TO if_ixml_element.
*To hold the all the Row elements under the Table element
DATA: v_obj_row_collections TYPE REF TO if_ixml_node_collection.
*To hold the iterator for the Row elements collection
DATA: v_obj_row_itr TYPE REF TO if_ixml_node_iterator.
*To hold a Row node.
DATA: v_obj_a_row_node TYPE REF TO if_ixml_node.
*To hold a Row element.
DATA: v_obj_a_row_element TYPE REF TO if_ixml_element.
*To hold the all the Cell elements under the Row element
DATA: v_obj_cell_collections TYPE REF TO if_ixml_node_collection.
*To hold the iterator for the Cell elements collection
DATA: v_obj_cell_itr TYPE REF TO if_ixml_node_iterator.
*To hold a Cell node.
DATA: v_obj_a_cell_node TYPE REF TO if_ixml_node.
*To hold a Cell element.
DATA: v_obj_a_cell_element TYPE REF TO if_ixml_element.
*To hold a Data element.
DATA: v_obj_a_data_element TYPE REF TO if_ixml_element.
* To hold text nodes
DATA: v_obj_text_lst TYPE REF TO if_ixml_node_list.
DATA: v_obj_text_itr TYPE REF TO if_ixml_node_iterator.
* To hold a text value
DATA: v_txt_val TYPE string.
*To hold a text node.
DATA: v_obj_a_txt_node TYPE REF TO if_ixml_node.
*Type casting
DATA: g_cast TYPE REF TO if_ixml_unknown.
*To hold the node type
DATA: v_txt_type TYPE i.
DATA: v_subrc TYPE i.
**************SELECTION-SCREEN*****************************
*Input file name
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETER: p_file TYPE rlgrap-filename.
SELECTION-SCREEN END OF BLOCK b1.
*Browse for input file
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM f001_file_value_request USING p_file.
*---------------------------------------------------------------------*
*FORM F001_FILE_FOR_VALUE_REQUEST
*---------------------------------------------------------------------*
FORM f001_file_value_request USING file TYPE rlgrap-filename.
*----------------------------------------------------------------------*
*FORM F004_GET_FILE_DATA
*----------------------------------------------------------------------*
*Before proceeding further, the developers must understand completely
*the structure of an XML file and a few terms associated with the XML.
*XML Version :
*Workbook :
*Worksheet :
*Table :
*Column :
*Row :
*Cell :
*Data :
*----------------------------------------------------------------------*
*STEP 1: Read the XML file data into V_XML_CODE
* The output in V_XML_CODE will be the complete XML raw data
* which is not understandable manually.
*----------------------------------------------------------------------*
FORM f004_get_file_data.
*Convert to right case
v_rlgrap_excel_xml_file = p_file.
IF sy-subrc <> 0.
* Cannot open the Presentation Server file.
WRITE: / 'Cannot open/read the Presentation Server file',
v_rlgrap_excel_xml_file.
EXIT.
ENDIF.
*---------------------------------------------------------------------*
*STEP 2:Initialize the EXCEL DOM object and get
* the root element.
*---------------------------------------------------------------------*
*CL_IXML is the standard class provided by SAP to handle
*any XML applications.
* Load ixml library
CLASS cl_ixml DEFINITION LOAD.
*Create the main factory for the iXML library. G_IXML is exactly
*similar to the class CL_IXML.
*Note that CREATE is a static method, hence we use '=>'
g_ixml = cl_ixml=>create( ).
* Create a main stream factory.
CALL METHOD g_ixml->create_stream_factory
RECEIVING
rval = g_streamfactory.
* Create a stream for the itab containg XML raw content.
CALL METHOD g_streamfactory->create_istream_itable
EXPORTING
size = v_xml_code_bytes
table = v_xml_data[]
RECEIVING
rval = g_istream.
* Create a new DOM object.
CALL METHOD g_ixml->create_document
RECEIVING
rval = g_obj_dom_xml.
* Parse the XML source.
CALL METHOD g_parser->parse
RECEIVING
rval = v_subrc.
IF v_subrc <> 0.
* Parsing failed for XML file, Make sure valid XML is found.
WRITE: / 'Parsing failed for XML file, Make sure valid ' &
'XML is found.'.
EXIT.
ENDIF.
* close the opened stream and thereby the opened xml file.
CALL METHOD g_istream->close.
* Get the root element of the Excel XML.
CALL METHOD g_obj_dom_xml->get_root_element
RECEIVING
rval = g_obj_excel_workbook.
IF g_obj_excel_workbook IS INITIAL.
* The DOM object for the Excel_XML does not seems to be initialized.
WRITE: / 'The DOM object for the Excel_XML does not seems' &
'to be initialized.'.
EXIT.
ENDIF.
* validate for Excel
* Get the root element name
CALL METHOD g_obj_excel_workbook->get_name
RECEIVING
rval = g_excel_workbook_name.
CONDENSE g_excel_workbook_name.
* Check if the name is Workbook
IF g_excel_workbook_name <> 'Workbook'.
WRITE: / 'The EXCEL XML root element name should be ' &
'Workbook and not', g_excel_workbook_name.
EXIT.
ENDIF.
*---------------------------------------------------------------------*
*STEP 3:Get worksheets
*---------------------------------------------------------------------*
* Get all the Worksheet elements
CALL METHOD g_obj_excel_workbook->get_elements_by_tag_name
EXPORTING
name = 'Worksheet'
* NAMESPACE = ''
depth = 1
RECEIVING
rval = g_obj_worksheet_collections.
* Make sure the collection is valid
IF g_obj_worksheet_collections IS INITIAL.
WRITE: / 'The Excel workbook does not contain any Worksheets.'.
EXIT.
ENDIF.
* create an iterator for Worksheet elements
CALL METHOD g_obj_worksheet_collections->create_iterator
RECEIVING
rval = g_obj_worksheet_itr.
* Make sure the iterator is valid.
IF g_obj_worksheet_itr IS INITIAL.
WRITE: / 'The Excel workbook does not contain any Worksheets.'.
EXIT.
ENDIF.
* step 3.1 get the first worksheet
CALL METHOD g_obj_worksheet_itr->get_next
RECEIVING
rval = v_obj_a_worksheet_node.
* Validate of not null before proceeding.
IF v_obj_a_worksheet_node IS INITIAL.
WRITE: / 'The Excel workbook does not contain any Worksheets.'.
EXIT.
ENDIF.
* Lets type cast THE NODE OBJECT to ELEMENT OBJECT
CLEAR g_cast.
CALL METHOD v_obj_a_worksheet_node->query_interface
EXPORTING
iid = 130 "IXML_IID_ELEMENT
RECEIVING
rval = g_cast.
v_obj_a_worksheet_element ?= g_cast.
* Make sure the casting was success
IF v_obj_a_worksheet_element IS INITIAL.
WRITE: / 'The Excel workbook does not contain any Worksheets.'.
EXIT.
ENDIF.
* Get the value for the attribute ss:Name
CALL METHOD v_obj_a_worksheet_element->get_attribute
EXPORTING
name = 'Name'
namespace = 'ss'
RECEIVING
rval = v_worksheet_ss_name.
CONDENSE v_worksheet_ss_name.
TRANSLATE v_worksheet_ss_name TO UPPER CASE.
* step 3.3 Loop (1) get each worksheet one by one,
* Note: it's assumed that the name of the worksheet is
* same as the table name.
* lets loop through all the Worksheet elements
DO."Worksheets
* DATA_COUNT = 0.
IF sy-index = 1. " Special case for the first
IF v_worksheet_ss_name = '#QUERY'.
* get the next element of the iterator.
CALL METHOD g_obj_worksheet_itr->get_next
RECEIVING
rval = v_obj_a_worksheet_node.
ENDIF.
ELSE.
* get the next element of the iterator.
CALL METHOD g_obj_worksheet_itr->get_next
RECEIVING
rval = v_obj_a_worksheet_node.
ENDIF.
* Validate of not null before proceeding.
IF v_obj_a_worksheet_node IS INITIAL.
EXIT.
ENDIF.
* Lets type cast THE NODE OBJECT to ELEMENT OBJECT
CLEAR g_cast.
CALL METHOD v_obj_a_worksheet_node->query_interface
EXPORTING
iid = 130 "IXML_IID_ELEMENT
RECEIVING
rval = g_cast.
v_obj_a_worksheet_element ?= g_cast.
* Make sure the casting was success
IF v_obj_a_worksheet_element IS INITIAL.
CONTINUE.
ENDIF.
* Get the value for the attribute ss:Name
CALL METHOD v_obj_a_worksheet_element->get_attribute
EXPORTING
name = 'Name'
namespace = 'ss'
RECEIVING
rval = v_worksheet_ss_name.
CONDENSE v_worksheet_ss_name.
* The worksheet name is empty, so just dont consider
* that worksheet
IF v_worksheet_ss_name = ''.
CONTINUE.
ENDIF.
*---------------------------------------------------------------------*
*STEP 4:Get the first row of the table element.
*---------------------------------------------------------------------*
* clear all cached records of the itab
* IT_EXCEL_FIELD for every new worksheet
CLEAR: is_excel_field, it_excel_field[].
* Get the Table element, under the Worksheet.
CALL METHOD v_obj_a_worksheet_element->find_from_name
EXPORTING
name = 'Table'
namespace = ''
depth = 1
RECEIVING
rval = v_obj_a_table_element.
IF v_obj_a_table_element IS INITIAL.
CONTINUE.
ENDIF.
* Get all the Row elements
CALL METHOD v_obj_a_table_element->get_elements_by_tag_name
EXPORTING
name = 'Row'
* NAMESPACE = ''
depth = 1
RECEIVING
rval = v_obj_row_collections.
* Make sure the collection is valid
IF v_obj_row_collections IS INITIAL.
CONTINUE.
ENDIF.
* create an iterator for Row elements
CALL METHOD v_obj_row_collections->create_iterator
RECEIVING
rval = v_obj_row_itr.
* Make sure the iterator is valid.
IF v_obj_row_itr IS INITIAL.
CONTINUE.
ENDIF.
* Get the first Row element.
CALL METHOD v_obj_row_itr->get_next
RECEIVING
rval = v_obj_a_row_node.
* Make sure the Row element is valid
IF v_obj_a_row_node IS INITIAL.
CONTINUE.
ENDIF.
* Lets type cast THE NODE OBJECT to ELEMENT OBJECT
CLEAR g_cast.
CALL METHOD v_obj_a_row_node->query_interface
EXPORTING
iid = 130 "IXML_IID_ELEMENT
RECEIVING
rval = g_cast.
v_obj_a_row_element ?= g_cast.
* Make sure the casting was success
IF v_obj_a_row_element IS INITIAL.
CONTINUE.
ENDIF.
*---------------------------------------------------------------------*
*STEP 5:Get the column names of the current table these are
* the fields defined under the table and cache them
* with together with the index.
* Also take into consideration the fields missing
* and merging of cells.
*---------------------------------------------------------------------*
* Get all the Cell elements
CALL METHOD v_obj_a_row_element->get_elements_by_tag_name
EXPORTING
name = 'Cell'
depth = 1
RECEIVING
rval = v_obj_cell_collections.
* Make sure the collection is valid
IF v_obj_cell_collections IS INITIAL.
CONTINUE.
ENDIF.
* create an iterator for Cell elements
CALL METHOD v_obj_cell_collections->create_iterator
RECEIVING
rval = v_obj_cell_itr.
* Make sure the iterator is valid.
IF v_obj_cell_itr IS INITIAL.
CONTINUE.
ENDIF.
* Reset the current Cell index before the first column
* The first column index is always one (1)
* Thus to reset it before the first column is value 0
v_current_cell_index = 0.
* Reset the merged Cells to 0, which means no
* cells are merged.
v_merged_cells = 0.
* loop through all the Cell elements
DO.
* First clear any garbage objects
CLEAR v_obj_a_cell_node.
CLEAR v_obj_a_cell_element.
* Lets skip the index for the merged Cells
v_current_cell_index =
v_current_cell_index + v_merged_cells.
* Get the next Cell element.
CALL METHOD v_obj_cell_itr->get_next
RECEIVING
rval = v_obj_a_cell_node.
* Make sure the Cell element is valid
IF v_obj_a_cell_node IS INITIAL.
EXIT.
ENDIF.
* Lets type cast THE NODE OBJECT to ELEMENT OBJECT
CLEAR g_cast.
CALL METHOD v_obj_a_cell_node->query_interface
EXPORTING
iid = 130 "IXML_IID_ELEMENT
RECEIVING
rval = g_cast.
v_obj_a_cell_element ?= g_cast.
* Make sure the casting was success
IF v_obj_a_cell_element IS INITIAL.
EXIT.
ENDIF.
* Calculate the current absolute Horizontal Index
* of the cell.
* First priority is to get any index value mentioned
* in the Cell as ss:Index attribute.
* Second priority is to predict based on the prevous
* value.
* get the attribute ss:Index of the current Cell element
v_an_int = 0.
CALL METHOD v_obj_a_cell_element->get_attribute
EXPORTING
name = 'Index'
namespace = 'ss'
RECEIVING
rval = v_an_integer_string.
CONDENSE v_an_integer_string.
* A Index attribute was received.
IF v_an_integer_string <> ''.
CATCH SYSTEM-EXCEPTIONS convt_no_number = 1.
v_an_int = v_an_integer_string.
ENDCATCH.
ENDIF.
* Make sure a valid index is avaliable
IF v_an_int > 0.
* Its visually set in the Excel Cell itself, so use it.
v_current_cell_index = v_an_int.
ELSE.
* no index is mentioned, so assume the next value
v_current_cell_index = v_current_cell_index + 1.
ENDIF.
* if required, cache the number of merged cells,
* as per the value mentioned in the attribute
* ss:MergeAcross.
*
* This can definitly occur when two or more cells
* are merged into one.
* Lets first assume there is no merged Cells
v_merged_cells = 0.
* get the attribute ss:MergeAcross of the current
* Cell element
v_an_int = 0.
* A MergeAcross attribute was received.
IF v_an_integer_string <> ''.
CATCH SYSTEM-EXCEPTIONS convt_no_number = 1.
v_an_int = v_an_integer_string.
ENDCATCH.
ENDIF.
* Make sure a valid MergeAcross attribute is avaliable
IF v_an_int > 0.
v_merged_cells = v_an_int.
ELSE.
v_merged_cells = 0.
ENDIF.
* get the Data element under the Cell element.
CALL METHOD v_obj_a_cell_element->find_from_name
EXPORTING
name = 'Data'
* namespace = ''
depth = 1
RECEIVING
rval = v_obj_a_data_element.
IF v_obj_a_data_element IS INITIAL.
CONTINUE.
ENDIF.
* get the text element under the Data element.
CALL METHOD v_obj_a_data_element->get_children
RECEIVING
rval = v_obj_text_lst.
v_txt_val = ''.
DO.
* get the next element of the iterator.
CALL METHOD v_obj_text_itr->get_next
RECEIVING
rval = v_obj_a_txt_node.
* Validate of not null before proceeding.
IF v_obj_a_txt_node IS INITIAL.
EXIT.
ENDIF.
* Get the current node type
CALL METHOD v_obj_a_txt_node->get_type
RECEIVING
rval = v_txt_type.
* We are only concentrating on text type nodes
IF v_txt_type <> if_ixml_node=>co_node_text.
CONTINUE.
ENDIF.
* We are only expecting one text node.
* If the DOM id is parsed from a document, only
* one text node will be present.
* Also its not standard to add more than one
* text nodes
CALL METHOD v_obj_a_txt_node->get_value
RECEIVING
rval = v_txt_val.
EXIT.
ENDDO.
CONDENSE v_txt_val.
* Make sure the field name is not empty
IF v_txt_val = ''.
CONTINUE.
ENDIF.
* cache these values in the itab IT_EXCEL_FIELD
* with excel index as the master key for the field name
* resolving.
CLEAR is_excel_field.
is_excel_field-excel_index = v_current_cell_index.
is_excel_field-field_name = v_txt_val.
INSERT is_excel_field INTO TABLE it_excel_field.
ENDDO.
IF NOT it_excel_field[] IS INITIAL.
* Loop (2) through all the remaining rows of the
* Current table.
DO.
* Clear any previous header field details.
LOOP AT it_excel_field INTO is_excel_field.
CLEAR is_excel_field-field_value.
MODIFY it_excel_field FROM is_excel_field INDEX sy-tabix.
ENDLOOP.
* Clear an previous values
CLEAR v_obj_a_row_node.
CLEAR v_obj_a_row_element.
* Get the next row node
CALL METHOD v_obj_row_itr->get_next
RECEIVING
rval = v_obj_a_row_node.
* Make sure the Row element is valid
IF v_obj_a_row_node IS INITIAL.
EXIT.
ENDIF.
* Lets type cast THE NODE OBJECT to ELEMENT OBJECT
* V_OBJ_A_ROW_ELEMENT ?=
* V_OBJ_A_ROW_NODE->QUERY_INTERFACE( IXML_IID_ELEMENT ).
* Make sure the casting was success
IF v_obj_a_row_element IS INITIAL.
CONTINUE.
ENDIF.
* Loop through each Cell/Data element.
* Clear any garbage objects
CLEAR v_obj_cell_collections.
CLEAR v_obj_cell_itr.
* Get all the Cell elements
CALL METHOD v_obj_a_row_element->get_elements_by_tag_name
EXPORTING
name = 'Cell'
* NAMESPACE = ''
depth = 1
RECEIVING
rval = v_obj_cell_collections.
* Make sure the collection is valid
IF v_obj_cell_collections IS INITIAL.
CONTINUE.
ENDIF.
* create an iterator for Cell elements
CALL METHOD v_obj_cell_collections->create_iterator
RECEIVING
rval = v_obj_cell_itr.
* Make sure the iterator is valid.
IF v_obj_cell_itr IS INITIAL.
CONTINUE.
ENDIF.
* Reset the current Cell index before the first column
* The first column index is always one (1)
* Thus to reset it before the first column is value 0
v_current_cell_index = 0.
* Reset the merged Cells to 0, which means no
* cells are merged.
v_merged_cells = 0.
* loop(3) through all the Cell elements
DO.
* Lets skip the index for the merged Cells
v_current_cell_index =
v_current_cell_index + v_merged_cells.
* First clear any garbage objects
CLEAR v_obj_a_cell_node.
CLEAR v_obj_a_cell_element.
* Get the next Cell element.
CALL METHOD v_obj_cell_itr->get_next
RECEIVING
rval = v_obj_a_cell_node.
* Make sure the Cell element is valid
IF v_obj_a_cell_node IS INITIAL.
EXIT.
ENDIF.
* Lets type cast THE NODE OBJECT to ELEMENT OBJECT
* V_OBJ_A_CELL_ELEMENT ?=
* V_OBJ_A_CELL_NODE->QUERY_INTERFACE( IXML_IID_ELEMENT ).
* Make sure the casting was success
IF v_obj_a_cell_element IS INITIAL.
EXIT.
ENDIF.
* Calculate the current absolute Horizontal Index
* of the cell.
* First priority is to get any index value mentioned
* in the Cell as ss:Index attribute.
* Second priority is to predict based on the prevous
* value.
* get the attribute ss:Index of the current Cell element
v_an_int = 0.
CALL METHOD v_obj_a_cell_element->get_attribute
EXPORTING
name = 'Index'
namespace = 'ss'
RECEIVING
rval = v_an_integer_string.
CONDENSE v_an_integer_string.
* A Index attribute was received.
IF v_an_integer_string <> ''.
CATCH SYSTEM-EXCEPTIONS convt_no_number = 1.
v_an_int = v_an_integer_string.
ENDCATCH.
ENDIF.
* Make sure a valid index is avaliable
IF v_an_int > 0.
* Its visually set in the Excel Cell itself, so use it.
v_current_cell_index = v_an_int.
ELSE.
* no index is mentioned, so assume the next value
v_current_cell_index = v_current_cell_index + 1.
ENDIF.
* if required, cache the number of merged cells,
* as per the value mentioned in the attribute
* ss:MergeAcross.
*
* This can definitly occur when two or more cells
* are merged into one.
* Lets first assume there is no merged Cells
v_merged_cells = 0.
* get the attribute ss:MergeAcross of the current
* Cell element
v_an_int = 0.
* A MergeAcross attribute was received.
IF v_an_integer_string <> ''.
CATCH SYSTEM-EXCEPTIONS convt_no_number = 1.
v_an_int = v_an_integer_string.
ENDCATCH.
ENDIF.
* Make sure a valid MergeAcross attribute is avaliable
IF v_an_int > 0.
v_merged_cells = v_an_int.
ELSE.
v_merged_cells = 0.
ENDIF.
* Resolve the field id based on its Horizontal Index
* from the itab IT_EXCEL_FIELD.
* get the Data element under the Cell element.
CALL METHOD v_obj_a_cell_element->find_from_name
EXPORTING
name = 'Data'
* namespace = ''
depth = 1
RECEIVING
rval = v_obj_a_data_element.
IF v_obj_a_data_element IS INITIAL.
CONTINUE.
ENDIF.
* get the text element under the Data element.
CALL METHOD v_obj_a_data_element->get_children
RECEIVING
rval = v_obj_text_lst.
* get the next element of the iterator.
CALL METHOD v_obj_text_itr->get_next
RECEIVING
rval = v_obj_a_txt_node.
* Validate of not null before proceeding.
IF v_obj_a_txt_node IS INITIAL.
EXIT.
ENDIF.
* Get the current node type
CALL METHOD v_obj_a_txt_node->get_type
RECEIVING
rval = v_txt_type.
* We are only concentrating on text type nodes
IF v_txt_type <> if_ixml_node=>co_node_text.
CONTINUE.
ENDIF.
* We are only expecting one text node.
* If the DOM id is parsed from a document, only
* one text node will be present.
* Also its not standard to add more than one
* text nodes
CALL METHOD v_obj_a_txt_node->get_value
RECEIVING
rval = v_txt_val.
EXIT.
ENDDO.
CONDENSE v_txt_val.
* Make sure the field name is not empty
IF v_txt_val = ''.
CONTINUE.
ENDIF.
* Clear any previous header line
CLEAR is_excel_field.
* Read the table IT_EXCEL_FIELD for the
* corrosponding entry.
READ TABLE it_excel_field INTO is_excel_field
WITH KEY excel_index = v_current_cell_index
BINARY SEARCH.
* Make sure the read was success
IF sy-subrc <> 0.
CONTINUE.
ENDIF.
CLEAR is_excel_field-field_value.
is_excel_field-field_value = v_txt_val.
MODIFY it_excel_field FROM is_excel_field
INDEX v_current_cell_index.
* Endo of loop (3)
ENDDO.
*---------------------------------------------------------------------*
*STEP 6:
*At this stage, we have all the XML data in the internal table
*IT_EXCEL_FIELD. Now, populate the internal tables representing
*individual data dictionary objects.
*---------------------------------------------------------------------*
CLEAR is_excel_field.
READ TABLE it_excel_field INTO is_excel_field INDEX 1.
IF sy-subrc = 0.
*If the data corresponds to DOMAINS
IF is_excel_field-field_name = 'DOMNAME'.
*Loop over the Excel table and
*populate the internal tables representing DOMAINS
CLEAR is_excel_field.
LOOP AT it_excel_field INTO is_excel_field.
CONCATENATE 'IS_DOMAINS-' is_excel_field-field_name
INTO line.
UNASSIGN <fs_value>.
ASSIGN (line) TO <fs_value>.
IF <fs_value> IS ASSIGNED.
<fs_value> = is_excel_field-field_value.
ENDIF.
ENDLOOP.
APPEND is_domains TO it_domains.
CLEAR is_domains.
CLEAR is_excel_field.
LOOP AT it_excel_field INTO is_excel_field.
CONCATENATE 'IS_DOMA_VALUES-' is_excel_field-field_name
INTO line.
UNASSIGN <fs_value>.
ASSIGN (line) TO <fs_value>.
IF <fs_value> IS ASSIGNED.
<fs_value> = is_excel_field-field_value.
ENDIF.
ENDLOOP.
APPEND is_doma_values TO it_doma_values.
CLEAR is_doma_values.
ENDIF. "IT_EXCEL_FIELD-FIELD_NAME = 'DOMANAME'
*If the data corresponds to DATAELEMENTS
IF is_excel_field-field_name = 'ROLLNAME'.
*Loop over the Excel table and
*populate the internal tables representing DATA ELEMENTS
CLEAR is_excel_field.
LOOP AT it_excel_field INTO is_excel_field.
CONCATENATE 'IS_DATAELEMENTS-' is_excel_field-field_name
INTO line.
UNASSIGN <fs_value>.
ASSIGN (line) TO <fs_value>.
IF <fs_value> IS ASSIGNED.
<fs_value> = is_excel_field-field_value.
ENDIF.
ENDLOOP.
APPEND is_dataelements TO it_dataelements.
CLEAR is_dataelements.
ENDIF. "IT_EXCEL_FIELD-FIELD_NAME = 'DTELNAME'.
*If the data corresponds to TABLES
IF is_excel_field-field_name = 'TABNAME'.
*Loop over the Excel table and
*populate the internal tables representing TABLES
CLEAR is_excel_field.
LOOP AT it_excel_field INTO is_excel_field.
CONCATENATE 'IS_TABLES-' is_excel_field-field_name
INTO line.
UNASSIGN <fs_value>.
ASSIGN (line) TO <fs_value>.
IF <fs_value> IS ASSIGNED.
<fs_value> = is_excel_field-field_value.
ENDIF.
ENDLOOP.
APPEND is_tables TO it_tables.
CLEAR is_tables.
CLEAR is_excel_field.
LOOP AT it_excel_field INTO is_excel_field.
CONCATENATE 'IS_TABL_TECHNICS-' is_excel_field-field_name
INTO line.
UNASSIGN <fs_value>.
ASSIGN (line) TO <fs_value>.
IF <fs_value> IS ASSIGNED.
<fs_value> = is_excel_field-field_value.
ENDIF.
ENDLOOP.
APPEND is_tabl_technics TO it_tabl_technics.
CLEAR is_tabl_technics.
CLEAR is_excel_field.
LOOP AT it_excel_field INTO is_excel_field.
CONCATENATE 'IS_TABL_FIELDS-' is_excel_field-field_name
INTO line.
UNASSIGN <fs_value>.
ASSIGN (line) TO <fs_value>.
IF <fs_value> IS ASSIGNED.
<fs_value> = is_excel_field-field_value.
ENDIF.
ENDLOOP.
APPEND is_tabl_fields TO it_tabl_fields.
CLEAR is_tabl_fields.
ENDIF. "IT_EXCEL_FIELD-FIELD_NAME = 'TABLNAME'.
ENDIF. "READ TABLE IT_EXCEL_FIELD
ENDDO.
ENDIF. "IF NOT IT_EXCEL_FIELD[] IS INITIAL.
ENDDO."Worksheets
*---------------------------------------------------------------------*
*STEP 7: Perform Data readiness
*---------------------------------------------------------------------*
*Delete duplicates
DELETE ADJACENT DUPLICATES FROM it_tables COMPARING tabname.
DELETE ADJACENT DUPLICATES FROM it_tabl_technics COMPARING tabname.
DELETE it_tables WHERE tabname = space.
DELETE it_tabl_technics WHERE tabname = space.
*The table category is always TRANSPARANT.
CLEAR:is_tables.
LOOP AT it_tables INTO is_tables.
is_tables-tabclass = 'TRANSP'.
MODIFY it_tables INDEX sy-tabix FROM is_tables.
ENDLOOP.
*Sort data by object name
SORT it_domains BY domname.
SORT it_doma_values BY domname.
SORT it_dataelements BY rollname.
SORT it_tables BY tabname.
SORT it_tabl_technics BY tabname.
SORT it_tabl_fields BY tabname position.
ENDFORM. "F004_GET_FILE_DATA
*---------------------------------------------------------------------*
*F007_CREATE_DOMAINS.
*---------------------------------------------------------------------*
FORM f007_create_domains.
DATA: ls_return TYPE coms_return.
CLEAR:is_domains, v_ext.
LOOP AT it_domains INTO is_domains.
CLEAR: ls_return.
*Check whether domain exists or not
CALL FUNCTION 'DD_CHECK_NAME'
EXPORTING
name = is_domains-domname
objtyp = 'DOMA'
IMPORTING
obj_exists = v_ext.
*Get domain attributes
CLEAR is_domains_tmp.
MOVE is_domains TO is_domains_tmp.
*Get domain range of values
* CLEAR:IS_DOMA_VALUES.
* LOOP AT IT_DOMA_VALUES INTO IS_DOMA_VALUES
* WHERE DOMNAME = IS_DOMAINS-DOMNAME.
*
* APPEND IS_DOMA_VALUES TO IT_DOMA_VALUES_TMP.
*
* ENDLOOP.
*---------------------------------------------------------------------*
*F008_CREATE_DATAELEMENTS.
*---------------------------------------------------------------------*
FORM f008_create_dataelements.
DATA: ls_return TYPE coms_return.
CLEAR: is_dataelements, v_ext.
LOOP AT it_dataelements INTO is_dataelements.
*Check whether data element exists or not
CALL FUNCTION 'DD_CHECK_NAME'
EXPORTING
name = is_dataelements-rollname
objtyp = 'DTEL'
IMPORTING
obj_exists = v_ext.
*If data element doesn't exist
IF v_ext <> c_x.
CLEAR: v_create.
v_create = c_yes.
ENDIF. "V_EXT <> C_X
IF v_create = c_yes.
*Get data element attributes
CLEAR: is_dataelements_tmp.
MOVE is_dataelements TO is_dataelements_tmp.
*Check if the domain exists, if not throw error
CLEAR: v_exist.
CALL FUNCTION 'DD_CHECK_NAME'
EXPORTING
name = is_dataelements_tmp-domname
objtyp = 'DOMA'
IMPORTING
obj_exists = v_exist.
*If domain does not exist
IF v_exist = c_space.
CLEAR:line, v_text.
CONCATENATE 'Domain' is_dataelements_tmp-domname
'does not exist to create Data Element'
is_dataelements-rollname
INTO line SEPARATED BY space.
v_text = line.
v_msgv3 = is_dataelements-rollname.
PERFORM f012_populate_messages
USING
space
'E'
v_msgno
'R3TR'
'DTEL'
v_msgv3
v_msgv4
v_text.
ENDIF.
*If domain exists
IF v_exist = c_x.
*Create data element
CLEAR: ls_return.
CALL FUNCTION 'COM_GEN_DATAELEMENT_CREATE'
EXPORTING
iv_dtel_name = is_dataelements-rollname
iv_devclass = '$TMP'
iv_activate = 'X'
is_dd04v = is_dataelements_tmp
IMPORTING
es_return = ls_return.
*---------------------------------------------------------------------*
*F009_CREATE_TABLES.
*---------------------------------------------------------------------*
FORM f009_create_tables.
DATA: ls_return TYPE coms_return.
CLEAR:is_tables, v_ext.
LOOP AT it_tables INTO is_tables.
*Get table header information
CLEAR: it_tabl_inf_tmp.
MOVE is_tables TO it_tabl_inf_tmp.
*Get table technics
CLEAR: is_tabl_technics, is_tabl_technics_tmp.
READ TABLE it_tabl_technics INTO is_tabl_technics WITH KEY tabname = is_tables-tabname.
IF sy-subrc = 0.
MOVE is_tabl_technics TO is_tabl_technics_tmp.
ENDIF.
*Get table fields
LOOP AT it_tabl_fields INTO is_tabl_fields
WHERE tabname = is_tables-tabname.
APPEND is_tabl_fields TO it_tabl_fields_tmp.
CLEAR is_tabl_fields.
ENDLOOP.
*Check if all the data elements exists or not
CLEAR: v_exist.
LOOP AT it_tabl_fields_tmp INTO is_tabl_fields.
CLEAR:v_exist.
CALL FUNCTION 'DD_CHECK_NAME'
EXPORTING
name = is_tabl_fields-rollname
objtyp = 'DTEL'
IMPORTING
obj_exists = v_exist.
IF v_exist = c_x.
CONTINUE.
ELSE.
CLEAR:line, v_text.
CONCATENATE 'Data Element' is_tabl_fields-rollname
'does not exist to create Table'
is_tabl_fields-tabname
INTO line SEPARATED BY space.
v_text = line.
v_msgv3 = is_tabl_fields-tabname.
PERFORM f012_populate_messages
USING
space
'E'
v_msgno
'R3TR'
'TABL'
v_msgv3
v_msgv4
v_text.
*---------------------------------------------------------------------*
* Form F011_DISPLAY_MESSAGES
*---------------------------------------------------------------------*
FORM f011_display_messages.
DATA: lt_fieldcat TYPE slis_t_fieldcat_alv,
ls_fieldcat TYPE slis_fieldcat_alv.
DATA: lv_repid TYPE sy-repid.
lv_repid = sy-repid.
IF it_messages IS INITIAL.
MESSAGE i002(sy) WITH 'No data available for processing'.
LEAVE PROGRAM.
ENDIF.
*--------------------------------------------------------------------*
* Form F012_POPULATE_MESSAGES
*--------------------------------------------------------------------*
FORM f012_populate_messages USING v_msgid TYPE sy-msgid
v_msgty TYPE sy-msgty
v_msgno TYPE sy-msgno
v_msgv1 TYPE sy-msgv1
v_msgv2 TYPE sy-msgv2
v_msgv3 TYPE sy-msgv3
v_msgv4 TYPE sy-msgv4
v_text TYPE any.
*If message text is not available
IF v_text = c_space.
APPEND is_messages TO it_messages.
CLEAR is_messages.
ENDIF. "V_TEXT = C_SPACE.
ENDFORM. "F012_POPULATE_MESSAGES
*&---------------------------------------------------------------------*
*& Form F_FILE_CHECK
*&---------------------------------------------------------------------*
FORM f_file_check.
*Input file should not be blank
IF p_file = c_space.
SET CURSOR FIELD 'P_FILE'.
MESSAGE e002(sy) WITH 'Enter Input File Name!'.
ENDIF.
ENDFORM. " F_FILE_CHECK
*&---------------------------------------------------------------------*
*& Form f_top_of_page
*&---------------------------------------------------------------------*
FORM f_top_of_page.
CLEAR:line.
CONCATENATE 'Customized Database Tables Generation by'
sy-uname
INTO line SEPARATED BY space.
WRITE :/ line.
WRITE :/10 'DATE:',sy-datum.
ENDFORM. " F_TOP_OF_PAGE
*&---------------------------------------------------------------------*
*& Form f_end_of_page
*&---------------------------------------------------------------------*
FORM f_end_of_page.
WRITE:sy-uline(90).
WRITE:/30 'PAGE:',sy-pagno CENTERED.
ENDFORM. " F_END_OF_PAGE
*&---------------------------------------------------------------------*
*& Form F_FREE_MEMORY
*&---------------------------------------------------------------------*
FORM f_free_memory.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.