Skip to Content
avatar image
Former Member

Adapter module for copying xml fields to another node

Hi Experts,

I have requirement to copy fields in xml to another node. We don't have XSD and much mapping to do. So decided to use adapter module. Just need to add <Batch> and </Batch> tags and copy DATA/FIELD4 to DATA1/FIELD2(repeat this step as many times we have DATA node). I managed to add <Batch> but not sure, how to copy fields. Any help will be really appreciated.

Input xml:

<MT_ROOT>

<DATA>

<FIELD1>value1</FIELD1>

<FIELD2>value2</FIELD2>

<FIELD3>value3</FIELD3>

<DATA1>

<FIELD1>value4</FIELD1>

</DATA1>

<FIELD4>value5</FIELD4>

</DATA>

<DATA>

<FIELD1>value1</FIELD1>

<FIELD2>value2</FIELD2>

<FIELD3>value3</FIELD3>

<DATA1>

<FIELD1>value4<FIELD1>

</DATA1>

<FIELD4>value5<FIELD4>

</DATA>

</MT_ROOT>

Expected xml:

<Batch>

<MT_ROOT>

<DATA>

<FIELD1>value1</FIELD1>

<FIELD2>value2</FIELD2>

<FIELD3>value3</FIELD3>

<DATA1>

<FIELD1>value4</FIELD1>

<FIELD2>value5</FIELD2>

</DATA1>

<FIELD4>value5<FIELD4>

</DATA>

<DATA>

<FIELD1>value1</FIELD1>

<FIELD2>value2</FIELD2>

<FIELD3>value3</FIELD3>

<FIELD4>value4</FIELD4>

<DATA1>

<FIELD1>value4</FIELD1>

<FIELD2>value5</FIELD2>

</DATA1>

<FIELD4>value5<FIELD4>

</DATA>

</MT_ROOT>

</Batch>

Thanks

module.jpg (53.6 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    avatar image
    Former Member
    Jan 26 at 01:38 PM

    Hi Tanu,

    I see several possibilities:

    1. If you like to stick to your adapter module, you could either use DOM or SAX parser to parse the whole document and do your changes on an object level. Or you could do it using the replace-method of the class String.

    2. I personally would've done these small changes in the document using a xslt-mapping. This way, you'll know what happens even if you don't have the Java source code at hand. Save it as mapping.xsl, zip it and upload it as Imported Archive in ESB. Then add it to your operation mapping.

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Create Batch root tag -->
        <xsl:template match="/">
            <Batch>
                <xsl:apply-templates />
            </Batch>
        </xsl:template>
        
        <!-- Copy whole document -->
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <!-- Special rule for DATA1, create tag -->
        <xsl:template match="DATA1">
            <DATA1>
                <!-- Copy the content of DATA1 -->
                <xsl:copy-of select="node()"/>
                <!-- Copy text of FIELD4 into FIELD2 -->
                <FIELD2>
                    <xsl:value-of select="../FIELD4/text()" />
                </FIELD2>
            </DATA1>
        </xsl:template>
        <!-- Special rule for FIELD4: Do nothing -->
        <xsl:template match="FIELD4" />
    </xsl:stylesheet>
    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Moritz,

      Thanks for your response. That's really helpful.

      I am just wondering, don't I need source and target XSD's if I go for XSLT. Now I am using dummy service interfaces and not using any mapping.

      Thanks,

      Tanu

  • Jan 31 at 06:05 AM

    Hi Tanu,

    You can achieve the above requirement by using the " Return as XML" option.

    Below are the things you need to do.

    you need to create a target field as batch and mention type as "string"

    create mapping structure as below

    Map your source root node to target root node and mention the below option during mapping.

    it will return the output as below.

    Regards,

    Manoj Kumar

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 31 at 09:49 AM

    Managed to do this in an adapter module. Even Moritz solution is working, If I use ICO rather than Iflow. But prefer to have Iflow, that's why used AM.

    Thanks Moritz and Manoj

    Add comment
    10|10000 characters needed characters exceeded