Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Conversion from ABAP into XML

Former Member
0 Kudos

Hi,

I am trying to convert data of internal table into XML, using below code

TYPES : BEGIN OF tp_vbap,

           vbeln TYPE vbap-vbeln,

           posnr TYPE vbap-posnr,

           matnt TYPE vbap-matnr,

         END OF tp_vbap.

DATA : tl_vbap TYPE STANDARD TABLE OF tp_vbap.

DATA : l_object TYPE REF TO cl_xml_document.

SELECT vbeln posnr matnr FROM vbap INTO TABLE tl_vbap  UP TO 100 ROWS.

CREATE OBJECT l_object.

l_object->create_with_data( dataobject tl_vbap ).

l_object->export_to_file( filename = l_pathtext ).


But in XML file all fields i get are in CAPITAL, like VBELN andd POSNR.I want it to be case sensitive.

How to do that?


Thanks.

1 ACCEPTED SOLUTION

saurabh_chikate
Active Participant
0 Kudos

Hi Bhavika,

You can use XSLT Transformation.

Pl follow this document.

By this procedure you have to

1. Create structure in SE11 same as your required structure.

2. Create XSLT Transformation.

3. Pass data from Internal Table to Transformation.

4. Capture output in String.

5. Download data using GUI_DOWNLOAD FM.

It will give you case sensitive XML tags in output.


Regards,

Saurabh

16 REPLIES 16

saurabh_chikate
Active Participant
0 Kudos

Hi Bhavika,

You can use XSLT Transformation.

Pl follow this document.

By this procedure you have to

1. Create structure in SE11 same as your required structure.

2. Create XSLT Transformation.

3. Pass data from Internal Table to Transformation.

4. Capture output in String.

5. Download data using GUI_DOWNLOAD FM.

It will give you case sensitive XML tags in output.


Regards,

Saurabh

0 Kudos

Thanks saurabh,

But isnt there any way of doing it by using class that i have used in code above?

0 Kudos

If you look through standard coding,

XML conversion is handled by Kernel Method.

i.e. We dont have any way to bypass/copy/enhance that code.

With my limited knowledge, I think it may not be possible to work this out with class.

0 Kudos

Thanks Saurabh,

I will try doing without class.

0 Kudos

You are welcome .

Pl note that XSLT will also give you output in all caps.

You have to change transformation source code manually. (it is quite easy)

By replacing caps to small you will get desired output.

0 Kudos

Hi Saurabh,

One morre thing, the user should be able to give name and path of XML file, so is the above method flexible to run that functionality even?

I am using call METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG, which gives popup to allow user for saving the XML file.

How do i do it in above method?

0 Kudos

Hi,


You can use any operations.

file path to FNAME.

output data to XTAB.


CALL FUNCTION 'GUI_DOWNLOAD'

   EXPORTING

     FILENAME    = FNAME

     REPLACEMENT = ' '

   TABLES

     DATA_TAB    = XTAB[]

.

PeterJonker
Active Contributor
0 Kudos

I think the fieldnames are upper case in SAP thats why you get them like that in your xml.

If you need them lower case you will have to build up your xml with the classes/interfaces: if_ixml , if_ixml_document and if_ixml_element.

,

0 Kudos

Hi Peter,

Could you please explain with small example?

0 Kudos

DATA: l_ixml            TYPE REF TO if_ixml.
DATA: lv_document        TYPE REF TO if_ixml_document.
DATA: lv_element_root    TYPE REF TO if_ixml_element.
DATA: lv_element_sub TYPE REF TO if_ixml_element.
DATA: lv_element_sub2 TYPE REF TO if_ixml_element.
DATA: l_streamfactory   TYPE REF TO if_ixml_stream_factory.
DATA: l_renderer        TYPE REF TO if_ixml_renderer.
DATA: l_ostream         TYPE REF TO if_ixml_ostream.
DATA l_rc              TYPE i.
DATA gv_xml_string TYPE string.
DATA l_value TYPE string.


* create an xml document

l_ixml = cl_ixml=>create( ).
lv_document = l_ixml->create_document( ).

* Create root element
lv_element_root  = lv_document->create_simple_element(
               name = 'SampleXML'
               parent = lv_document ).
*set attributes (in this case two namespaces
lv_element_root->set_attribute(
     name      = 'xmlns'
     value     = 'yournamespace.com'
        ).

lv_element_root->set_attribute(
       name      = 'xmlns:tn4'
       value     = 'anothernamespace.com'
          ).

* first tag has no value.
lv_element_sub  = lv_document->create_simple_element(
                      name = 'FirstTag'
                      parent = lv_element_root  ).

l_value = 'ValueSecondTag'.

lv_element_sub2  = lv_document->create_simple_element(
                     name = 'SecondTag'
                     value = l_value
                     parent = lv_element_sub  ).

l_value = 'ValueLastTag'.

lv_element_sub  = lv_document->create_simple_element(
                      name = 'LastTag'
                      value = l_value
                      parent = lv_element_root  ).

* now render the xml into a string object

l_streamfactory = l_ixml->create_stream_factory( ).

l_ostream = l_streamfactory->create_ostream_cstring(
                         string = gv_xml_string ).

l_renderer = l_ixml->create_renderer( ostream  = l_ostream
                                    document = lv_document ).
l_rc = l_renderer->render( ).


Now your xml is in gv_xml_string.:



0 Kudos

This message was moderated.

0 Kudos

I have copied your program as is , but i am not able to see XML file or in output.How do i see XML output, like you are showing one.

0 Kudos

I made the screenshot in debugger. In there you have an option to show xml. Doubleclick on gv_xml_string while in debug and check the xml view.

Or add code to download the file (gv_xml_string)

0 Kudos

Hi peter,

Thanks for quick reply.I have one more doubt.Need your help

When i am trying to provide string as

''http://w.w3.org/2001/XMLScance" version="3.1" xsi:noNamespaceSch'.

when i pass this string in values, the XML file is created, but it is blan.

I think it is because of '/', so how to do it.

0 Kudos

Sorry, but I only see your post now.

Can you post the sourcecode where you are adding this ?

I do not think '/' is the problem. But are you using the right quotes (single) ?

and shouldn't it be

xsi:noNamespaceSchemaLocation="http://place_where_your_xsd_is_located" ?

Because your string doesn't make real sense.

0 Kudos

Thanks Peter,

I have solved the issue, and i realized it was not because of '/'.