cancel
Showing results for 
Search instead for 
Did you mean: 

SAVE_TOXML <-> LOAD_FROMXML

Former Member
0 Kudos

Hi experts,

I try to save my WebDynpro context in XML structure to DBase, to restore it at a later time ... (should be kind of storing a draft of a form) ...

The SAVE_TOXML is quite easy because of the TO_XML() method, but I struggle with the reverse binding. Here we go:

First the SAVE_TOXML, which works like it should:


method save_toxml .
**********************************************************************
* Saves Context Root to XML String in Database
**********************************************************************

  data lv_data_string type string.
  lv_data_string = wd_context->to_xml( ).

  data ls_save type zkdp_gaxmldrafts.
  ls_save-mandt         = sy-mandt.
  ls_save-atluser       = sy-uname.
  concatenate sy-datlo sy-timlo into ls_save-timestamp.
* just for testing purposes with static ID
  ls_save-gaid          = '0000000001'.
  ls_save-xmlstring     = lv_data_string.

  insert into zkdp_gaxmldrafts values ls_save.

endmethod.

The stored XMLSTRING is like

<context>

<node1><attr1>12345</attr1><attr2>test</attr2></node1>

<node2>...</node2>

...

</context> which is ok.

And now how to get it back is not so easy?

I found the hint to use:

cl_wdr_xml_convert_util=>if_wd_client_conversion_util~string_to_struct

and to bind the sturcture back, but I struggle while using this functions ...

But how can I reference (assign) the table to bind to the wd_context?

Here we go:


method load_fromxml .
**********************************************************************
* Load GA Data from DBase
**********************************************************************

  data lv_data_string         type string.
  data lv_typedescr           type ref to cl_abap_typedescr.

  field-symbols:
                 <fs_data>    type data.

* just for testing purposes with static ID
  select single xmlstring from zkdp_gaxmldrafts into lv_data_string
    where atluser = sy-uname
    and   gaid    = '0000000001'.

*   get typedefinition of context table
*   needed for conversion
  call method cl_abap_typedescr=>describe_by_object_ref
    exporting
      p_object_ref         = wd_context
    receiving
      p_descr_ref          = lv_typedescr
    exceptions
      reference_is_initial = 1
      others               = 2
          .
  if sy-subrc <> 0.
* Do error handling later    
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  endif.

** How to Assign context table ?*
  *ASSIGN ? to <fs_data>.*

* try conversion
  try.
    call method cl_wdr_xml_convert_util=>if_wd_client_conversion_util~string_to_struct
      exporting
        in        = lv_data_string
        typedescr = lv_typedescr
      importing
        data      = <fs_data>
        .
  catch cx_wdr_conversion_exception .
  endtry.

  wd_context->bind_table(
    new_items              = <fs_data>
    set_initial_elements   = abap_true
  ).

endmethod.

Thanks for any advice ...

Best regards

Volker

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Anyone?

Former Member
0 Kudos

Hi Volker,

select single xmlstring from zkdp_gaxmldrafts into lv_data_string where atluser = sy-uname

and gaid = '0000000001'.

To me the approach should be to use the STRING operations that is something like spli

"Column headers

split xmlstring at cl_abap_char_utilities=>cr_lf into table s_table.

s_table should have all the lines in form of XML.

Greetings

Prashant

P.S. Points welcome

Former Member
0 Kudos

Well I did it the following way:

After putting the wd_context->to_xml() I added the normal beginning and ending of the simple transformation so that the XML data is the same, as by using


     CALL TRANSFORMATION id
         SOURCE <data>
         RESULT XML <data>.


* context to XML
  lv_data_string = wd_context->to_xml( ).

* modify XML Output
* to ABAP XML like in simple "id" transformation
  replace
    '<context>' in lv_data_string
    with '<?xml version="1.0" encoding="utf-16"?>#<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0"><asx:values>'.

  replace
    '</context>' in lv_data_string with '</asx:values></asx:abap>'.

For restoring the XML and set it back to context, you can then use the Simple XML Transformation like described in SAP help ...


* just for testing purposes with static ID
  select single xmlstring from zkdp_gaxmldrafts into lv_data_string
    where atluser = lv_userid
    and   gaid    = '0000000001'.

* This are my nodes which I have to restore from my XML structure
* Structure which is needed from XML-String
  data: begin of lv_componentcontroller,
          node1         type wd_this->element_node1,
          node2         type wd_this->element_node2,
          node3         type wd_this->element_node3,
          node4         type wd_this->elements_node4, "this is a table
          ....
        end of lv_componentcontroller.

* transform XML String to COMPONENTCONTROLLER Structure
      call transformation id
        source xml lv_data_string
        result componentcontroller = lv_componentcontroller.

* restore values to context
* like normal data binding to context

*node1
  data lo_nd_node1 type ref to if_wd_context_node.
  data lo_el_node1 type ref to if_wd_context_element.

* navigate from <CONTEXT> to <NODE1> via lead selection
  lo_nd_node1 = wd_context->get_child_node( name = wd_this->wdctx_node1 ).

* get element via lead selection
  lo_el_node1 = lo_nd_node1->get_element(  ).

* set all declared attributes
  lo_el_node1->set_static_attributes(
    exporting
      static_attributes = lv_componentcontroller-node1 ).

This is probably not Standard but it works for me.

Best regards

Volker

Former Member
0 Kudos

Hi Volker,

Went thru the code..its really cool to see something out of the box, never thought that entire context data getting manipulated as well formed XML( although i am sure its always trapnsported over the network the same way). If it works , nothing better

Greetings

Prashant