XML data into itab

Hi Experts,
I have a XML file on my desk top.
How to upload data from XML file into ITAB?
Regards
Advertisement
Reply

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