Convert xml response to payload - mule

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}].

Related

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

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 }
}

how to implement nested multi level looping in dataweave in Mule

I am using dataweave for transforming XML to CSV. I want to know how to implement nested for loop in dataweave.
Below is the input xml:
<employee>
<id>1236</id>
<emplinfo>
<emplid>1961</emplid>
<jobinfo>
<status>T</status>
<title>Manager</title>
<start_date>2016-09-01</start_date>
</jobinfo>
<jobinfo>
<status>P</status>
<end_date>2016-08-31</end_date>
<title>Integration Manager</title>
<start_date>2016-08-01</start_date>
</jobinfo>
<jobinfo>
<status>A</status>
<end_date>2016-07-31</end_date>
<title>Communications Manager</title>
<start_date>2016-07-17</start_date>
</jobinfo>
</emplinfo>
<emplinfo>
<emplid>1801</emplid>
<jobinfo>
<status>T</status>
<title>AM</title>
<start_date>2016-09-01</start_date>
</jobinfo>
</emplinfo>
</employee>
Excepted output:
id empl_id status end_date title start_date
1236 1961 T Manager 2016-09-01
1236 1961 P 2016-08-31 Integration Manager 2016-08-01
1236 1961 A 2016-07-31 Communications Manager 2016-07-17
1236 1801 T AM 2016-09-01
Any help is greatly appreciated.
This works for me.
%dw 1.0
%output application/csv
---
flatten (payload map ((parent, parentindex) -> {
emplinfo:(parent.*emplinfo map ((emplinfo,empindex) -> {
jobinfo:(emplinfo.*jobinfo map ((jobinfo,jobindex) -> {
id: parent.id,
emplid : emplinfo.emplid,
status: jobinfo.status,
end_date:jobinfo.end_date,
title:jobinfo.title,
start_date:jobinfo.start_date
}))
}))
}))..jobinfo
I have used normal csv. you can choose any kind of format. Output is
id,emplid,status,end_date,title,start_date
1236,1961,T,,Manager,2016-09-01
1236,1961,P,2016-08-31,Integration Manager,2016-08-01
1236,1961,A,2016-07-31,Communications Manager,2016-07-17
1236,1801,T,,AM,2016-09-01
Hope this helps.
Do it something like this
Segment:(payload.segments map ((segment, segmentIndex)->
{
Leg:(segment.legs map ((leg, legIndex)->
{
Origin:leg.departureField.airportCodeField,
Destination:leg.arrivalField.airportCodeField
}))
}))
You have to use map operator for looping through the members of an array. The left hand side of map is the array which is processed and right hand side will have code for how members in it should be mapped. We can use lambda functions as well.
You can name the current member and refer it as well.
(payload.ARRAY map ((value,index) ->
{
id: parent.id,
emplid : value.emplid,
status: value.status,
}
)
Similiar depending on how you want to process the input array we can have nested map operators.
(payload.ARRAY map ((value,index) ->
{
(payload.ARRAY2 map ((value,index) ->
{
id: parent.id,
emplid : value.emplid,
status: value.status,
}
)

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
}