Application Development and Automation 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: 
Read only

Reg : Simple transformations for generating XML

Former Member
0 Likes
931

Hello All,

I want to generate XML file in below format

<Root>

   <Node count = '1'>

       .....

       .....

   </Node>

   <Node count = '2'>

       .....

       .....

   </Node>

   <Node count = '3'>

       .....

       .....

   </Node>

</Root>

The closing node of <Node count>  is <Node>.

and the value present in single code should increment by one for every next node.

I am able to get the closing node of <Node count> as </Node>, but unable to get the counter value.

I am trying to do it be variables etc but could not get the exact logic.

Requesting to help in this regard.

With regards,

Sandeep Akella

8 REPLIES 8
Read only

Former Member
0 Likes
880

Hi Sandeep,

You can use a simple transformation to generate the xml file.

Example:

SIMPLE TRANSFORMATION

<?sap.transform simple?>

<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">

<tt:root name="ROOT"/>

  <tt:template>

    <root>

      <tt:loop ref=".ROOT" name="line">

        <node>

          <tt:attribute name="count" value-ref="$line.node.count" />

        </node>

      </tt:loop>

    </root>

  </tt:template>

</tt:transform>

REPORT

REPORT ztest_node_count.

TYPES: BEGIN OF type_node_attr,

         count TYPE i,

       END OF type_node_attr,

       BEGIN OF type_xml,

         node TYPE type_node_attr,

       END OF type_xml.

DATA: t_xml  TYPE STANDARD TABLE OF type_xml,

      s_xml  TYPE type_xml,

      strxml TYPE string.

START-OF-SELECTION.

  s_xml-node-count = 1.

  APPEND s_xml TO t_xml.

  s_xml-node-count = 2.

  APPEND s_xml TO t_xml.

  s_xml-node-count = 3.

  APPEND s_xml TO t_xml.

   CALL TRANSFORMATION znodes

    SOURCE root = t_xml

    RESULT XML strxml.

Reference: http://help.sap.com/saphelp_erp60_sp/helpdata/en/48/c21940a65ec442e10000000a1550b0/content.htm

Regards,

Christian

Read only

0 Likes
880

Hello Christian,

Thank you for your reply.

   By folowing your code I will get XML file as

<Root>

    <Node> 1

    </Node>

    <Node> 2

    </Node>

    <Node> 3

    </Node>

</Root>

But my requirement is

<Root>

    <Node count = "1">

     .....

        Some inner tags

     ......

    </Node>

    <Node count = "2">

      .....

        Some inner tags

     ......

    </Node>

    <Node count  = "3">

      .....

        Some inner tags

     ......

    </Node>

</Root>

count and its value is inside the xml tag.

with regards,

Sandeep Akella

Read only

0 Likes
880

Hi Sandeep,

The code below generates an empty tag node with an attribute count.

        <node>

          <tt:attribute name="count" value-ref="$line.node.count" />

        </node>

So if you run the report the result xml (strxml) will be:

<Root>

    <Node count = "1" />

    <Node count = "2" />

    <Node count = "3" />

</Root>

Read only

0 Likes
880

Hello Christian,

  Thank you for your reply.

Can we get the value of the attribute dynamically like from a field of internal table.

With regrds,

Sandeep Akella

Read only

0 Likes
880

Hello Christian,

  Thank you for your reply.

Can we get the value of the attribute dynamically like from a field of internal table.

With regrds,

Sandeep Akella

Read only

0 Likes
880

Hello Christian,

  Thank you for your reply.

Can we get the value of the attribute dynamically like from a field of internal table.

With regrds,

Sandeep Akella

Read only

0 Likes
880

Hi Sandeep,

That's exactly what the example report is doing

Populating the internal table t_xml

   s_xml-node-count = 1.

   APPEND s_xml TO t_xml.

 

  s_xml-node-count = 2.

  APPEND s_xml TO t_xml.

   s_xml-node-count = 3.

   APPEND s_xml TO t_xml.

and then passing the internal table as the source for the transformation

CALL TRANSFORMATION znodes

    SOURCE root = t_xml

    RESULT XML strxml.

Hope it helps.

Regards,

Christian

Read only

0 Likes
880

Hello Christian,

  Thank you for your reply.

Can we get the value of the attribute dynamically like from a field of internal table.

With regrds,

Sandeep Akella