DataWeave 2 - transform Java ArrayList to XML with array item tags - mule

I have a Java payload in Mule 4.3 which contains an ArrayList:
agentList = {ArrayList}
0 = {SomeClass}
id = "0"
name = "Agent0"
1 = {SomeClass}
id = "1"
name = "Agent1"
I want to transform this to XML as:
<agentList>
<agent>
<id>0</id>
<name>Agent0</name>
</agent>
<agent>
<id>1</id>
<name>Agent1</name>
</agent>
</agentList>
If I do a DataWeave transform output application/xml --- result: payload, I get this XML:
<result>
<agentList>
<id>0</id>
<name>Agent0</name>
</agentList>
<agentList>
<id>1</id>
<name>Agent1</name>
</agentList>
</result>
How can I transform the ArrayList so that I get each item enclosed by agent tags and the entire list as agentList?

%dw 2.0
output application/xml
---
result: {
agentList: payload.*agentList map (value) -> { agent: value }
}

Related

Karate: Match repeating element in xml

I'm trying to match a repeating element in a xml to karate schema.
XML message
* def xmlResponse =
"""
<Envelope>
<Header/>
<Body>
<Response>
<Customer>
<keys>
<primaryKey>1111111</primaryKey>
</keys>
<simplePay>false</simplePay>
</Customer>
<serviceGroupList>
<serviceGroup>
<name>XXXX</name>
<count>1</count>
<parentName>DDDDD</parentName>
<pendingCount>0</pendingCount>
<pendingHWSum>0.00</pendingHWSum>
</serviceGroup>
<serviceGroup>
<name>ZZZZZ</name>
<count>0</count>
<parentName/>
<pendingCount>3</pendingCount>
<pendingHWSum>399.00</pendingHWSum>
</serviceGroup>
</serviceGroupList>
</Response>
</Body>
</Envelope>
"""
I want to match each with following karate schema
Given def serviceGroupItem =
"""
<serviceGroup>
<name>##string</name>
<count>##string</count>
<parentName>##string</parentName>
<pendingCount>##string</pendingCount>
<pendingHWSum>##string</pendingHWSum>
</serviceGroup>
"""
This is how I tried
* xml serviceGroupListItems = get xmlResponse //serviceGroupList
* match each serviceGroupListItems == serviceGroupItem
But it doesn't work. Any idea how can I make it work
You have to match each serviceGroup.
* xml serviceGroupListItems = get xmlResponse //serviceGroupList
* match each serviceGroupListItems.serviceGroupList.serviceGroup == serviceGroupItem.serviceGroup

Convert xml response to payload

I have response coming like this -
<rsp stat="ok" version="1.0">
<result>
<tagObject>
<id>xx</id>
<tag_id>001</tag_id>
<type>Test</type>
<object_id>101</object_id>
<created_at>2013-10-09 11:41:45</created_at>
</tagObject>
<tagObject>
<id>xy</id>
<tag_id>002</tag_id>
<type>Test</type>
<object_id>102</object_id>
<created_at>2013-10-09 11:41:45</created_at>
</tagObject>
<tagObject>
<id>xz</id>
<tag_id>003</tag_id>
<type>Test</type>
<object_id>103</object_id>
<created_at>2013-10-09 11:43:44</created_at>
</tagObject>
</result>
</rsp>
Now I have to create a payload out of it, so that I can insert only <tag_id> and <object_id> into a database table.
Any suggestion on this? I was trying to work with Dataweave with Xpath3 but it did not work for me.
enter image description hereYou can convert Xml to Java map using dataweave
%dw 1.0
%output application/java
---
payload.rsp.result.*tagObject map ((tagObject , indexOfTagObject) -> {
tag_id: tagObject.tag_id as :string,
object_id: tagObject.object_id as :string
})
Out put: [{tag_id=001, object_id=101}, {tag_id=002, object_id=102}, {tag_id=003, object_id=103}].

How to access previous iteration element in mule dataweave

I want to access previous value element BaseChargePerUnit in the next iteration. The NewPrice value must be same in the next iteration as previous iteration's PreviousPrices value
%dw 1.0
%input payload application/xml
%output application/xml
---
using (w=payload.Order.OrderLines.*OrderLine default [], a=payload.*Order.PaymentMethods.*PaymentMethod default [])
{((payload.*Order default []) map {
POSLog:{
Transaction #(CancelFlag:"false"):{
((w default []) map {
LineItem #(EntryMethod:"Keyed"):{
Associate:{
CustomerOrderForDelivery:{
((w.LineCharges.*LineCharge default []) map {
RetailPriceModifier #(MethodCode:"AutomaticPromotion",VoidFlag:"false"):{
SequenceNumber:$$ + 1,
//Amount #(Action:"Subtract"):$.Extn.#BaseChargeAmount,
PreviousPrices:sessionVars.abc when $$ == 0 otherwise w[0].Extn.#BaseUnitPrice - $.Extn.#BaseChargePerUnit, // Clarify
NewPrice:w[0].Extn.#BaseUnitPrice - $.Extn.#BaseChargePerUnit
}
})
}}}})}}})}
This is sample output as
<?xml version='1.0' encoding='UTF-8'?>
<POSLog>
<Transaction CancelFlag="false">
<LineItem EntryMethod="Keyed">
<Associate>
<CustomerOrderForDelivery>
<RetailPriceModifier MethodCode="AutomaticPromotion" VoidFlag="false">
<SequenceNumber>1</SequenceNumber>
<PreviousPrices>10</PreviousPrices>
<NewPrice>8</NewPrice>
</RetailPriceModifier>
<RetailPriceModifier MethodCode="AutomaticPromotion" VoidFlag="false">
<SequenceNumber>2</SequenceNumber>
<PreviousPrices>8</PreviousPrices>
<NewPrice>7</NewPrice>
</RetailPriceModifier>
</CustomerOrderForDelivery>
</Associate>
</LineItem>
</Transaction>
</POSLog>
Input file is like this:
<?xml version="1.0" encoding="UTF-8"?>
<Order AuthorizationExpirationDate="2015-12-11T20:45:34-05:00">
<OrderLines>
<OrderLine AllocationDate="2015-12-11T00:00:00-05:00">
<Extn BaseLineTotal="3230.00" BaseListPrice="223.00" BaseUnitPrice="10" LineTotal="2000.00" POSDepartmentID="0623" />
<LineCharges>
<LineCharge ChargeAmount="40.00" ChargeCategory="SystemPromotion">
<Extn BaseChargeAmount="2" BaseChargePerLine="20.33" BaseChargePerUnit="2"/>
</LineCharge>
<LineCharge ChargeAmount="40.00" ChargeCategory="SystemPromotion" ChargeName="ProductDollarOffPromotion">
<Extn BaseChargeAmount="9.33" BaseChargePerLine="20.33" BaseChargePerUnit="1"/>
</LineCharge>
</LineCharges>
</OrderLine>
</OrderLines>
</Order>

ESB mule - Data Weaver transformation

I am new to mule esb. I am trying to fetch values from mySQL and transforming to XML. Then hitting service and then result will be stored back in DB table. I have two questions
1) I can see two different way of mapping in ESB mule.
a) Using "Data Mapper"
b) Using "Transform Message" - This will use data Weaver.
Which one of the transformation method will suit for my requirement.
2) I tried using "Transform Message" and Data Weaver for transformation.
My input from DB will fetch three rows as like below.
college = abc college = abc college = abc
dept = IT dept = CSE dept = MECH
No of students = 5 No of students = 4 No of students = 7
Expected output is
<University>
<college>abc</college> <!-- this is simple tag/ It will not repeat -->
<dept> <!-- This is complex tag -->
<dept Name>IT</dept Name>
<No of students>5</No of students>
</dept>
<dept>
<dept Name>IT</dept Name>
<No of students>5</No of students>
</dept>
<dept>
<dept Name>IT</dept Name>
<No of students>5</No of students>
</dept>
I tried data weaver something like below.
%dw 1.0
%output application/xml
"University":{
"college": payload.college,
dept: {
dept_Name: payload."dept_Name",
No of students: payload."No of students"
}
}
But the above dw is not giving expected result. Could you please help me on this ?
You can try this DataWeave, and modify as you need. Hope that helps.
%dw 1.0
%output application/xml
---
{
University: {
college: payload.college[0],
(payload map ((payload01 , indexOfPayload01) -> {
dept: {
deptName: payload01.dept,
Noofstudents: payload01."No of students" as :number
}
}))
}
}

How to add new tag in xml structure payload using MEL (mule)

I'm new to Mule, please guide me how to insert new tag inside the XML structure using Mule Expression Language (MEL).Need to insert B tag in the below XML structure
<Test>
<A>table 1</A>
<C>table 3</C>
</Test>
Thanks in Advance.
My dom4j fu is limited but here is what I came up with:
<mulexml:xml-to-dom-transformer returnClass="org.dom4j.Document" />
<expression-component><![CDATA[
bNode = message.payload.rootElement.addElement('B');
bNode.text = 'table 2';
message.payload.rootElement.elements().add(1, bNode.detach());
]]></expression-component>
<mulexml:dom-to-xml-transformer />
This works fine with Mule 3.4.0.
Use a Data Weave component as below:
%dw 1.0
%output application/xml
%var myValue='MyValue'
%var B=''
---
myoutput:{
data: payload.Test ++ B:myValue
}