‎2010 Mar 14 5:04 PM
Hi,
does anyone know if Simple Transformations can be used to trasfer an XML data to an object? (instance of a class?)
all examples I saw are for structs and tables only, and not object references..
Thanks,
Mirit.
‎2010 Mar 14 8:43 PM
Yes. Please search forum about IF_SERIALIZABLE_OBJECT interface
‎2010 Mar 15 5:01 PM
Yeh, thanks for that.
what i still don't know is how to access the fields of the object inside the ST tself. like regular attribute of a struct?
because it still not recognizing the attributes, even after i implemented the serialization interface.
‎2010 Mar 15 5:26 PM
‎2010 Mar 16 5:04 PM
Here's the sample code I used to check the ST:
the object class (includes the if_serializabe_object):
-
class Z_PERSON definition
public
final
create public .
" public components of class Z_PERSON
" do not include other source files here!!!
public section.
interfaces IF_SERIALIZABLE_OBJECT .
interfaces Z_IF_ENTITY .
interfaces Z_IF_PAYABLE .
data SIMPLE_STRING type STRINGVAL .
methods WRITE_PROG .
protected section.
" protected components of class Z_PERSON
" do not include other source files here!!!
private section.
" private components of class Z_PERSON
" do not include other source files here!!!
ENDCLASS.
CLASS Z_PERSON IMPLEMENTATION.
ENDCLASS.
-
the ST code:
-
'<'?sap.transform simple?'>'
'<'tt:transform xmlns:tt="http://www.sap.com/transformation-templates"'>'
'<'tt:root name="ROOT" /'>'
'<'tt:template'>'
'<'tt:deserialize'>'
'<'sapesourcing'>'
' <'objects'>'
'<'object'>'
'<'fields'>'
'<'PERSON_NAME tt:value-ref=".ROOT.SIMPLE_STRING"/'>'
'<'/fields'>'
'<'/object'>'
'<'/objects'>'
'<'/sapesourcing'>'
'<'/tt:deserialize'>'
'<'/tt:template'>'
'<'/tt:transform'> '
-
the report which runs the transformation:
-
REPORT z_test_simple_transformation.
TYPES: BEGIN OF ls_xml_line,
data(256) TYPE x,
END OF ls_xml_line.
DATA:
exc TYPE REF TO cx_root,
lv_ixml TYPE REF TO if_ixml,
lv_xml_stream_factory TYPE REF TO if_ixml_stream_factory,
lv_xml_input_stream TYPE REF TO if_ixml_istream,
lv_xml_output_stream TYPE REF TO if_ixml_ostream,
lv_input_filepath TYPE string,
lv_output_filepath TYPE string,
lv_input_filelength TYPE i,
lv_output_filelength TYPE i,
lt_input_xml_table TYPE TABLE OF ls_xml_line,
lt_output_xml_table TYPE TABLE OF ls_xml_line.
TRY.
lv_output_filepath = 'C:\files\AmexGL1025\partial_GL1025_BoConverted_ABAP.xml'.
cl_gui_frontend_services=>gui_upload( EXPORTING filename = lv_output_filepath
filetype = 'BIN'
IMPORTING filelength = lv_output_filelength
CHANGING data_tab = lt_input_xml_table ).
lv_ixml = cl_ixml=>create( ).
lv_xml_stream_factory = lv_ixml->create_stream_factory( ).
lv_xml_input_stream = lv_xml_stream_factory->create_istream_itable( table = lt_input_xml_table size = lv_output_filelength ).
DATA lo_testing_object TYPE REF TO z_person.
CREATE OBJECT lo_testing_object TYPE z_person.
CALL TRANSFORMATION Z_simple_transformation_test SOURCE XML lv_xml_input_stream RESULT root = lo_testing_object .
CATCH cx_root INTO exc.
DATA lv_error_message TYPE string.
lv_error_message = exc->get_text( ).
WRITE lv_error_message.
ENDTRY.
-
I don't know why the indentation here is so awful.
If you can see what the bug is- it will be great...
Thanks!
Edited by: Mirit Mondani on Mar 16, 2010 6:05 PM
Edited by: Mirit Mondani on Mar 16, 2010 6:07 PM
‎2010 Mar 16 9:42 PM
‎2010 Mar 17 8:28 AM
of course..
(it's just a simple partial xml for the trying, but its compatible with the ST)
<?xml version="1.0" encoding="utf-8"?>
<sapesourcing>
<objects>
<object>
<fields>
<PERSON_NAME>string name</PERSON_NAME>
</fields>
</object>
</objects>
</sapesourcing>
‎2010 Mar 17 10:30 AM
I can see that it doesn't work, but don't know how to make it work. Usually, we serialize a variable into xml, and deserialize this xml (unchanged) to a variable. Why don't you do it this way?
The XML would be:
<?xml version="1.0" encoding="utf-16"?>
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<ROOTO href="#o3"/>
</asx:values>
<asx:heap xmlns:xsd="http://www.w3.org/2001/XMLSchem
a"
xmlns:abap="http://www.sap.com/abapxml/types/built-in"
xmlns:cls="http://www.sap.com/abapxml/classes/global"
xmlns:dic="http://www.sap.com/abapxml/types/dictionary">
<prg:Z_PERSON xmlns:prg="http://www.sap.com/abapxml/classes/program/Z_TEST" i
d="o3">
<local.Z_PERSON>
<SIMPLE_STRING>AA</SIMPLE_STRING>
</local.Z_PERSON>
</prg:Z_PERSON>
</asx:heap>
</asx:abap>
‎2010 Mar 17 1:35 PM
What i actually get is an xml file, which contains the whole object in it. so i need to deserialize the object at once.
what are you using in order to generate such an xml? ST that serializes a specific field?
‎2010 Mar 17 2:06 PM
To serialize, you just have to reverse the call transformation:
CALL TRANSFORMATION <transformation_name>
SOURCE root = variable
RESULT XML string.
‎2010 Mar 17 5:47 PM
I remember that you have conditioned the transformation to work only with deserialization (<tt:deserialize>) so you should remove it so that the transformation can process the serialization (with CALL TRANSFORMATION as I said above).
‎2010 Mar 18 7:59 AM
yes, I know. I conditioned it just for the clarification that it is used only for deserialization (the xml is given by outside system).
Thanks a lot !
I'll try to make this work for the whole object, instead of a variable.