Dynamically adding an array elements in an existing XML element

Hi,
I want to adding dynamically an array elements in an existing XML element in my input parameters of my post SOAP request, but I stuck how to do it.

Here’s what I have done so far:
In the modeler input parameter I added my SOAP payload which contains:

<set: MyParentElement>${myParentElement}</set: MyParentElement>.

in order to get at the end (Dynamically) something like:

<set: MyParentElement>
<sch: child> child1</sch:child>
<sch: child> child2</sch:child>
<sch: child> child3</sch:child>
</set: MyParentElement>.

In my delegate where I create the payload:
public void execute(DelegateExecution execution) throw Excepltion{

String myParentElement = createXmlChilds(execution);
execution.setVariable(“set:MyParentElement”, myParentElement);

}

This method should add an array of elements, and which I’m stuck with.

public String createXmlChilds(DelegateExecution execution){
// I want to get the parent element but when I use SprinXmlTreeElement i got cannot be resolved to a type
SprinXmlTreeElement parentElement = XML("<MyParentElement/>");

// getting the childs from a JSONArray
SprinXmlTreeElement child1 = XML("<child/>");
SprinXmlTreeElement child2 = XML("<child/>");
SprinXmlTreeElement child3 = XML("<child/>");

// Convert the child’s array to a XML String and update the parent
parentElement.append(child1, child2, child3);
}

Can anyone help, please, how to achieve all above? or tell me if there is any missing configuration that needs to be done? or if there is a simpler way to achieve what I need?

Thanks in advance

Sab

Hi @yezsab,

can you please tell what exactly doesn’t work?
Which Camunda version do you use?

Using Spin, you can add XML child elements like this:

SpinXmlElement parent = Spin.XML("<parent/>");

parent.append(Spin.XML("<child/>"));
parent.append(Spin.XML("<child/>"));
parent.append(Spin.XML("<child/>"));

// result is '<parent> <child/>  <child/> <child/> </parent>'

Best regards,
Philipp

Hi @yezsab,

I achieve this by using a recent version of the freemarker engine (not the old jar shipped with Camunda). It has #list iterator allowing you to create multiple “child” entries in XML payload. So in a case below lines has a an array of objects corresponding to invoice lines. if you have n elements in lines array this iterator will produce n sections <invoiceDetail> </invoiceDetail>

<#list lines as line_item>
    <invoiceDetail>
        <GLOffset/>
        <amount>
            <#if foreign == true>
                <foreignTaxableAmount>${(sign*line_item.LineAmount)?c}</foreignTaxableAmount>
                <#else>
                    <taxableAmount>${(sign*line_item.LineAmount)?c}</taxableAmount>
                </#if>
            </amount>
            <payStatus>${payStatus}</payStatus>
            <payItem>${(line_item?index+1)?left_pad(3, "0")}</payItem>
            <processing>
                <payItemActionType>${payItemActionType}</payItemActionType>
            </processing>
            <taxRateArea>${line_item.LineTaxCode.taxCode}</taxRateArea>
            <paymentTerms>${paymentTerms}</paymentTerms>
            <remark>${line_item.LineDescription}</remark>
            <userReservedData>
                <userReservedCode>${userReservedCode}</userReservedCode>
            </userReservedData>
        </invoiceDetail>
</#list>

References: