Sunday, 1 March 2015

MuleSoft - one to many xml file output



new to MuleSoft here, using their AnyPoint Studio IDE. Starting to get a hang of how flows and mapping data to different outputs from different inputs work.


There is a lingering question I can't quite figure out though. How do I generate an XML file based off of some database queries, such that the resulting XML file is populated correctly with the one-to-one data elements, yet also populated with the unbounded data elements?


Example: Let's say we had a customer that had one-to-one elements such as customer ID and zipcode. But they have multiple credit cards associated with them. Via SQL you could do your join properly to list out the customer owner for each credit card.


However, the output of the XML I'd want to look something like this:



<customer>
<name>Bill</name>
<custID>123456789</custID>
<zipcode>99999</zipcode>
<CreditCard>
<creditType>Visa</creditType>
<cardNumber>123456789</cardNumber>
</CreditCard>
<CreditCard>
<creditType>AmericanExpress</creditType>
<cardNumber>987665431</cardNumber>
</CreditCard>
<CreditCard>
<creditType>MasterCard</creditType>
<cardNumber>11111111111</cardNumber>
</CreditCard>
</customer>


Do you have to use multiple queries for this? As in, run a certain query against the DB that pulls that individual customer, then run a second query against the DB that pulls all the card information based off that customer? From there, somehow "merge" that data into a single XML file?


Struggling to understand how to set up a flow and generate such an xml file. What I'm trying to AVOID is duplication of customer data, such as in the example below:



<customer>
<name>Bill</name>
<custID>123456789</custID>
<zipcode>99999</zipcode>
<CreditCard>
<creditType>Visa</creditType>
<cardNumber>123456789</cardNumber>
</CreditCard>
</customer>

<customer>
<name>Bill</name>
<custID>123456789</custID>
<zipcode>99999</zipcode>
<CreditCard>
<creditType> MasterCard </creditType>
<cardNumber>11111111111</cardNumber>
</CreditCard>
</customer>
<customer>
<name>Bill</name>
<custID>123456789</custID>
<zipcode>99999</zipcode>
<CreditCard>
<creditType> AmericanExpress </creditType>
<cardNumber>987665431</cardNumber>
</CreditCard>
</customer>


Any help would be greatly appreciated!


No comments:

Post a Comment