Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

xml to ztable

Former Member
0 Likes
918

Hi

I need to read the xml from Application Server and need to store only the values in customised table.

i have come across some examples to read the xml to internal table in front end.

in those data is taken as string, how to store the data into the z table?

4 REPLIES 4
Read only

Former Member
0 Likes
669

Hi

see this blog

/people/r.eijpe/blog/2005/11/21/xml-dom-processing-in-abap-part-ii--convert-an-xml-file-into-an-abap-table-using-sap-dom-approach

Please see the below program.

TYPE-POOLS: ixml. "iXML Library Types

*TABLES : rbkp.

&----


  • TYPE DECLERATIION

&----


TYPES: BEGIN OF type_tabpo,

ebeln TYPE ekko-ebeln, "PO document number

ebelp TYPE ekpo-ebelp, "PO line item

END OF type_tabpo.

TYPES: BEGIN OF type_ekbe,

belnr TYPE rbkp-belnr, "Invoice document

gjahr TYPE rbkp-gjahr, "fiscal year

END OF type_ekbe.

TYPES: BEGIN OF type_invoice,

belnr TYPE rbkp-belnr, "PO document number

gjahr TYPE rbkp-gjahr, "Fiscal Year

rbstat TYPE rbkp-rbstat, "invoice status

END OF type_invoice.

TYPES: BEGIN OF t_xml_line, "Structure for holding XML data

data(256) TYPE x,

END OF t_xml_line.

&----


  • INTERNAL TABLE DECLERATIION

&----


DATA: gi_tabpo TYPE STANDARD TABLE OF type_tabpo,

gi_ekbe TYPE STANDARD TABLE OF type_ekbe,

gi_invoice TYPE STANDARD TABLE OF type_invoice,

gi_bapiret2 TYPE STANDARD TABLE OF bapiret2.

DATA: l_ixml TYPE REF TO if_ixml,

l_streamfactory TYPE REF TO if_ixml_stream_factory.

  • l_parser TYPE REF TO if_ixml_parser,

  • l_istream TYPE REF TO swif_ixml_istream,

  • l_document TYPE REF TO if_ixml_document,

  • l_node TYPE REF TO if_ixml_node,

  • l_xmldata TYPE string.

*DATA: l_elem TYPE REF TO if_ixml_element,

  • l_root_node TYPE REF TO if_ixml_node,

  • l_next_node TYPE REF TO if_ixml_node,

  • l_name TYPE string,

  • l_iterator TYPE REF TO if_ixml_node_iterator.

DATA: l_xml_table TYPE TABLE OF t_xml_line, " XML Table of the structure

l_xml_line TYPE t_xml_line, " Record of structure t_xml_line

l_xml_table_size TYPE i. " XML table size

DATA: l_filename TYPE string.

----


  • WORK AREA DECLARATION

----


DATA: gw_tabpo TYPE type_tabpo,

gw_ekbe TYPE type_ekbe,

gw_invoice TYPE type_invoice,

gw_bapiret2 TYPE bapiret2.

*********************************************************************

  • BEGIN OF SELECTION SCREEN

*********************************************************************

SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001.

PARAMETERS: p_file TYPE pathintern LOWER CASE DEFAULT '/usr/sap/tmp/'.

  • Validation of XML file: Only DTD included in XML document is supported

SELECTION-SCREEN END OF BLOCK blk1.

***********************************************************************

  • INTIALISATION.

***********************************************************************

INITIALIZATION.

***********************************************************************

  • SELECTION SCREEN VALIDATION

***********************************************************************

AT SELECTION-SCREEN.

  • To validate p_file is not initial

PERFORM sub_validate_file.

  • PERFORM sub_validate_path.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  • Request for filename for xml file from the application server

PERFORM sub_get_filename_appl USING p_file.

***********************************************************************

  • START OF SELECTION SCREEN

***********************************************************************

START-OF-SELECTION.

PERFORM sub_fetch_po_details.

PERFORM sub_get_invoice.

PERFORM sub_rel_invoice.

***********************************************************************

  • END OF SELECTION SCREEN

***********************************************************************

END-OF-SELECTION.

&----


*& Form sub_validate_file

&----


  • To Validate the file

*

----


FORM sub_validate_file .

IF p_file IS INITIAL.

MESSAGE e000. "specify the file path

ENDIF.

ENDFORM. " sub_validate_file

&----


*& Form sub_get_filename_appl

&----


form sub_get_filename_appl USING l_fname TYPE any.

  • DATA: l_fname TYPE filename-fileintern. " File name

*GET THE FILENAME FROM THE APPLICATION SERVER

CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'

EXPORTING

directory = l_fname

filemask = '*'

IMPORTING

serverfile = l_fname

EXCEPTIONS

canceled_by_user = 1

OTHERS = 2.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

ENDFORM. " sub_get_filename_appl

&----


*& Form sub_fetch_po_details

&----


  • To fetch the PO details from the application server

  • Format of file is XML

*----


FORM sub_fetch_po_details .

************************************************************************

  • TYPE DECLERATIION

************************************************************************

l_ixml = cl_ixml=>create( ).

  • Creating a stream factory

l_streamfactory = l_ixml->create_stream_factory( ).

PERFORM get_xml_table.

LOOP AT gi_tabpo INTO gw_tabpo.

WRITE:/ gw_tabpo.

ENDLOOP.

ENDFORM. " sub_fetch_po_details

&----


*& Form get_xml_table

&----


  • Read from the xml file

----


FORM get_xml_table .

************************************************************************

  • Local variable declarations

************************************************************************

DATA: l_len TYPE i,

l_len2 TYPE i,

l_tab TYPE tsfixml,

l_content TYPE string,

l_str1 TYPE string,

c_conv TYPE REF TO cl_abap_conv_in_ce,

l_itab TYPE TABLE OF string.

l_filename = p_file.

***********************************************************************

  • code to upload data from application server

***********************************************************************

OPEN DATASET l_filename FOR INPUT IN BINARY MODE.

IF sy-subrc <> 0.

WRITE:/ 'invalid file path'.

ENDIF.

DO.

READ DATASET l_filename INTO l_xml_line.

IF sy-subrc EQ 0.

APPEND l_xml_line TO l_xml_table.

ELSE.

EXIT.

ENDIF.

ENDDO.

CLOSE DATASET l_filename.

  • code to find the table size

DESCRIBE TABLE l_xml_table.

l_xml_table_size = ( sy-tleng ) * ( sy-tfill ).

*code to convert hexadecimal to XML

LOOP AT l_xml_table INTO l_xml_line.

c_conv = cl_abap_conv_in_ce=>create( input = l_xml_line-data

replacement

= space ).

c_conv->read( IMPORTING data = l_content len = l_len ).

CONCATENATE l_str1 l_content INTO l_str1.

ENDLOOP.

l_str1 = l_str1+0(l_xml_table_size).

SPLIT l_str1 AT cl_abap_char_utilities=>cr_lf INTO TABLE l_itab.

LOOP AT l_itab INTO l_str1.

REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>horizontal_tab IN

l_str1 WITH space.

ENDLOOP.

CALL TRANSFORMATION ('ID') " code to put in internal table

SOURCE XML l_str1

RESULT tab = gi_tabpo[].

ENDFORM. " get_xml_table

<b>Reward if usefull</b>

Read only

0 Likes
669

hi,

some whta i am successful in reading the data to R/3 internal table and able to append to ztable with the help of blog:

/people/r.eijpe/blog/2005/11/21/xml-dom-processing-in-abap-part-ii--convert-an-xml-file-into-an-abap-table-using-sap-dom-approach

modification as followed:

  • text node

value = node->get_value( ).

WRITE: / 'VALUE :'.

WRITE: AT indent value COLOR COL_GROUP INVERSE.

<b> if name = 'VENDOR_NAME'.

assign component 6 of structure wa_zpp13 to <fs_field>.

<fs_field> = value.

append wa_zpp13 to it_zpp13.

endif.</b>

but problem here is i have around 50 field values to be appended to ztable.

is there any dynamic way to handle this!

Note: my ztable field names maintained same as XML field names.

Read only

Former Member
0 Likes
669

hI

SEE THIS BLOG

/people/r.eijpe/blog/2005/11/21/xml-dom-processing-in-abap-part-ii--convert-an-xml-file-into-an-abap-table-using-sap-dom-approach

TYPE-POOLS: ixml. "iXML Library Types
*TABLES : rbkp.

*&---------------------------------------------------------------------*
* TYPE DECLERATIION
*&---------------------------------------------------------------------*

TYPES: BEGIN OF type_tabpo,
ebeln TYPE ekko-ebeln, "PO document number
ebelp TYPE ekpo-ebelp, "PO line item
END OF type_tabpo.

TYPES: BEGIN OF type_ekbe,
belnr TYPE rbkp-belnr, "Invoice document
gjahr TYPE rbkp-gjahr, "fiscal year
END OF type_ekbe.

TYPES: BEGIN OF type_invoice,
belnr TYPE rbkp-belnr, "PO document number
gjahr TYPE rbkp-gjahr, "Fiscal Year
rbstat TYPE rbkp-rbstat, "invoice status
END OF type_invoice.

TYPES: BEGIN OF t_xml_line, "Structure for holding XML data
data(256) TYPE x,
END OF t_xml_line.

*&---------------------------------------------------------------------*
* INTERNAL TABLE DECLERATIION
*&---------------------------------------------------------------------*

DATA: gi_tabpo TYPE STANDARD TABLE OF type_tabpo,
gi_ekbe TYPE STANDARD TABLE OF type_ekbe,
gi_invoice TYPE STANDARD TABLE OF type_invoice,
gi_bapiret2 TYPE STANDARD TABLE OF bapiret2.

DATA: l_ixml TYPE REF TO if_ixml,
l_streamfactory TYPE REF TO if_ixml_stream_factory.
* l_parser TYPE REF TO if_ixml_parser,
* l_istream TYPE REF TO swif_ixml_istream,
* l_document TYPE REF TO if_ixml_document,
* l_node TYPE REF TO if_ixml_node,
* l_xmldata TYPE string.

*DATA: l_elem TYPE REF TO if_ixml_element,
* l_root_node TYPE REF TO if_ixml_node,
* l_next_node TYPE REF TO if_ixml_node,
* l_name TYPE string,
* l_iterator TYPE REF TO if_ixml_node_iterator.

DATA: l_xml_table TYPE TABLE OF t_xml_line, " XML Table of the structure

l_xml_line TYPE t_xml_line, " Record of structure t_xml_line
l_xml_table_size TYPE i. " XML table size

DATA: l_filename TYPE string.

*----------------------------------------------------------------*
* WORK AREA DECLARATION
*----------------------------------------------------------------*

DATA: gw_tabpo TYPE type_tabpo,
gw_ekbe TYPE type_ekbe,
gw_invoice TYPE type_invoice,
gw_bapiret2 TYPE bapiret2.


*********************************************************************
* BEGIN OF SELECTION SCREEN
*********************************************************************

SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001.

PARAMETERS: p_file TYPE pathintern LOWER CASE DEFAULT '/usr/sap/tmp/'.

* Validation of XML file: Only DTD included in XML document is supported

SELECTION-SCREEN END OF BLOCK blk1.


***********************************************************************
* INTIALISATION.
***********************************************************************

INITIALIZATION.

***********************************************************************
* SELECTION SCREEN VALIDATION
***********************************************************************

AT SELECTION-SCREEN.

* To validate p_file is not initial
PERFORM sub_validate_file.
* PERFORM sub_validate_path.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

* Request for filename for xml file from the application server
PERFORM sub_get_filename_appl USING p_file.

***********************************************************************
* START OF SELECTION SCREEN
***********************************************************************

START-OF-SELECTION.

PERFORM sub_fetch_po_details.

PERFORM sub_get_invoice.

PERFORM sub_rel_invoice.

***********************************************************************
* END OF SELECTION SCREEN
***********************************************************************

END-OF-SELECTION.

*&---------------------------------------------------------------------*
*& Form sub_validate_file
*&---------------------------------------------------------------------*
* To Validate the file
*
*----------------------------------------------------------------------*
FORM sub_validate_file .

IF p_file IS INITIAL.
MESSAGE e000. "specify the file path

ENDIF.

ENDFORM. " sub_validate_file




*&---------------------------------------------------------------------*
*& Form sub_get_filename_appl
*&----------------------------------------------------------------------*
form sub_get_filename_appl USING l_fname TYPE any.


* DATA: l_fname TYPE filename-fileintern. " File name
*GET THE FILENAME FROM THE APPLICATION SERVER

CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
EXPORTING
directory = l_fname
filemask = '*'
IMPORTING
serverfile = l_fname
EXCEPTIONS
canceled_by_user = 1
OTHERS = 2.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. " sub_get_filename_appl



*&---------------------------------------------------------------------*
*& Form sub_fetch_po_details
*&---------------------------------------------------------------------*
* To fetch the PO details from the application server
* Format of file is XML
*----------------------------------------------------------------------

FORM sub_fetch_po_details .


************************************************************************
* TYPE DECLERATIION
************************************************************************


l_ixml = cl_ixml=>create( ).

* Creating a stream factory
l_streamfactory = l_ixml->create_stream_factory( ).

PERFORM get_xml_table.

LOOP AT gi_tabpo INTO gw_tabpo.
WRITE:/ gw_tabpo.
ENDLOOP.

ENDFORM. " sub_fetch_po_details

*&--------------------------------------------------------------------*
*& Form get_xml_table
*&--------------------------------------------------------------------*
* Read from the xml file
*---------------------------------------------------------------------*
FORM get_xml_table .

************************************************************************
* Local variable declarations
************************************************************************

DATA: l_len TYPE i,
l_len2 TYPE i,
l_tab TYPE tsfixml,
l_content TYPE string,
l_str1 TYPE string,
c_conv TYPE REF TO cl_abap_conv_in_ce,
l_itab TYPE TABLE OF string.

l_filename = p_file.

***********************************************************************
* code to upload data from application server
***********************************************************************

OPEN DATASET l_filename FOR INPUT IN BINARY MODE.
IF sy-subrc <> 0.
WRITE:/ 'invalid file path'.
ENDIF.
DO.
READ DATASET l_filename INTO l_xml_line.

IF sy-subrc EQ 0.

APPEND l_xml_line TO l_xml_table.

ELSE.
EXIT.
ENDIF.
ENDDO.

CLOSE DATASET l_filename.


* code to find the table size


DESCRIBE TABLE l_xml_table.
l_xml_table_size = ( sy-tleng ) * ( sy-tfill ).


*code to convert hexadecimal to XML


LOOP AT l_xml_table INTO l_xml_line.

c_conv = cl_abap_conv_in_ce=>create( input = l_xml_line-data
replacement
= space ).
c_conv->read( IMPORTING data = l_content len = l_len ).
CONCATENATE l_str1 l_content INTO l_str1.

ENDLOOP.

l_str1 = l_str1+0(l_xml_table_size).
SPLIT l_str1 AT cl_abap_char_utilities=>cr_lf INTO TABLE l_itab.
LOOP AT l_itab INTO l_str1.
REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>horizontal_tab IN
l_str1 WITH space.
ENDLOOP.

CALL TRANSFORMATION ('ID') " code to put in internal table
SOURCE XML l_str1
RESULT tab = gi_tabpo[].

ENDFORM. " get_xml_table

<b>REWARD IF USEFULL</b>

Read only

0 Likes
669

Hi Naresh,

I checked your code. It is giving dump at

<b>CALL TRANSFORMATION ('ID')</b>

I think the source XML type is not supportive for thIs ID. I am not very much aware of ST Technology to create our own...