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 data into itab

Former Member
0 Likes
2,119

Hi Experts,

I have a XML file on my desk top.

How to upload data from XML file into ITAB?

Regards

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,754

Hai Ravi

Check the following Code

REPORT abc.

*----


DATA

DATA : t001 LIKE TABLE OF t001 WITH HEADER LINE.

DATA : BEGIN OF itab OCCURS 0,

a(100) TYPE c,

END OF itab.

DATA: xml_out TYPE string .

DATA : BEGIN OF upl OCCURS 0,

f(255) TYPE c,

END OF upl.

DATA: xmlupl TYPE string .

                                                              • FIRST PHASE

                                                              • FIRST PHASE

                                                              • FIRST PHASE

*----


Fetch Data

SELECT * FROM t001 INTO TABLE t001.

*----


XML

CALL TRANSFORMATION ('ID')

SOURCE tab = t001[]

RESULT XML xml_out.

*----


Convert to TABLE

CALL FUNCTION 'HR_EFI_CONVERT_STRING_TO_TABLE'

EXPORTING

i_string = xml_out

i_tabline_length = 100

TABLES

et_table = itab.

*----


Download

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

filetype = 'BIN'

filename = 'd:\xx.xml'

TABLES

data_tab = itab.

                                                              • SECOND PHASE

                                                              • SECOND PHASE

                                                              • SECOND PHASE

BREAK-POINT.

REFRESH t001.

CLEAR t001.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = 'D:\XX.XML'

filetype = 'BIN'

TABLES

data_tab = upl.

LOOP AT upl.

CONCATENATE xmlupl upl-f INTO xmlupl.

ENDLOOP.

*----


XML

CALL TRANSFORMATION ('ID')

SOURCE XML xmlupl

RESULT tab = t001[].

Thanks & regards

Sreeni

13 REPLIES 13
Read only

Former Member
0 Likes
1,754

hi RaVI,

Use FM GUI_UPLOAD FM with file type as XML

Check these Threads

Read only

Former Member
0 Likes
1,754

Hi ravi,

1. itab --- > xml

xml ---> itab.

2. This program will do both.

(just copy paste in new program)

3.

REPORT abc.

*----


DATA

DATA : t001 LIKE TABLE OF t001 WITH HEADER LINE.

DATA : BEGIN OF itab OCCURS 0,

a(100) TYPE c,

END OF itab.

DATA: xml_out TYPE string .

DATA : BEGIN OF upl OCCURS 0,

f(255) TYPE c,

END OF upl.

DATA: xmlupl TYPE string .

                                                              • FIRST PHASE

                                                              • FIRST PHASE

                                                              • FIRST PHASE

*----


Fetch Data

SELECT * FROM t001 INTO TABLE t001.

*----


XML

CALL TRANSFORMATION ('ID')

SOURCE tab = t001[]

RESULT XML xml_out.

*----


Convert to TABLE

CALL FUNCTION 'HR_EFI_CONVERT_STRING_TO_TABLE'

EXPORTING

i_string = xml_out

i_tabline_length = 100

TABLES

et_table = itab.

*----


Download

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

filetype = 'BIN'

filename = 'd:\xx.xml'

TABLES

data_tab = itab.

                                                              • SECOND PHASE

                                                              • SECOND PHASE

                                                              • SECOND PHASE

BREAK-POINT.

REFRESH t001.

CLEAR t001.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = 'D:\XX.XML'

filetype = 'BIN'

TABLES

data_tab = upl.

LOOP AT upl.

CONCATENATE xmlupl upl-f INTO xmlupl.

ENDLOOP.

*----


XML

CALL TRANSFORMATION ('ID')

SOURCE XML xmlupl

RESULT tab = t001[]

.

BREAK-POINT.

regards,

amit m.

Read only

0 Likes
1,754

Hi All,

Thanks.Points are given.

But i have to do that in CRM.

The FM 'HR_EFI_CONVERT_STRING_TO_TABLE' does not exist in CRM system.

Can you tell me what is CALL TRANSFORMATION ('ID')

this we have to type as it is ?

Read only

0 Likes
1,754

Hi again,

1. use this FM

SCMS_STRING_TO_FTEXT

*----


replace the FM code, with this one :

CALL FUNCTION 'SCMS_STRING_TO_FTEXT'

EXPORTING

TEXT = xml_out

  • IMPORTING

  • LENGTH =

TABLES

FTEXT_TAB = itab.

.

2. Can you tell me what is CALL TRANSFORMATION ('ID')

this we have to type as it is ?

Yes, this is the syntax.

regards,

amit m.

Read only

0 Likes
1,754

Thanks Amit..

I will try that.

Read only

0 Likes
1,754

Hi Amit,

I exicuted the above code in version 4.6c

I am getting error like :"Transaction Expected" Not "Transformation".

Can you tell me what may be the problem?

Read only

Former Member
0 Likes
1,754

You can use the FMs:

SAP_CONVERT_TO_XML_FORMAT

SDIXML_DOM_TO_XML to do this.

http://help.sap.com/saphelp_erp2005/helpdata/en/bb/5766c1dca511d4990b00508b6b8b11/frameset.htm

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

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/b2dcbdfc-0801-0010-97bd-b3f...

See

for more reference.

Read only

Former Member
0 Likes
1,754

Hi,

Check these links..

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

Regards

vijay

Read only

Former Member
0 Likes
1,755

Hai Ravi

Check the following Code

REPORT abc.

*----


DATA

DATA : t001 LIKE TABLE OF t001 WITH HEADER LINE.

DATA : BEGIN OF itab OCCURS 0,

a(100) TYPE c,

END OF itab.

DATA: xml_out TYPE string .

DATA : BEGIN OF upl OCCURS 0,

f(255) TYPE c,

END OF upl.

DATA: xmlupl TYPE string .

                                                              • FIRST PHASE

                                                              • FIRST PHASE

                                                              • FIRST PHASE

*----


Fetch Data

SELECT * FROM t001 INTO TABLE t001.

*----


XML

CALL TRANSFORMATION ('ID')

SOURCE tab = t001[]

RESULT XML xml_out.

*----


Convert to TABLE

CALL FUNCTION 'HR_EFI_CONVERT_STRING_TO_TABLE'

EXPORTING

i_string = xml_out

i_tabline_length = 100

TABLES

et_table = itab.

*----


Download

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

filetype = 'BIN'

filename = 'd:\xx.xml'

TABLES

data_tab = itab.

                                                              • SECOND PHASE

                                                              • SECOND PHASE

                                                              • SECOND PHASE

BREAK-POINT.

REFRESH t001.

CLEAR t001.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = 'D:\XX.XML'

filetype = 'BIN'

TABLES

data_tab = upl.

LOOP AT upl.

CONCATENATE xmlupl upl-f INTO xmlupl.

ENDLOOP.

*----


XML

CALL TRANSFORMATION ('ID')

SOURCE XML xmlupl

RESULT tab = t001[].

Thanks & regards

Sreeni

Read only

0 Likes
1,754

Hi ,

Thanks..In the above code can you separate the code which contains only XML file to ITAB?

Read only

Former Member
0 Likes
1,754

Hi,

You can go through the following code as an example:

&----


*& Report Z_XML_TO_SAP_PRG *

*& *

&----


*& *

*& *

&----


REPORT z_xml_to_sap_prg NO STANDARD PAGE HEADING MESSAGE-ID zp.

*----


*PURPOSE: This program transfers XML data into SAP internal table format

*The nodes in DOM can be stored as fields in SAP Internal table

*----


*----


  • type pool definitions

*----


TYPE-POOLS: ixml. "iXML Library Types

*----


  • type definitions

*----


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

data(256) TYPE x,

END OF t_xml_line.

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 if_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 t_xml_line

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. " String to hold filename

PARAMETERS: pa_file TYPE char1024 DEFAULT 'C:\reg.xml'.

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

PARAMETERS: pa_val TYPE char1 AS CHECKBOX.

*----


  • start of selection

*----


START-OF-SELECTION.

  • Creating the main iXML factory

l_ixml = cl_ixml=>create( ).

  • Creating a stream factory

l_streamfactory = l_ixml->create_stream_factory( ).

PERFORM get_xml_table CHANGING l_xml_table_size l_xml_table.

  • Wrap the table containing the file into a stream.

l_istream = l_streamfactory->create_istream_itable( table = l_xml_table

size = l_xml_table_size ).

  • Creating a document

l_document = l_ixml->create_document( ).

  • Creating a Parser

l_parser = l_ixml->create_parser( stream_factory = l_streamfactory

istream = l_istream

document = l_document ).

  • Validate a document

IF pa_val = 'X'.

l_parser->set_validating( mode = if_ixml_parser=>co_validate ).

ENDIF.

  • Parse the stream

IF l_parser->parse( ) <> 0.

IF l_parser->num_errors( ) <> 0.

DATA: parseerror TYPE REF TO if_ixml_parse_error,

str TYPE string,

i TYPE i,

count TYPE i,

index TYPE i.

count = l_parser->num_errors( ).

WRITE: count, ' parse errors have occured:'.

index = 0.

WHILE index < count.

parseerror = l_parser->get_error( index = index ).

i = parseerror->get_line( ).

WRITE: 'line: ', i.

i = parseerror->get_column( ).

WRITE: 'column: ', i.

str = parseerror->get_reason( ).

WRITE: str.

index = index + 1.

ENDWHILE.

ENDIF.

ENDIF.

  • Process the document

IF l_parser->is_dom_generating( ) EQ 'X'.

PERFORM process_dom USING l_document.

ENDIF.

&----


*& Form get_xml_table

&----


  • text

----


  • <--P_L_XML_TABLE_SIZE text

  • <--P_L_XML_TABLE text

----


FORM get_xml_table CHANGING p_l_xml_table_size

p_l_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 = pa_file.

  • Upload file from the client's workstation

CALL METHOD cl_gui_frontend_services=>gui_upload

EXPORTING

filename = l_filename

filetype = 'BIN'

IMPORTING

filelength = l_xml_table_size

CHANGING

data_tab = l_xml_table

EXCEPTIONS

OTHERS = 19.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

  • Writing the XML document to the screen

CLEAR l_str1.

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.

WRITE: /.

WRITE: /' XML File'.

WRITE: /.

LOOP AT l_itab INTO l_str1.

REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>horizontal_tab IN

l_str1 WITH space.

WRITE: / l_str1.

ENDLOOP.

WRITE: /.

ENDFORM. " get_xml_table

&----


*& Form process_dom

&----


  • text

----


  • -->P_L_DOCUMENT text

----


FORM process_dom USING document TYPE REF TO if_ixml_document.

DATA: node TYPE REF TO if_ixml_node,

iterator TYPE REF TO if_ixml_node_iterator,

nodemap TYPE REF TO if_ixml_named_node_map,

attr TYPE REF TO if_ixml_node,

name TYPE string,

prefix TYPE string,

value TYPE string,

indent TYPE i,

count TYPE i,

index TYPE i.

node ?= document.

CHECK NOT node IS INITIAL.

ULINE.

WRITE:/.

WRITE: /' DOM-TREE'.

WRITE: /.

IF node IS INITIAL.

EXIT.

ENDIF.

  • Create a node iterator

iterator = node->create_iterator( ).

  • Get current node

node = iterator->get_next( ).

  • Loop over all nodes

WHILE NOT node IS INITIAL.

indent = node->get_height( ) * 2.

indent = indent + 20.

CASE node->get_type( ).

WHEN if_ixml_node=>co_node_element.

  • element node

name = node->get_name( ).

nodemap = node->get_attributes( ).

WRITE: / 'ELEMENT :'.

WRITE: AT indent name COLOR COL_POSITIVE INVERSE.

IF NOT nodemap IS INITIAL.

  • attributes

count = nodemap->get_length( ).

DO count TIMES.

index = sy-index - 1.

attr = nodemap->get_item( index ).

name = attr->get_name( ).

prefix = attr->get_namespace_prefix( ).

value = attr->get_value( ).

WRITE: / 'ATTRIBUTE:'.

WRITE: AT indent name COLOR COL_HEADING INVERSE, '=',

value COLOR COL_TOTAL INVERSE.

ENDDO.

ENDIF.

WHEN if_ixml_node=>co_node_text OR

if_ixml_node=>co_node_cdata_section.

  • text node

value = node->get_value( ).

WRITE: / 'VALUE :'.

WRITE: AT indent value COLOR COL_GROUP INVERSE.

ENDCASE.

  • Advance to next node

node = iterator->get_next( ).

ENDWHILE.

ENDFORM. " process_dom

Read only

0 Likes
1,754

Hi Sangeeta,

Thank you Very much.But Can you tell me what is the final internal table in this code which is storing the XML file data?Becoz..from that itab i have to update the DB table.

regards

Read only

Former Member
0 Likes
1,754

check this code

this mat be useful to u

REPORT Z_NTVI_XML_TO_ABAP .

  • Load iXML Lib.

type-pools: ixml.

class cl_ixml definition load.

data: G_IXML type ref to if_ixml.

data: STREAMFACTORY type ref to if_ixml_stream_factory.

data: ISTREAM type ref to if_ixml_istream.

data: DOCUMENT type ref to if_ixml_document.

data: PARSER type ref to if_ixml_parser.

data: LV_FILE_URL type rlgrap-filename.

  • You should provide the parameter for file name

LV_FILE_URL = 'C:input_xml.xml'.

types: begin of XML_LINE,

DATA(256) type x,

end of XML_LINE.

types: begin of TY_HEADER,

CUST_NAME(20) type c,

CARD_NO(20) type c,

TAX_AMOUNT(10) type c,

TOTAL_AMOUNT(10) type c,

end of TY_HEADER.

types: begin of TY_ITEM,

ITEM_NO(4) type n,

ITEM_ID(20) type c,

ITEM_TITLE(50) type c,

ITEM_QTY(10) type c,

ITEM_UPRICE(10) type c,

end of TY_ITEM.

data: GV_HEADER type TY_HEADER.

data: GT_ITEM type standard table of TY_ITEM with header line.

data: XML_TABLE type table of XML_LINE,

XML_TABLE_SIZE type i.

  • The next step is creating the main factory for the iXML library:

G_IXML = cl_ixml=>create( ).

  • Now Create Stream Factory

STREAMFACTORY = G_IXML->create_stream_factory( ).

  • upload a file from the client's workstation

call function 'WS_UPLOAD'

exporting

filename = LV_FILE_URL

filetype = 'BIN'

importing

filelength = XML_TABLE_SIZE

tables

data_tab = XML_TABLE

exceptions

others = 11.

  • wrap the table containing the file into a stream

ISTREAM = STREAMFACTORY->create_istream_itable( table = XML_TABLE

size = XML_TABLE_SIZE )

.

  • Get the file data as Stream

*istream = streamfactory->create_istream_uri( public_id = lv_file_url

  • system_id = lv_file_url ).

  • Create XML Document instance

DOCUMENT = G_IXML->create_document( ).

  • Create parser Object

PARSER = G_IXML->create_parser( stream_factory = STREAMFACTORY

ISTREAM = istream

DOCUMENT = document ).

  • Parse an XML document into a DOM tree

*parser->parse( ).

  • Parsing Error Processing

if PARSER->parse( ) ne 0.

if PARSER->num_errors( ) ne 0.

data: PARSEERROR type ref to if_ixml_parse_error,

STR type STRING,

I type i,

COUNT type I,

INDEX type i.

COUNT = PARSER->num_errors( ).

write: COUNT, ' parse errors have occured:'.

INDEX = 0.

while INDEX < COUNT.

PARSEERROR = PARSER->get_error( INDEX = index ).

I = PARSEERROR->get_line( ).

write: 'line: ', i.

I = PARSEERROR->get_column( ).

write: 'column: ', i.

STR = PARSEERROR->get_reason( ).

write: STR.

INDEX = index + 1.

endwhile.

endif.

endif.

  • Close the stream since it �s not needed anymore

call method ISTREAM->close( ).

clear ISTREAM.

  • Now try to make it look good

  • data : lv_size type sytabix,

  • lv_ret_code type sysubrc.

*

  • data: lo_xml_document type ref to cl_xml_document.

*

  • field-symbols: <fs_xml_data> type any table.

*

  • lo_xml_document = document.

*

  • call method lo_xml_document->get_as_table

  • importing

  • table = <fs_xml_data>

  • size = lv_size

  • retcode = lv_ret_code

.

*

*data: items type ref to if_ixml_node_collection.

*

*items = document->get_elements_by_tag_name( name = 'Item' ).

*

*data: iterator type ref to if_ixml_node_iterator,

  • node type ref to if_ixml_node.

*

*iterator = document->create_iterator( ).

*node = iterator->get_next( ).

*

*while not node is initial.

*

    • do something with the node

*

  • ...

*

  • node = iterator->get_next( ).

*

*endwhile.

DATA : GV_NODE type ref to if_ixml_node.

DATA : GV_NODETEXT type STRING.

data: GV_FIRST_TIME.

GV_FIRST_TIME = 'X'.

GV_NODE = DOCUMENT.

GT_ITEM-item_no = 1.

perform GET_DATA tables GT_ITEM

using GV_NODE

changing GV_HEADER.

  • Last item is still not added.

append GT_ITEM.

write : GV_HEADER-cust_name,

GV_HEADER-card_no,

GV_HEADER-tax_amount,

GV_HEADER-total_amount.

loop at GT_ITEM.

write /:.

write : GT_ITEM-item_no,

GT_ITEM-item_id,

GT_ITEM-item_title,

GT_ITEM-item_qty,

GT_ITEM-item_uprice.

endloop.

*----


*

  • FORM Get_data *

*----


*

form get_data tables YT_ITEM structure GT_ITEM

using value(x_node) type ref to if_ixml_node

changing Y_HEADER type TY_HEADER.

data: INDENT type i.

data: PTEXT type ref to if_ixml_text.

data: STRING type string.

data: TEMP_STRING(100).

case X_NODE->get_type( ).

when if_ixml_node=>co_node_element.

STRING = X_NODE->get_name( ).

GV_NODETEXT = STRING.

when if_ixml_node=>co_node_text.

PTEXT ?= X_NODE->query_interface( IXML_IID_TEXT ).

if PTEXT->ws_only( ) is initial.

STRING = X_NODE->get_value( ).

case GV_NODETEXT.

when 'Customer'.

clear GV_HEADER.

when 'Name'.

move STRING to GV_HEADER-cust_name.

when 'Cardnum'.

move STRING to GV_HEADER-card_no.

when 'Tax'.

move STRING to GV_HEADER-tax_amount.

when 'Total'.

move STRING to GV_HEADER-total_amount.

  • Iteam details

when 'ID'.

move STRING to GT_ITEM-item_id.

when 'Title'.

move STRING to TEMP_STRING.

move TEMP_STRING to GT_ITEM-item_title.

when 'Quantity'.

move STRING to GT_ITEM-item_qty.

when 'UnitPrice'.

move STRING to GT_ITEM-item_uprice.

endcase.

endif.

endcase.

if GV_NODETEXT = 'Customer'.

clear GV_HEADER.

elseif GV_NODETEXT = 'Item'.

if GV_FIRST_TIME ne 'X'.

append GT_ITEM.

  • clear : gt_item.

GT_ITEM-item_no = gt_item-item_no + 1.

endif.

GV_FIRST_TIME = ' '.

endif.

  • Get the next child

X_NODE = x_node->get_first_child( ).

  • Recurse

while not X_NODE is initial.

perform GET_DATA tables GT_ITEM

using X_NODE

changing GV_HEADER.

X_NODE = x_node->get_next( ).

endwhile.

endform.

*sample xml file

*----

-


*

*

  • <Name>Satya Mandiga</Name>

  • <Cardnum>234 234 234 234</Cardnum>

  • </Customer>

*-

  • <ID>209</ID>

  • <Title>Duke: A Biography of the SAP Java Guru</Title>

  • <Quantity>1</Quantity>

  • <UnitPrice>Rs10.75</UnitPrice>

  • </Item>

*-

  • <ID>208</ID>

  • <Title>100% Pure: Making Cross Platform Deployment a Reality</Title>

  • <Quantity>1</Quantity>

  • <UnitPrice>Rs10.75</UnitPrice>

  • </Item>

*-

  • <ID>204</ID>

  • <Title>Making the Transition from C++ to the Java(tm)

*Language</Title>

  • <Quantity>1</Quantity>

  • <UnitPrice>Rs10.75</UnitPrice>

  • </Item>

*-

  • <ID>202</ID>

  • <Title>Web Servers for Fun and Profit</Title>

  • <Quantity>1</Quantity>

  • <UnitPrice>Rs10.75</UnitPrice>

  • </Item>

*-

  • <ID>210</ID>

  • <Title>I Think Not: Dukes Likeness to the Federation Insignia</Title>

*

  • <Quantity>1</Quantity>

  • <UnitPrice>Rs10.75</UnitPrice>

  • </Item>

  • </Manifest>

*-

  • <Subtotal>Rs53.75</Subtotal>

  • <Tax>Rs4.43</Tax>

  • <Total>Rs58.18</Total>

  • </Receipt>

  • </Order>

reward the points