foreach loop example in Mule ESB

In this tutorial I am going to show you how we can use foreach loop in Mule ESB. We will use File Connector to take an XML file as input then we will iterate each entry of XML file content using foreach loop of JAXB object.

You can see also Convert JAXB Object to XML in Mule ESB and Convert XML to JAXB Object in Mule ESB


Mule Studio 3.x(Anypoint Studio) (Download from
Maven 3.2.1 (Download from
JDK 1.7 (Download from

Configure JDK, Maven and Mule Studio

Step 1. First install JDK
Step 2. Add the Java_Home/bin directory to your system’s PATH.
Step 3. After downloading Maven, extract it to a drive
Step 4. Add the M2_Home/bin directory to your system’s PATH.
Step 5. Download and extract Mule Studio to a drive
Step 6. Now start Mule Studio by clicking on AnypointStudio exe icon in the folder <physical drive>/AnypointStudio
Step 7. Once started, close the startup page
Step 8. In Mule Studio, go to Window -> Preferences. Expand Java, then click on Installed JREs. Add JDK 1.7 and select it. In expanded Java, click on Compiler and select the compiler level as 1.7
Step 9. Now expand Anypoint Studio and click on Maven Settings. Then select appropriate Maven installation home directory using Browse button.
Step 10. If you want you can input Default groupId for new projects, it will save your time every time when you want to create a new project.

Create Mule project in Mule Studio

Now we will see how to create a new project in Mule Studio(Anypoint Studio).

Step 1. In Anypoint Studio, go to File -> New -> Mule Project
Step 2. Input Project Name: mule-xml-java, Runtime is by default selected, tick on Use Maven; here the artifactId is automatically picked up from the Project Name:, the Group Id is picked up from the Default groupId for new projects and version is also a default value.
Step 3. Click Next and verify the JDK, mainly select Use default JRE(currently ‘jdk1.7.0_x’)
Step 4. Click on Next and click on Finish.

So when the project mule-xml-java is created in the Anypoint Studio, the project structure looks like below

mule xml to object

Convert XML file to JAXB object and use foreach to iterate through collection

mule foreach

Step 1. Open the mule-xml-java.xml file and click on Configuration XML view in the Editor
Step 2. Modify the mule-xml-java.xml file as shown below

In the above configuration we have first created the JAXB_Context using either spring:bean or mulexml:jaxb-context that is used later in mulexml:jaxb-xml-to-object-transformer.

Then we have <file:inbound-endpoint/> for taking an XML file as an input. Then we transform the file content or payload to string value using <file:file-to-string-transformer/>. We log the file content using <logger/>.

Next we transform the XML file data into JAXB Java object – we specify here the JAXB_Context to use for transforming and returnClass to what type of Java object it has to convert.

Then again we use <foreach/> loop for iterating over cpllection for logging message using <logger/> into the Console.

Step 3. Create catalog.xsd file in src/main/resources/xsd directory with below content

Step 4. Add XJC plugin to the pom.xml file for generating Java object from XSD

Step 5. Now build the project, you will and have been generated in com.roytuts.jaxb.model package

Step 6. Create sample XML file catalog.xml with below data

Running the application

Now do a right-click on the mule-xml-java project and click on Run As -> Mule Application. Then you will see something like below in Console when the application runs

Once the application is up and running put the catalog.xml file under D:AnypointWorkspace.

Console output

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.