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
}
}))
}
}
Related
How to transform json data as shown in input into XML as shown in output using MuleSoft's DataWeave language.
Input
{
"sponsors":[{
"SP1": "John Smith",
"NINO": "SC105012B",
"INCOME1": 10000,
"INCOME2":12000,
"DEPENDENTDETAILS": "No, FirstName, LastName, DateOfBirth, EduStatus, Income; 1, John, Smith, 12/12/84, studying at college or university, yes; 2, williams, Smith, 20/06/84, studying at college or university, No;"
}]
}
Expected Output
<sponsors>
<sp1>john smith</sp1>
<nino>sc15012b</nino>
<income1>10000</income1>
<income2>12000</income2>
<dependentdetails>
<no>1</no>
<firstname>john</firstname>
<lastname>smith</lastname>
<dateOfBirth>12/12/84</dateOfBirth>
<EduStatus>studying in college or university<EduStatus/>
<Income>yes</Income>
<no>2</no>
<firstname>williams</firstname>
<lastname>smith</lastname>
<dateOfBirth>20/06/84</dateOfBirth>
<EduStatus>studying in college or university<EduStatus/>
<Income>No</Income>
</dependentdetails>
</sponsors>
DEPENDENTDETAILS seems to be a string of headers and records separated by ; and each field separated by ,. Once we split by each of those characters we can get a list of field names and a list of values for each records that we can manipulate easily.
%dw 2.0
output application/xml
fun recordsToObjects(a)= do {
var headers=a[0]
---
a[1 to -1] map ($ map ((headers[$$]): $) reduce ($$++$)) reduce ($$++$)
}
---
sponsors: {
sp1: payload.sponsors[0].SP1,
nino: payload.sponsors[0].NINO,
income1: payload.sponsors[0].INCOME1,
income2: payload.sponsors[0].INCOME2,
dependentdetails:recordsToObjects( (payload.sponsors[0].DEPENDENTDETAILS splitBy ";") map ($ splitBy "," map trim($)) )
}
There is one more option that you can try
%dw 2.0
output application/xml
var dependentdetails = payload.sponsors.DEPENDENTDETAILS[0] splitBy ";" map( $ splitBy ", ")
var keys=dependentdetails[0]
var dependentXml={(flatten(dependentdetails filter $$>0 map (
$ map ((item, index) ->
(keys[index]) : trim(item)
)
)))}
---
sponsors : payload.sponsors map ($ mapObject ((value, key, index) ->
if(key as String == "DEPENDENTDETAILS")(key): dependentXml
else (key):value
))
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 }
}
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}].
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,
}
)
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
}