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

Convert XML to ABAP

Former Member
0 Likes
1,962

Hi Experts,

I want to convert XML to ABAP.. I came to know through sdn...These are the two methods..

1. XSLT

2. Simple transformation

Can you tell me which is the best method among these.If not is there any other methods.Pls suggest...Provide me the code

Thanks.

9 REPLIES 9
Read only

Former Member
0 Likes
1,097

do F1 on statement CALL TRANSFORMATION.

G@urav.

Read only

0 Likes
1,097

Gupta,

Can u tell which is the best way among this.

Thanks.

Read only

0 Likes
1,097

check this link

Read only

0 Likes
1,097

Hans,

I have no actual experience in practice, however, I did get to know a bit more about XML and ABAP by reading the blogs from Tobias Trapp. In this blog series about XML processing in ABAP, you will get your answer. To get back to your question, there is no real good answer to your question. It will be a matter of what exactly you want to do. ST is an SAP proprietary language which is designed for converting XML in ABAP types and vice versa.

Bottom line, read the blog series, and hopefully you will get your answer from this.

[XML processing in ABAP|https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/1601] [original link is broken] [original link is broken] [original link is broken];

Read only

Former Member
0 Likes
1,097

Hi

&---------------------------------------------------------------------
*& Report z_xit_xml_check
&---------------------------------------------------------------------
report z_xit_xml_check.

class cl_ixml definition load.

type-pools: ixml.

types: begin of t_xml_line,
data(256) type x,
end of t_xml_line,

begin of tsfixml,
data(1024) type c,
end of tsfixml.


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,
l_xml_line type t_xml_line,
l_xml_table_size type i.
data: l_filename type string.


parameters: pa_file type char1024 default
'd:\joao\desenvolvimentos\fi\fact\teste.xml'.


Validation of XML file: Only DTD included in xml document is supported 
parameters: pa_val type char1 as checkbox.

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( ).


Create a Parser 
l_parser = l_ixml->create_parser( stream_factory = l_streamfactory
istream = l_istream
document = l_document ).


Validate a document 
if pa_val eq 'X'.

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


Parse the stream 
if l_parser->parse( ) ne 0.
if l_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 = 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
&--------------------------------------------------------------------
form get_xml_table changing l_xml_table_size type i
l_xml_table type standard table.



Local variable declaration 
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 a 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
&--------------------------------------------------------------------
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.

data: name2 type string,
name_root type string,
node_parent type ref to if_ixml_node,
node_root type ref to if_ixml_node,
num_children 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.

num_children = node->num_children( ).
case node->get_type( ).
when if_ixml_node=>co_node_element.

element node 
name = node->get_name( ).
nodemap = node->get_attributes( ).
node_root = node->get_root( ).
name_root = node_root->get_name( ).
write: / 'ELEMENT :'.
write: at indent name color col_positive inverse.
write: 'NUM_CHILDREN:', num_children.
write: 'ROOT:', name_root.
node_parent = node->get_parent( ).
name2 = node_parent->get_name( ).
write: 'NAME2: ' , name2.

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 :'.


mjprocha 
node_parent = node->get_parent( ).
write: at indent value color col_group inverse.
name2 = node_parent->get_name( ).
write: 'NAME2: ' , name2.
endcase.

advance to next node 
node = iterator->get_next( ).
endwhile.
endform. "process_dom

Hope this resolves your query.

<REMOVED BY MODERATOR>

Edited by: Alvaro Tejada Galindo on Aug 14, 2008 2:59 PM

Read only

0 Likes
1,097

Guys,

Hans Bauer had asked a simple question in plain english which everybody can understand. which is the best method XSLT or ST.

if you dont know the answer, do not type in , copy/paste some junk here.

Regards

Raja

Read only

0 Likes
1,097

The link i provided contains code as to how to convert xml to abap thats the reason why i provided it

Read only

Former Member
0 Likes
1,097

Hi Simple transformation method is best compare with XSLT.

Hari

Read only

Former Member
0 Likes
1,097

For some dome coding how to convert your data with XSLT, you can read a short tutorial I've made:

http://www.heidoc.net/joomla/en/technology-science/sap-and-xslt/5-tutorial-data-exchange-between-aba...

Now for the question: What is better: XSLT or ST?

Well, that depends...

Advantages XSLT:

  • Compatibility: XSLT is compatible with Internet Explorer, Java, .NET etc.

  • Flexibility: XSLT can read source data multiple times in any order, ST can read source data only once in serial order.

  • Conversion: XSLT can transform any given XML, ST can only transform from or to ABAP tables.

Advantages ST:

  • Symmetry: Use the same stylesheet in both directions.

  • Cleanliness: XSLT coding can be quite messy...

  • Speed: ST is faster

Best wishes,

Jan

Edited by: Jan Krohn on Jul 15, 2011 10:53 AM