Skip to Content
author's profile photo Former Member
Former Member

Help needed for Transforming XML into nested ITAB

Hi guys,

I am having problems converting this XML into a ITAB. Pls look thru and advise me on the XSLT codes cheers!

<b><u>XML</u></b>

<?xml version="1.0" encoding="iso-8859-1"?>

<gebizXML>

<PROCESS>TX-TIME-EXTENSION</PROCESS>

<PAYLOAD>TEST</PAYLOAD>

</gebizXML>

<u><b>THE PROGRAM</b></u>

TYPES: BEGIN OF GENERIC,

PROCESS TYPE STRING,

PAYLOAD TYPE STRING,

END OF GENERIC.

DATA: G_XML TYPE TABLE OF GENERIC.

DATA: xml_string TYPE string .

DATA: xslt_error TYPE REF TO cx_xslt_exception,

xslt_message TYPE string .

CLEAR xml_string .

CONCATENATE

`<?xml version="1.0" encoding="utf-8"?>`

`<G_XML timestamp="20051213T221545+08:00">`

`<PROCESS>TX SCHEDULE</PROCESS>`

`<PAYLOAD>this is a test of a string</PAYLOAD>`

`</G_XML>` INTO xml_string .

TRY .

CALL TRANSFORMATION (`ZSAMPLEX2A`)

SOURCE

XML xml_string

RESULT

G_XML = G_XML.

CATCH

cx_xslt_exception INTO xslt_error.

xslt_message = xslt_error->get_text( ).

ENDTRY.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Oct 17, 2006 at 09:25 AM

    check this thread where i had given XSLT program code sample for converting XML into nested itab.

    problem-converting-xml-back-to-structure-using-xsl

    Regards

    Raja

    Add a comment
    10|10000 characters needed characters exceeded

    • here is the code

      <u><b>XSLT</b></u>

      <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/abapxml" version="1.0">
        <xsl:template match="/">
          <asx:abap xmlns:asx="http://www.sap.com/abapxml">
            <asx:values>
              <G_XML>
      
                <xsl:for-each select="gebizXML">
                  <G_XML1>
                    <PROCESS>
                      <xsl:value-of select="PROCESS"/>
                    </PROCESS>
                    <PAYLOAD>
                      <xsl:value-of select="PAYLOAD"/>
                    </PAYLOAD>
                  </G_XML1>
                </xsl:for-each>
      
              </G_XML>
              
            </asx:values>
          </asx:abap>
        </xsl:template>
      </xsl:transform>

      <u><b>program to test</b></u>

      REPORT  y_test_xml_tran.
      TYPES: BEGIN OF GENERIC,
      PROCESS TYPE STRING,
      PAYLOAD TYPE STRING,
      END OF GENERIC.
      DATA: G_XML TYPE TABLE OF GENERIC.
      
      DATA: xml_string TYPE string .
      DATA: xslt_error	TYPE REF TO	cx_xslt_exception,
      xslt_message	TYPE	string .
      
      CLEAR xml_string .
      CONCATENATE
      
      `<?xml version="1.0" encoding="iso-8859-1"?>`
      `<gebizXML>`
      `<PROCESS>TX-TIME-EXTENSION</PROCESS>`
      `<PAYLOAD>TEST</PAYLOAD>`
      `</gebizXML>`
      
      
      INTO xml_string .
      
      
      TRY .
          CALL TRANSFORMATION (`YSIMPLE_XML_TO_ABAP`)
          SOURCE XML  xml_string
          RESULT     g_xml = g_xml.
        CATCH cx_xslt_exception INTO xslt_error.
          xslt_message = xslt_error->get_text( ).
      ENDTRY.
      
      

      Regards

      Raja

  • Posted on Oct 17, 2006 at 09:26 AM

    hi,

    chk this sample code.

    here is the code sample using ixml library .

    just create a report program and copy paste the following code.

    if time permits, later on i will post the xslt program which you can use it call transformation.

    REPORT y_test_xml.

    DATA: l_ixml TYPE REF TO if_ixml,

    l_ixml_sf TYPE REF TO if_ixml_stream_factory,

    l_istream TYPE REF TO if_ixml_istream,

    l_ostream TYPE REF TO if_ixml_ostream,

    l_booklist TYPE REF TO if_ixml_element,

    l_document TYPE REF TO if_ixml_document,

    l_parser TYPE REF TO if_ixml_parser,

    l_root_element TYPE REF TO if_ixml_element,

    l_book_record TYPE REF TO if_ixml_element,

    l_date TYPE REF TO if_ixml_element ,

    l_time TYPE REF TO if_ixml_element ,

    l_book_group TYPE REF TO if_ixml_element,

    l_book_cat TYPE REF TO if_ixml_element ,

    others TYPE REF TO if_ixml_element ,

    link TYPE REF TO if_ixml_element ,

    description TYPE REF TO if_ixml_element ,

    xml TYPE xstring ,

    size TYPE i ,

    l_xml TYPE REF TO cl_xml_document .

    DATA: xml_out TYPE string ,

    temp_string TYPE string .

    TYPES: BEGIN OF add_category,

    category_id(10),

    category_desc(40),

    END OF add_category,

    add_t_category TYPE add_category OCCURS 0 .

    TYPES: BEGIN OF add_book_grp,

    book_category TYPE add_t_category,

    END OF add_book_grp,

    add_t_book_grp TYPE add_book_grp OCCURS 0.

    TYPES: BEGIN OF add_book,

    book_num(10) TYPE c,

    short_desc(40) TYPE c,

    book_group TYPE add_t_book_grp,

    END OF add_book,

    add_t_book TYPE add_book OCCURS 0.

    TYPES: BEGIN OF type_data,

    date TYPE sy-datum,

    time TYPE sy-uzeit,

    book_record TYPE add_t_book,

    END OF type_data.

    DATA: i_data TYPE type_data OCCURS 0 WITH HEADER LINE.

    DATA: itab LIKE soli OCCURS 0 WITH HEADER LINE.

    DATA: cat_wa TYPE add_category ,

    bk_gp_wa TYPE add_book_grp ,

    bk_rec_wa TYPE add_book ,

    bk_wa LIKE LINE OF i_data .

    DATA: cat_tab TYPE STANDARD TABLE OF add_category ,

    bk_gp_tab TYPE STANDARD TABLE OF add_book_grp ,

    bk_rec_tab TYPE STANDARD TABLE OF add_book .

    MOVE: '03' TO cat_wa-category_id ,

    ' BK GP 3' TO cat_wa-category_desc .

    APPEND cat_wa TO cat_tab .

    MOVE: '02' TO cat_wa-category_id ,

    ' BK GP 2' TO cat_wa-category_desc .

    APPEND cat_wa TO cat_tab .

    bk_gp_wa-book_category = cat_tab.

    APPEND bk_gp_wa TO bk_gp_tab .

    *

    MOVE: '0012345678' TO bk_rec_wa-book_num ,

    'OMS book' TO bk_rec_wa-short_desc .

    bk_rec_wa-book_group = bk_gp_tab .

    APPEND bk_rec_wa TO bk_rec_tab .

    CLEAR:bk_gp_tab, cat_tab .

    REFRESH :bk_gp_tab, cat_tab .

    MOVE: '01' TO cat_wa-category_id ,

    ' BK GP 1' TO cat_wa-category_desc .

    APPEND cat_wa TO cat_tab .

    MOVE: '09' TO cat_wa-category_id ,

    ' BK GP 9' TO cat_wa-category_desc .

    APPEND cat_wa TO cat_tab .

    bk_gp_wa-book_category = cat_tab.

    APPEND bk_gp_wa TO bk_gp_tab .

    MOVE: '00123456789' TO bk_rec_wa-book_num ,

    'SAP book' TO bk_rec_wa-short_desc .

    bk_rec_wa-book_group = bk_gp_tab .

    APPEND bk_rec_wa TO bk_rec_tab .

    MOVE: sy-datum TO bk_wa-date ,

    sy-uzeit TO bk_wa-time .

    bk_wa-book_record = bk_rec_tab .

    APPEND bk_wa TO i_data .

    CLEAR: cat_wa , bk_gp_wa ,bk_rec_wa , bk_wa .

    l_ixml = cl_ixml=>create( ).

    l_ixml_sf = l_ixml->create_stream_factory( ).

    l_document = l_ixml->create_document( ).

    l_root_element = l_document->create_element( name = 'asx:abap' ).

    l_root_element->set_attribute( name = 'xmlns:asx' value = 'http://www.sap.com/abapxml' ) .

    l_root_element->set_attribute( name = 'version' value = '1.0' ).

    l_document->append_child( new_child = l_root_element ).

    others = l_document->create_simple_element( parent = l_root_element name = 'asx:values' ).

    l_booklist = l_document->create_simple_element( parent = others name = 'BOOKLIST' ).

    LOOP AT i_data INTO bk_wa .

    CLEAR temp_string .

    MOVE: bk_wa-date TO temp_string .

    l_date = l_document->create_simple_element( parent = l_booklist name = 'DATE' value = temp_string ).

    CLEAR temp_string .

    MOVE: bk_wa-time TO temp_string .

    l_time = l_document->create_simple_element( parent = l_booklist name = 'TIME' value = temp_string ).

    LOOP AT bk_wa-book_record INTO bk_rec_wa .

    l_book_record = l_document->create_simple_element( parent = l_booklist name = 'BOOK_RECORD' ) .

    CLEAR temp_string .

    MOVE: bk_rec_wa-book_num TO temp_string .

    l_date = l_document->create_simple_element( parent = l_book_record name = 'BOOK_NUM' value = temp_string ).

    CLEAR temp_string .

    MOVE: bk_rec_wa-short_desc TO temp_string .

    l_time = l_document->create_simple_element( parent = l_book_record name = 'SHORT_DESC' value = temp_string ).

    l_book_group = l_document->create_simple_element( parent = l_book_record name = 'BOOK_GROUP' ).

    LOOP AT bk_rec_wa-book_group INTO bk_gp_wa .

    LOOP AT bk_gp_wa-book_category INTO cat_wa .

    l_book_cat = l_document->create_simple_element( parent = l_book_group name = 'BOOK_CATEGORY' ).

    CLEAR temp_string .

    MOVE: cat_wa-category_id TO temp_string .

    l_date = l_document->create_simple_element( parent = l_book_cat name = 'CATEGORY_ID' value = temp_string ).

    CLEAR temp_string .

    MOVE: cat_wa-category_desc TO temp_string .

    l_time = l_document->create_simple_element( parent = l_book_cat name = 'CATEGORY_DESC' value = temp_string ).

    ENDLOOP .

    ENDLOOP .

    ENDLOOP .

    ENDLOOP .

    l_ostream = l_ixml_sf->create_ostream_xstring( xml ).

    l_document->render( ostream = l_ostream ).

    CREATE OBJECT l_xml.

    CALL METHOD l_xml->parse_xstring

    EXPORTING

    stream = xml.

    l_xml->render_2_string(

    EXPORTING

    pretty_print = 'X'

    IMPORTING

    • RETCODE = RETCODE

    stream = xml_out

    size = size

    ).

    CALL METHOD l_xml->display.

    Hope this is helpful.

    do let us know how it goes.

    Regards

    **************************8

    to read the xml data to abap itab you could parse node by node or write a XSLT to map it to your itab or use the following method. (add the following code to the earlier program)

    data: result_xml type standard table of smum_xmltb .

    data: return type standard table of bapiret2 .

    after the statement

    l_document->render( ostream = l_ostream ).

    add

    • converting xml to itab

    call function 'SMUM_XML_PARSE'

    exporting

    xml_input = xml

    tables

    xml_table = result_xml

    return = return .

    now check the result_xml itab.

    rgds

    anver

    if hlpful pls mark points

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 03:25 AM

    Thanks Raja! Appreciated!

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.