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 internal table

former_member842213
Participant
0 Likes
1,028

Hi all,

I have a requirement like , i need to convert xml file to internal table,and the file will be present in application server,, i got a code from sdn , it is working fine for presentation server file(conversion of xml to internal table),, and i want to know is there any function module to do the same,,,,,,,,,,,

Plz help me urgent...............

Thanks.

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
856

Hello San

The following sample report <b>BCCIIXMLT2</b> (package SIXML_TEST, on ECC 5.0) shows how to process XML files stored on the application server.

*  BCCIIXMLT2 - sample program
*
*  This sample program shows how to read an XML document from a file
*  in the appserver's file system, parse the document into a DOM
*  representation and display the content as a list.
*  Additionally the DOM representation is rendered back into an XML
*  stream and stored as an output XML file on the appserver machine.
*
*  This sample program uses ABAP strings to represent the XML input
*  and output streams. See BCCIIXMLT1 for using internal tables.
*

report bcciixmlt2 message-id bcciixmlt3_msg line-size 1000.

start-of-selection.

parameters: filename(80) type c
              default 'tempREC-xml-19980210.xml'. "#EC NOTEXT

*-- read the XML document from a dataset into a string
data: inputstring type xstring.
open dataset filename for input in binary mode.
if sy-subrc ne 0.
  message e000.
endif.
read dataset filename into inputstring.
close dataset filename.

*-- create the main factory
data: pixml type ref to if_ixml.
pixml = cl_ixml=>create( ).

*-- create the initial document
data: pdocument type ref to if_ixml_document.
pdocument = pixml->create_document( ).

*-- create the stream factory
data: pstreamfactory type ref to if_ixml_stream_factory.
pstreamfactory = pixml->create_stream_factory( ).

*-- create a stream for the input (string)
data: pistream type ref to if_ixml_istream.
pistream = pstreamfactory->create_istream_xstring( inputstring ).


*-- create the parser
data: pparser type ref to if_ixml_parser.
pparser = pixml->create_parser( stream_factory  = pstreamfactory
                                  istream       = pistream
                                  document      = pdocument ).

*-- parse the stream
if pparser->parse( ) ne 0.
  if pparser->num_errors( ) ne 0.
    data: count type i.
    count = pparser->num_errors( ).
    write: count, ' parse errors have occured:'.    "#EC NOTEXT
    data: pparseerror type ref to if_ixml_parse_error,
          i type i.
    data: index type i value 0.
      while index < count.
        pparseerror = pparser->get_error( index = index ).
        i = pparseerror->get_line( ).
        write: 'line: ', i.                         "#EC NOTEXT
        i = pparseerror->get_column( ).
        write: 'column: ', i.                       "#EC NOTEXT
        data: string type string.
        string = pparseerror->get_reason( ).
        write: string.
        index = index + 1.
      endwhile.
  endif.
endif.

*-- we don't need the stream any more, so let's close it...
clear pistream.


*-- just for fun: render the DOM back into an output stream/xstring
data: postream type ref to if_ixml_ostream,
      outputstring type xstring.
postream = pstreamfactory->create_ostream_xstring( outputstring ).
call method pdocument->render( ostream = postream ).

*-- write the XML document as a dataset
data: dsn(80) type c.
concatenate filename '.out' into dsn.              "#EC NOTEXT
transfer outputstring to dsn.


*-- print the whole DOM tree as a list...
data: pnode type ref to if_ixml_node.
pnode = pdocument.
perform print_node using pnode.





*---------------------------------------------------------------------*
*       FORM print_node                                               *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
form print_node using value(pnode) type ref to if_ixml_node.
  data: indent      type i.
  data: ptext       type ref to if_ixml_text.
  data: string      type string.

  indent  = pnode->get_height( ) * 2.

  case pnode->get_type( ).
    when if_ixml_node=>co_node_element.
      string  = pnode->get_name( ).
      write: at /indent '<', string, '> '.          "#EC NOTEXT
    when if_ixml_node=>co_node_text.
       ptext ?= pnode.
       if ptext->ws_only( ) is initial.
        string = pnode->get_value( ).
        write: at /indent string.
       endif.
  endcase.

  pnode = pnode->get_first_child( ).
  while not pnode is initial.

     perform print_node using pnode.
     pnode = pnode->get_next( ).

  endwhile.

endform.

Regards

Uwe

5 REPLIES 5
Read only

uwe_schieferstein
Active Contributor
0 Likes
857

Hello San

The following sample report <b>BCCIIXMLT2</b> (package SIXML_TEST, on ECC 5.0) shows how to process XML files stored on the application server.

*  BCCIIXMLT2 - sample program
*
*  This sample program shows how to read an XML document from a file
*  in the appserver's file system, parse the document into a DOM
*  representation and display the content as a list.
*  Additionally the DOM representation is rendered back into an XML
*  stream and stored as an output XML file on the appserver machine.
*
*  This sample program uses ABAP strings to represent the XML input
*  and output streams. See BCCIIXMLT1 for using internal tables.
*

report bcciixmlt2 message-id bcciixmlt3_msg line-size 1000.

start-of-selection.

parameters: filename(80) type c
              default 'tempREC-xml-19980210.xml'. "#EC NOTEXT

*-- read the XML document from a dataset into a string
data: inputstring type xstring.
open dataset filename for input in binary mode.
if sy-subrc ne 0.
  message e000.
endif.
read dataset filename into inputstring.
close dataset filename.

*-- create the main factory
data: pixml type ref to if_ixml.
pixml = cl_ixml=>create( ).

*-- create the initial document
data: pdocument type ref to if_ixml_document.
pdocument = pixml->create_document( ).

*-- create the stream factory
data: pstreamfactory type ref to if_ixml_stream_factory.
pstreamfactory = pixml->create_stream_factory( ).

*-- create a stream for the input (string)
data: pistream type ref to if_ixml_istream.
pistream = pstreamfactory->create_istream_xstring( inputstring ).


*-- create the parser
data: pparser type ref to if_ixml_parser.
pparser = pixml->create_parser( stream_factory  = pstreamfactory
                                  istream       = pistream
                                  document      = pdocument ).

*-- parse the stream
if pparser->parse( ) ne 0.
  if pparser->num_errors( ) ne 0.
    data: count type i.
    count = pparser->num_errors( ).
    write: count, ' parse errors have occured:'.    "#EC NOTEXT
    data: pparseerror type ref to if_ixml_parse_error,
          i type i.
    data: index type i value 0.
      while index < count.
        pparseerror = pparser->get_error( index = index ).
        i = pparseerror->get_line( ).
        write: 'line: ', i.                         "#EC NOTEXT
        i = pparseerror->get_column( ).
        write: 'column: ', i.                       "#EC NOTEXT
        data: string type string.
        string = pparseerror->get_reason( ).
        write: string.
        index = index + 1.
      endwhile.
  endif.
endif.

*-- we don't need the stream any more, so let's close it...
clear pistream.


*-- just for fun: render the DOM back into an output stream/xstring
data: postream type ref to if_ixml_ostream,
      outputstring type xstring.
postream = pstreamfactory->create_ostream_xstring( outputstring ).
call method pdocument->render( ostream = postream ).

*-- write the XML document as a dataset
data: dsn(80) type c.
concatenate filename '.out' into dsn.              "#EC NOTEXT
transfer outputstring to dsn.


*-- print the whole DOM tree as a list...
data: pnode type ref to if_ixml_node.
pnode = pdocument.
perform print_node using pnode.





*---------------------------------------------------------------------*
*       FORM print_node                                               *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
form print_node using value(pnode) type ref to if_ixml_node.
  data: indent      type i.
  data: ptext       type ref to if_ixml_text.
  data: string      type string.

  indent  = pnode->get_height( ) * 2.

  case pnode->get_type( ).
    when if_ixml_node=>co_node_element.
      string  = pnode->get_name( ).
      write: at /indent '<', string, '> '.          "#EC NOTEXT
    when if_ixml_node=>co_node_text.
       ptext ?= pnode.
       if ptext->ws_only( ) is initial.
        string = pnode->get_value( ).
        write: at /indent string.
       endif.
  endcase.

  pnode = pnode->get_first_child( ).
  while not pnode is initial.

     perform print_node using pnode.
     pnode = pnode->get_next( ).

  endwhile.

endform.

Regards

Uwe

Read only

0 Likes
856

hi

can i use the same in 4.7c...

Read only

0 Likes
856

Hello San

You have to check whether the classes are already available on SAP Basis 6.20 (=> 4.70).

Regards

Uwe

Read only

0 Likes
856

thanks it is working fine,, may i know how to transfer the xml file from one directory to another directory in application server...i tried , the file is transfered but it doesnt ve any data..

Read only

0 Likes
856

Hello San

You can use the function module <b>EPS_FTP_PUT</b>.

Regards

Uwe