SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

XML to ABAP in 4.6C using iXML Library, without XSLT



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Conversion
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sun Oct 14, 2007 9:48 pm    Post subject: XML to ABAP in 4.6C using iXML Library, without XSLT Reply with quote

XML to ABAP in 4.6C ( Inbound ) using iXML Library, without XSLT.

Requirement:
Processing XML to ABAP in 4.6C ( Inbound ) using iXML Library, without XSLT.

Processing:
This program is an example for processing XML files into SAP ( Inbound Interface ) without using XSLT approach and hecnce it can be used even on SAP 4.6C. It uses iXML library to process XML document using DOM parsing. A sample input XML file input_xml.xml is taken as example and kept in C directory.

Code:

*---------------------------------------------------------------------*
* Written By   :Ram Manohar Tiwari
* Presented By :www.rmtiwari.com
* Functionality: This program is an example for processing XML files
*                into SAP ( Inbound Interface ) without using XSLT
*                approach and  hence it can be used even on SAP 4.6C.
*                It uses iXML library to process XML document using DOM
*                parsing. A sample input XML file input_xml.xml is taken
*                as example and kept in C directory.
*---------------------------------------------------------------------*

REPORT  Z_RMTIWARI_XML_TO_ABAP_46C              .

* 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.


input_xml.xml
Code:

 <?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>
- <Order>
- <Customer>
  <Name>Bill Buckram</Name>
  <Cardnum>234 234 234 234</Cardnum>
  </Customer>
- <Manifest>
- <Item>
  <ID>209</ID>
  <Title>Duke: A Biography of the Java Evangelist</Title>
  <Quantity>1</Quantity>
  <UnitPrice>$10.75</UnitPrice>
  </Item>
- <Item>
  <ID>208</ID>
  <Title>100% Pure: Making Cross Platform Deployment a Reality</Title>
  <Quantity>1</Quantity>
  <UnitPrice>$10.75</UnitPrice>
  </Item>
- <Item>
  <ID>204</ID>
  <Title>Making the Transition from C++ to the Java(tm) Language</Title>
  <Quantity>1</Quantity>
  <UnitPrice>$10.75</UnitPrice>
  </Item>
- <Item>
  <ID>202</ID>
  <Title>Web Servers for Fun and Profit</Title>
  <Quantity>1</Quantity>
  <UnitPrice>$10.75</UnitPrice>
  </Item>
- <Item>
  <ID>210</ID>
  <Title>I Think Not: Dukes Likeness to the Federation Insignia</Title>
  <Quantity>1</Quantity>
  <UnitPrice>$10.75</UnitPrice>
  </Item>
  </Manifest>
- <Receipt>
  <Subtotal>$53.75</Subtotal>
  <Tax>$4.43</Tax>
  <Total>$58.18</Total>
  </Receipt>
  </Order>
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Conversion All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.