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

Parse XML data to ABAP internal table

pavneet_rana
Active Participant
0 Likes
1,298

Hi to all,

i have writen a code to Parse XML data to ABAP internal table .

Now i need that the final table result_xml which contain XML data need to devide into other internal table itab_wkc according to the value in result_xml for WORK CENTER.

but the value are repeating TWICE for WORK CENTER and ktext in internal table itab_wkc , i need that value should be updated in itab_wkc only 1 time.

types: begin of ty_st_wkc,

work_center type arbpl,

ktext(40) type c ,

end of ty_st_wkc.

data: itab_wkc type table of ty_st_wkc,

wa_wkc like line of itab_wkc.

types: begin of xml_line,

data(256) type x,

end of xml_line.

data: xml_tab type table of xml_line.

data:xmldata type xstring .

data: size type i .

data: result_xml type standard table of smum_xmltb ,

wa type smum_xmltb.

data: return type standard table of bapiret2 .

data:filename type string value 'C:\.xml'.

call function 'GUI_UPLOAD'

exporting

filename = filename

filetype = 'BIN'

has_field_separator = ' '

header_length = 0

importing

filelength = size

tables

data_tab = xml_tab

exceptions

others = 1.

call function 'SCMS_BINARY_TO_XSTRING'

exporting

input_length = size

  • FIRST_LINE = 0

  • LAST_LINE = 0

importing

buffer = xmldata

tables

binary_tab = xml_tab

exceptions

failed = 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.

call function 'SMUM_XML_PARSE'

exporting

xml_input = xmldata

tables

xml_table = result_xml

return = return.

loop at result_xml into wa.

case wa-cname.

when 'WORK_CENTER'.

wa_wkc-work_center = wa-cvalue .

append wa_wkc to itab_wkc.

when 'KTEXT'.

wa_wkc-ktext = wa-cvalue .

append wa_wkc to itab_wkc.

endcase.

endloop.

loop at itab_wkc into wa_wkc .

write: /2 wa_wkc-work_center ,

wa_wkc-ktext.

endloop.

please can any one provide me the solution for that.

Regards

Pavneet Rana

4 REPLIES 4
Read only

peter_ruiz2
Active Contributor
0 Likes
1,036

Hi Pavneet,

Change the part of your code to this.


data: l_lineno type p value 0.

loop at result_xml into wa.

case wa-cname.
when 'WORK_CENTER'.
wa_wkc-work_center = wa-cvalue .
append wa_wkc to itab_wkc.
add 1 to l_lineno.

when 'KTEXT'.
wa_wkc-ktext = wa-cvalue .
modify itab_wkc from wa_wkc transporting ktext index l_lineno.
endcase.

modify table
endloop.

regards,

Peter

Edited by: Peter Ruiz on Aug 12, 2011 3:57 PM

Read only

0 Likes
1,036

Thanks for Reply,

This codes work fine.

But if result_xml table contain multiple KTEXT.

example:

loop at result_xml into wa.

case wa-cname.

when 'WORK_CENTER'.

wa_wkc-work_center = wa-cvalue .

append wa_wkc to itab_wkc.

add 1 to l_lineno1.

when 'KTEXT'.

wa_wkc-ktext = wa-cvalue .

modify itab_wkc index l_lineno1 from wa_wkc

transporting ktext.

when 'CONTROL_KEY'.

wa_ck-control_key = wa-cvalue.

append wa_ck to itab_ck.

add 1 to l_lineno2.

when 'KTEXT'.

wa_ck-txt = wa-cvalue.

modify itab_ck index l_lineno2 from wa_ck

transporting txt.

result_xml table have

WORK_CENTER and KTEXT ( WORK_CENTER description )

CONTROL_KEY and KTEXT ( CONTROL_KEY description )

then the given code work for WORK_CENTER and KTEXT ( WORK_CENTER description ) and CONTROL_KEY , and dont work for KTEXT ( CONTROL_KEY description ), as in CASE statement it find first KTEXT and Second KTEXT is ignore.

please can any one provide me the solution that, so that i can polutae FIRST itab_wkc for WORK_CENTER and KTEXT ( WORK_CENTER description ) and SECOND tab_ck for CONTROL_KEY and KTEXT ( CONTROL_KEY description ) from result_xml table .

I shall be thankful to you for this.

Regards

Pavneet Rana

Edited by: pavneet rana on Aug 16, 2011 12:33 PM

Edited by: pavneet rana on Aug 16, 2011 12:38 PM

Read only

0 Likes
1,036

Hi Pavneet Rana,

I don't know this function module. But you could use the official XML transformations (CALL TRANSFORMATION and transaction STRANS) if you use WebAS 6.10 or later. Could you provide an excerpt of your current XML result (or the expected result), so that we can answer this other way.

Sandra

Read only

0 Likes
1,036

Hi Pavneet,

You need to include some validation on your code to meet your requirement.


data: l_lineno1 type p value 0,
         l_lineno2 type p value 0.

data: l_wkc type c,
         l_ck type c.
 
clear: l_wkc, l_ck.

loop at result_xml into wa.
 
case wa-cname.
when 'WORK_CENTER'.
wa_wkc-work_center = wa-cvalue .
l_wkc = 'X'.
clear l_ck.
append wa_wkc to itab_wkc.
add 1 to l_lineno1.

when 'CONTROL_KEY'.
wa_ck-control_key = wa-cvalue.
l_ck = 'X'.
clear l_wkc.
append wa_ck to itab_ck.
add 1 to l_lineno2.

when 'KTEXT'.
if l_wkc = 'X'.
wa_wkc-ktext = wa-cvalue .
modify itab_wkc from wa_wkc transporting ktext index l_lineno1.
else.
wa_ck-ktext = wa-cvalue.
modify itab_ck from wa_ck transporting ktext index l_lineno2.
endif.

clear l_wkc, l_ck.
endcase.
 
modify table
endloop.

Regards,

Peter