Skip to Content
avatar image
Former Member

Parsing XML file to Internal table.

Hi Experts,

I know how to parse XML to ITAB.I have code for that.

But in my XML file if i have a field like Longdescription of 40 or 50 lines..

Ex:in an XML file:

<field name="longDescription">

<p>bvfhhjbdgbgkjdbjjbkjbkjbdgjkhilghjbgkbgjkbgkbgkbgkgdgbjkngjkdbbhuihgdjklgdhjklghjkghjkdghjklghjkghjkghjkghjkghjkghjkhgjkhgjkghjghgjklhgdjhghlghlgkh;ghghtuioyiotyiotiotuoi589589uiofhiofhl;fhgfhghioghdioghiohgfdhjklsfh8i4y894yiadgvghadcghvfghhjljioppkjdfspjiosfhfs578y8945yfhdsihghighighihsfiohgiohgioghoigh89y4u492uojsfihjfklfkhgfghvdbvhjbdbvdjbvjdbvhjdbvjhbvdhjdvhjdbvhjdbvhjhjhjdbvhj</p>

</field>

So the whole description can be transferd into ITAB?...

Has any body encountered such problem?...If so please let me know how to do it?

Regards

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

3 Answers

  • Best Answer
    avatar image
    Former Member
    May 29, 2007 at 10:06 AM

    Hi experts!!!

    when i use call transformation it creates an exact xml file.

    But the problem comes because of a large internal table,having hundred of records,in that case when using the write statement to store the data in a file on the server, it is not saved correctly or may be is not viewed properly and hence cannot be used properly.Maybe it has anything to do with the size of string data type. Can anyone suggest anything in this regard?

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Ravi

      Did you manage to get solution in case of large records. have you changed the size or ? if you found any method regarding this please email me since Im facing the same.

      Cheers

      Tony

  • Aug 09, 2006 at 12:31 PM

    yes for that you need to have a field of type string in your itab.

    Regards

    Raja

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Durairaj Athavan Raja

      Hi ,

      I am using the following code for Parsing XML file:

      *----


      • type pool definitions

      *----


      TYPE-POOLS: ixml. "iXML Library Types

      *----


      • type definitions

      *----


      TYPES: BEGIN OF t_xml_line, "Structure for holding XML data

      data(256) TYPE x,

      END OF t_xml_line.

      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 : v_text(1000).

      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, " XML Table of the structure

      *t_xml_line

      l_xml_line TYPE t_xml_line, " Record of structure t_xml_line

      l_xml_table_size TYPE i. " XML table size

      DATA: l_filename TYPE string. " String to hold filename

      DATA : NUM TYPE I.

      DATA : HOWMANY TYPE I.

      DATA : CTR TYPE I.

      DATA : MYIND TYPE SY-INDEX.

      data: begin of i_final occurs 0,

      value(1000),

      end of i_final.

      data: begin of it_final occurs 0,

      finaltext(65535),

      end of it_final.

      data: v_temp type i.

      data : it_final1 like it_final occurs 0 with header line.

      DATA : V_TABIX TYPE SY-TABIX.

      data: p_file like rlgrap-filename.

      p_file = 'D:\test.TXT'.

      *PARAMETERS: pa_file TYPE char1024 DEFAULT 'C:\Test1.html.xml'.

      *parameters: pa_file type localfile.

      parameters: pa_file type rlgrap-filename.

      data: begin of IT_SERENA occurs 0,

      ID LIKE COMM_PCAT_CTY-ID,

      TEXT(65000),

      end of IT_SERENA.

      DATA:WA_SERENA like IT_SERENA.

      data: productnum type COMT_PRWB_OBJECT.

      data: proguid type COMT_PRODUCT_GUID.

      *

      DATA: BASIC_TEXT LIKE THEAD OCCURS 0 WITH HEADER LINE.

      DATA: YGUID LIKE COMM_PCAT_CTY-GUID,

      ZGUID LIKE COMM_PCAT_CTV-PARENT_CATEGORY.

      DATA: BASICTEXT like TLINE OCCURS 0 WITH HEADER LINE.

      DATA: IT_THEAD LIKE THEAD OCCURS 0 WITH HEADER LINE.

      • Validation of XML file: Only DTD included in XML document is supported

      *PARAMETERS: pa_val TYPE char1 AS CHECKBOX.

      at selection-screen on value-request for pa_file.

      CALL FUNCTION 'KD_GET_FILENAME_ON_F4'

      EXPORTING

      • PROGRAM_NAME = SYST-REPID

      • DYNPRO_NUMBER = SYST-DYNNR

      • FIELD_NAME = ' '

      STATIC = 'X'

      • MASK = ' '

      CHANGING

      FILE_NAME = pa_file

      • EXCEPTIONS

      • MASK_TOO_LONG = 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.

      *----


      • start of selection

      *----


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

      • Creating a Parser

      l_parser = l_ixml->create_parser( stream_factory = l_streamfactory

      istream = l_istream

      document = l_document ).

      • Validate a document

      • IF pa_val = 'X'.

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

      • ENDIF.

      • Parse the stream

      IF l_parser->parse( ) <> 0.

      IF l_parser->num_errors( ) <> 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.

      • perform download.

      ENDIF.

      &----


      *& Form get_xml_table

      &----


      • text

      ----


      • <--P_L_XML_TABLE_SIZE text

      • <--P_L_XML_TABLE text

      ----


      FORM get_xml_table CHANGING p_l_xml_table_size

      p_l_xml_table.

      • Local variable declarations

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

      &----


      • text

      ----


      • -->P_L_DOCUMENT text

      ----


      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,

      • value(1000),

      indent TYPE i,

      count TYPE i,

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

      CASE node->get_type( ).

      WHEN if_ixml_node=>co_node_element.

      • element node

      name = node->get_name( ).

      nodemap = node->get_attributes( ).

      *WRITE: / 'ELEMENT :'.

      *WRITE: AT indent name COLOR COL_POSITIVE INVERSE.

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

      *WRITE: AT indent value COLOR COL_GROUP INVERSE.

      concatenate i_final-value value into i_final-value separated by'|'.

      append i_final.

      clear i_final.

      ENDCASE.

      • Advance to next node

      node = iterator->get_next( ).

      ENDWHILE.

      loop at i_final.

      split i_final-value at '|' into table it_final.

      insert lines of it_final into table it_final1.

      delete it_final1 where finaltext = space.

      endloop.

      In the above code you can observe 2 methods:

      1.value = attr->get_value( ).

      2.value = node->get_value( ).

      So in the second method the actual data is comind and storing in variable 'value' which is declared as type string.

      So when i exicute it is retrieving only 132 characters into value.

      This method get_value is in inerface 'IF_IXML_NODE'

      Can any body provide some solution for this.

      Regards

  • avatar image
    Former Member
    Aug 09, 2006 at 12:28 PM

    hi check the follwoing web log::

    <b>/people/tobias.trapp/blog/2005/05/04/xml-processing-in-abap-part-1

    Regards

    Ashok P

    Add comment
    10|10000 characters needed characters exceeded